Add initial approach for transaction sending for ERC-20 tokens

This commit is contained in:
OmarHatem 2023-06-06 01:17:26 +03:00
parent 3324ed61cd
commit 8b384e91dc

View file

@ -60,14 +60,9 @@ class EthereumClient {
required CryptoCurrency currency, required CryptoCurrency currency,
}) async { }) async {
bool _isEthereum = currency == CryptoCurrency.eth; bool _isEthereum = currency == CryptoCurrency.eth;
print("!!!!!!!!!!!!!!!!!!!");
final estimatedGas = BigInt.from(_isEthereum ? 21000 : 50000); final estimatedGas = BigInt.from(_isEthereum ? 21000 : 50000);
print("@@@@@@@@@@@@@@@");
print(estimatedGas);
final price = await _client!.getGasPrice(); final price = await _client!.getGasPrice();
print("################");
print(price);
final Transaction transaction; final Transaction transaction;
@ -79,7 +74,8 @@ class EthereumClient {
gasPrice: price, gasPrice: price,
value: EtherAmount.inWei(BigInt.parse(amount)), value: EtherAmount.inWei(BigInt.parse(amount)),
); );
} else { /// ERC-20 currency } else {
/// ERC-20 currency
final String abi = await rootBundle.loadString("assets/abi_json/erc20_abi.json"); final String abi = await rootBundle.loadString("assets/abi_json/erc20_abi.json");
final contractAbi = ContractAbi.fromJson(abi, "ERC20"); final contractAbi = ContractAbi.fromJson(abi, "ERC20");
@ -89,31 +85,33 @@ class EthereumClient {
); );
final originalAmount = BigInt.parse(amount) / BigInt.from(pow(10, 18)); final originalAmount = BigInt.parse(amount) / BigInt.from(pow(10, 18));
print("@@@@@@@@@@@@@");
print("originalAmount: $originalAmount");
final decimalsFunction = contract.function('decimals'); int exponent = await _getDecimalPlacesForContract(contract);
final decimals = await _client!.call(
contract: contract,
function: decimalsFunction,
params: [],
);
int exponent = int.parse(decimals.first.toString());
final transferFunction = contract.function('transfer');
final _amount = BigInt.from(originalAmount * pow(10, exponent)); final _amount = BigInt.from(originalAmount * pow(10, exponent));
transaction = Transaction.callContract( final transferFunction = contract.function('transfer');
contract: contract, final transferData =
function: transferFunction, transferFunction.encodeCall([EthereumAddress.fromHex(toAddress), _amount]);
parameters: [EthereumAddress.fromHex(toAddress), _amount],
transaction = Transaction(
from: privateKey.address, from: privateKey.address,
to: contract.address,
maxGas: gas, maxGas: gas,
gasPrice: price, gasPrice: price,
value: EtherAmount.inWei(_amount), value: EtherAmount.zero(),
data: transferData,
); );
// transaction = Transaction.callContract(
// contract: contract,
// function: transferFunction,
// parameters: [EthereumAddress.fromHex(toAddress), _amount],
// from: privateKey.address,
// maxGas: gas,
// gasPrice: price,
// value: EtherAmount.inWei(_amount),
// );
print("^^^^^^^^^^^^^^^^^^"); print("^^^^^^^^^^^^^^^^^^");
print(transaction); print(transaction);
print(transaction.maxGas); print(transaction.maxGas);
@ -123,7 +121,8 @@ class EthereumClient {
print(exponent); print(exponent);
print(originalAmount * pow(10, exponent)); print(originalAmount * pow(10, exponent));
print(_amount); print(_amount);
print((BigInt.from(transaction.maxGas!) * transaction.gasPrice!.getInWei) + transaction.value!.getInWei); print((BigInt.from(transaction.maxGas!) * transaction.gasPrice!.getInWei) +
transaction.value!.getInWei);
sendERC20Token(EthereumAddress.fromHex(toAddress), currency, BigInt.parse(amount)); sendERC20Token(EthereumAddress.fromHex(toAddress), currency, BigInt.parse(amount));
} }
@ -220,16 +219,8 @@ I/flutter ( 4474): Gas Used: 53000
params: [userAddress], params: [userAddress],
); );
final decimalsFunction = contract.function('decimals');
final decimals = await _client!.call(
contract: contract,
function: decimalsFunction,
params: [],
);
// 10.270282
BigInt tokenBalance = BigInt.parse(balance.first.toString()); BigInt tokenBalance = BigInt.parse(balance.first.toString());
int exponent = int.parse(decimals.first.toString()); int exponent = await _getDecimalPlacesForContract(contract);
erc20Balances[currency] = ERC20Balance(tokenBalance, exponent: exponent); erc20Balances[currency] = ERC20Balance(tokenBalance, exponent: exponent);
} catch (e, s) { } catch (e, s) {
@ -269,4 +260,16 @@ I/flutter ( 4474): Gas Used: 53000
return false; return false;
} }
} }
Future<int> _getDecimalPlacesForContract(DeployedContract contract) async {
final decimalsFunction = contract.function('decimals');
final decimals = await _client!.call(
contract: contract,
function: decimalsFunction,
params: [],
);
int exponent = int.parse(decimals.first.toString());
return exponent;
}
} }