forked from candidelabs/candide-mobile-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86c169d
commit 4d3cd66
Showing
24 changed files
with
963 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
import 'package:candide_mobile_app/config/network.dart'; | ||
import 'package:candide_mobile_app/controller/token_info_storage.dart'; | ||
import 'package:candide_mobile_app/utils/constants.dart'; | ||
import 'package:wallet_dart/wallet/user_operation.dart'; | ||
|
||
class FeeToken { | ||
TokenInfo token; | ||
BigInt fee; | ||
BigInt paymasterFee; | ||
BigInt exchangeRate; | ||
|
||
FeeToken({required this.token, required this.fee, required this.exchangeRate}); | ||
} | ||
FeeToken({required this.token, required this.fee, required this.paymasterFee, required this.exchangeRate}); | ||
|
||
class FeeCurrencyUtils { | ||
static final BigInt costOfPost = BigInt.from(45000); // todo shouldn't be hardcoded | ||
|
||
static BigInt calculateFee(UserOperation op, BigInt exchangeRate, bool isEther) { | ||
BigInt operationMaxEthCostUsingPaymaster = op.maxFeePerGas * (costOfPost + op.callGasLimit + (op.verificationGasLimit * BigInt.from(isEther ? 1 : 3)) + op.preVerificationGas); | ||
BigInt tokenToEthPrice = (operationMaxEthCostUsingPaymaster * exchangeRate) ~/ BigInt.from(10).pow(18); | ||
return tokenToEthPrice; | ||
BigInt calculateETHFee(UserOperation op, Network network) { | ||
bool isEther = token.symbol == network.nativeCurrency && token.address == Constants.addressZeroHex; | ||
BigInt operationMaxEthCost = op.maxFeePerGas * (costOfPost + op.callGasLimit + (op.verificationGasLimit * BigInt.from(isEther ? 1 : 3)) + op.preVerificationGas); | ||
return operationMaxEthCost; | ||
} | ||
|
||
BigInt calculateFee(UserOperation op, Network network) { | ||
BigInt operationMaxEthCost = calculateETHFee(op, network); | ||
BigInt tokenFee = (operationMaxEthCost * exchangeRate) ~/ BigInt.from(10).pow(18); | ||
tokenFee += paymasterFee; | ||
return tokenFee; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'package:candide_mobile_app/config/network.dart'; | ||
import 'package:candide_mobile_app/models/paymaster/source/paymaster_contract.g.dart'; | ||
import 'package:candide_mobile_app/utils/constants.dart'; | ||
import 'package:wallet_dart/wallet/account.dart'; | ||
import 'package:web3dart/credentials.dart'; | ||
|
||
class GasBackData { | ||
EthereumAddress paymaster; | ||
String paymasterAndData; | ||
bool gasBackApplied; | ||
|
||
GasBackData({required this.paymaster, required this.paymasterAndData, required this.gasBackApplied}); | ||
|
||
static Future<GasBackData> getGasBackData(Account account, EthereumAddress _paymaster, Network network, BigInt maxETHCost) async { | ||
PaymasterContract paymasterContract = PaymasterContract(address: _paymaster, client: network.client); | ||
BigInt accountGasBack = await paymasterContract.gasBackBalances(account.address); | ||
bool gasBackApplied = accountGasBack >= maxETHCost; | ||
String paymasterAndData = "0x"; | ||
if (gasBackApplied){ | ||
paymasterAndData += _paymaster.hexNo0x; | ||
paymasterAndData += Constants.addressZero.hexNo0x; // no token | ||
paymasterAndData += BigInt.from(2).toRadixString(16).padLeft(2, '0'); // paymaster mode = 2 (GAS_BACK) | ||
paymasterAndData += BigInt.from(0).toRadixString(16).padLeft(12, '0'); // validUntil = 0 (48 bits = 6 bytes = 12 hex chars) | ||
paymasterAndData += BigInt.from(0).toRadixString(16).padLeft(64, '0'); // fee = 0 (256 bits = 32 bytes = 64 hex chars) | ||
paymasterAndData += BigInt.from(0).toRadixString(16).padLeft(64, '0'); // exchangeRate = 0 | ||
} | ||
return GasBackData( | ||
paymaster: _paymaster, | ||
paymasterAndData: paymasterAndData, | ||
gasBackApplied: gasBackApplied, | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.