Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/infrastructure/blockchain/blockchain_info_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ class BlockchainInfoRepository {
}

Future<Map<String, String>> fetchGasPrice() async {
final GasPrices200Response? gasPrice = await queryApi.gasPrices();
final price = gasPrice?.prices.firstOrNull;
final double? gasPriceValue = double.tryParse(price?.amount ?? "0.01");
final String denom = price?.denom ?? feeDenom;
final EvmBaseFee200Response? gasPrice = await queryApi.baseFee();
final price = gasPrice?.baseFee;
final double? gasPriceValue = double.tryParse(price ?? "0.02");
return {
"gasPrice": "$gasPriceValue $denom",
"gasPrice": "$gasPriceValue $feeDenom",
"amount": gasPriceValue.toString(),
"denom": denom
"denom": feeDenom
};
}
}
2 changes: 1 addition & 1 deletion lib/infrastructure/blockchain/wallet_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class WalletRepository {
String gasPriceStr,
mantra.Simulate200ResponseGasInfo gasInfo,
) {
final gasPrice = double.parse(gasPriceStr);
final gasPrice = double.parse(gasPriceStr) * gasLimitMultiplier;
final gasLimit = double.parse(
gasInfo.gasUsed ?? defaultGasUsed
) * gasLimitMultiplier;
Expand Down
4 changes: 2 additions & 2 deletions lib/infrastructure/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ const String feeDenom = "uom";
const int feeExponent = 6;
// For building transactions, lib/infrastructure/blockchain/wallet_repository.dart
const String defaultGasUsed = "30000";
const double gasLimitMultiplier = 3;
const String explorerUrl = "https://explorer.mantrachain.io/MANTRA-Dukong";
const double gasLimitMultiplier = 2;
const String explorerUrl = "https://mantrascan.io/dukong";
2 changes: 1 addition & 1 deletion lib/presentation/home_page/dapps/tx_status_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class _TxStatusWidgetState extends State<TxStatusWidget> {
context,
MaterialPageRoute(
builder: (context) => Browser(
title: "MANTRA Explorer",
title: "MANTRA Scan",
url: "$explorerUrl/tx/$_txHash",
),
),
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: main
resolved-ref: "63edd4cc2da12b80a002e3111d7f957cc5b5a761"
ref: "v0.2.0"
resolved-ref: "23df8d15c796f7bbf3ad2d5a7bda412437a4a8c2"
url: "https://github.com/BigtoC/mantrachain-dart.git"
source: git
version: "1.0.0"
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 3.0.3
version: 3.1.0

environment:
sdk: "^3.8.0"
Expand All @@ -41,7 +41,7 @@ dependencies:
mantrachain_dart_sdk:
git:
url: https://github.com/BigtoC/mantrachain-dart.git
ref: main
ref: v0.2.0
cosmos_sdk: ^2.7.0
blockchain_utils: ^5.0.0
flutter_secure_storage: ^9.2.4
Expand Down
55 changes: 28 additions & 27 deletions test/infrastructure/blockchain/wallet_repository_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ void main() {
group("_calculateFee", () {
// Create a test-only method that exposes the private _calculateFee method
Fee calculateFeeForTesting(
String gasPriceStr, mantra.Simulate200ResponseGasInfo gasInfo
String gasPriceStr,
mantra.Simulate200ResponseGasInfo gasInfo,
) {
return walletRepository.calculateFee(gasPriceStr, gasInfo);
}
Expand All @@ -25,7 +26,7 @@ void main() {
final gasPriceStr = "0.025";
final gasInfo = mantra.Simulate200ResponseGasInfo(
gasUsed: "100000",
gasWanted: "150000"
gasWanted: "150000",
);

// Act
Expand All @@ -36,31 +37,32 @@ void main() {
expect(fee.amount.length, 1);
expect(fee.amount[0].denom, feeDenom);

// Expected amount = gasPrice * gasLimit = 0.025 * (100000 * 3) = 7500
expect(fee.amount[0].amount, BigInt.from(7500));
expect(fee.amount[0].amount, BigInt.from(10000));
});

test("should use default gas when gasUsed is null", () {
// Arrange
final gasPriceStr = "0.025";
final gasInfo = mantra.Simulate200ResponseGasInfo(
gasUsed: null,
gasWanted: "150000"
gasWanted: "150000",
);

// Act
final fee = calculateFeeForTesting(gasPriceStr, gasInfo);

// Assert
expect(fee.gasLimit, BigInt.from(
int.parse(defaultGasUsed) * gasLimitMultiplier)
expect(
fee.gasLimit,
BigInt.from(int.parse(defaultGasUsed) * gasLimitMultiplier),
);

// Expected amount = gasPrice * gasLimit = 0.025 * (30000 * 1.5) = 1125
// (assuming defaultGasUsed is '30000')
final expectedAmount = BigInt.from(double.parse(gasPriceStr) *
double.parse(defaultGasUsed) *
gasLimitMultiplier);
final expectedAmount = BigInt.from(
double.parse(gasPriceStr) *
gasLimitMultiplier *
double.parse(defaultGasUsed) *
gasLimitMultiplier,
);
expect(fee.amount[0].amount, expectedAmount);
});

Expand All @@ -69,29 +71,30 @@ void main() {
final gasPriceStr = "0.01234";
final gasInfo = mantra.Simulate200ResponseGasInfo(
gasUsed: "123456",
gasWanted: "200000"
gasWanted: "200000",
);

// Act
final fee = calculateFeeForTesting(gasPriceStr, gasInfo);

// Assert
// Check that the result is properly rounded with no decimal places
// Expected calculation: 0.01234 * (123456 * 3) = 4570.0512, truncated to 4570
final rawAmount = double.parse(gasPriceStr) *
double.parse(gasInfo.gasUsed!) *
gasLimitMultiplier;
final rawAmount =
double.parse(gasPriceStr) *
gasLimitMultiplier *
double.parse(gasInfo.gasUsed!) *
gasLimitMultiplier;
final expectedAmount = BigInt.parse(rawAmount.floor().toString());
expect(fee.amount[0].amount, expectedAmount);
expect(fee.amount[0].amount, BigInt.from(4570));
expect(fee.amount[0].amount, BigInt.from(6093));
});

test("should handle high gas values properly", () {
// Arrange
final gasPriceStr = "0.0001";
final gasInfo = mantra.Simulate200ResponseGasInfo(
gasUsed: "10000000", // 10 million gas
gasWanted: "15000000"
gasWanted: "15000000",
);

// Act
Expand All @@ -100,8 +103,7 @@ void main() {
// Assert
expect(fee.gasLimit, BigInt.from(10000000 * gasLimitMultiplier));

// Expected amount = 0.0001 * (10000000 * 3) = 3000
final expectedAmount = BigInt.from(3000);
final expectedAmount = BigInt.from(4000);
expect(fee.amount[0].amount, expectedAmount);
});

Expand All @@ -110,22 +112,21 @@ void main() {
final gasPriceStr = "0.05";
final gasInfo = mantra.Simulate200ResponseGasInfo(
gasUsed: "99999", // odd number to test rounding
gasWanted: "100000"
gasWanted: "100000",
);

// Act
final fee = calculateFeeForTesting(gasPriceStr, gasInfo);

// Assert
// Expected gasLimit = 99999 * 1.5 = 149998.5, rounded to 149999
final expectedGasLimit = BigInt.parse(
(99999 * gasLimitMultiplier).toStringAsFixed(0)
(99999 * gasLimitMultiplier).toStringAsFixed(0),
);
expect(fee.gasLimit, expectedGasLimit);

// Expected amount = 0.05 * (99999 * 3) = 14999.85, floored to 14999
final expectedAmount = BigInt.parse(
(0.05 * 99999 * gasLimitMultiplier).floor().toString()
(0.05 * gasLimitMultiplier * 99999 * gasLimitMultiplier)
.floor()
.toString(),
);
expect(fee.amount[0].amount, expectedAmount);
});
Expand Down