mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-11 13:24:51 +00:00
Refactor signing/sending transactions
This commit is contained in:
parent
8b384e91dc
commit
cd206d730a
4 changed files with 70 additions and 226 deletions
|
@ -1,108 +0,0 @@
|
||||||
[
|
|
||||||
{
|
|
||||||
"constant": true,
|
|
||||||
"inputs": [],
|
|
||||||
"name": "name",
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "",
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"payable": false,
|
|
||||||
"stateMutability": "view",
|
|
||||||
"type": "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"constant": true,
|
|
||||||
"inputs": [],
|
|
||||||
"name": "symbol",
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "",
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"payable": false,
|
|
||||||
"stateMutability": "view",
|
|
||||||
"type": "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"constant": true,
|
|
||||||
"inputs": [],
|
|
||||||
"name": "decimals",
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "",
|
|
||||||
"type": "uint8"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"payable": false,
|
|
||||||
"stateMutability": "view",
|
|
||||||
"type": "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"constant": true,
|
|
||||||
"inputs": [
|
|
||||||
{
|
|
||||||
"name": "_owner",
|
|
||||||
"type": "address"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"name": "balanceOf",
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "balance",
|
|
||||||
"type": "uint256"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"payable": false,
|
|
||||||
"stateMutability": "view",
|
|
||||||
"type": "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"constant": false,
|
|
||||||
"inputs": [
|
|
||||||
{
|
|
||||||
"name": "_to",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "_value",
|
|
||||||
"type": "uint256"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"name": "transfer",
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "",
|
|
||||||
"type": "bool"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"payable": false,
|
|
||||||
"stateMutability": "nonpayable",
|
|
||||||
"type": "function"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"anonymous": false,
|
|
||||||
"inputs": [
|
|
||||||
{
|
|
||||||
"indexed": true,
|
|
||||||
"name": "_from",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": true,
|
|
||||||
"name": "_to",
|
|
||||||
"type": "address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"indexed": false,
|
|
||||||
"name": "_value",
|
|
||||||
"type": "uint256"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"name": "Transfer",
|
|
||||||
"type": "event"
|
|
||||||
}
|
|
||||||
]
|
|
|
@ -6,6 +6,7 @@ import 'package:cw_ethereum/pending_ethereum_transaction.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:http/http.dart';
|
import 'package:http/http.dart';
|
||||||
import 'package:web3dart/web3dart.dart';
|
import 'package:web3dart/web3dart.dart';
|
||||||
|
import 'package:web3dart/contracts/erc20.dart';
|
||||||
import 'package:cw_core/node.dart';
|
import 'package:cw_core/node.dart';
|
||||||
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
|
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
|
||||||
|
|
||||||
|
@ -64,76 +65,44 @@ class EthereumClient {
|
||||||
|
|
||||||
final price = await _client!.getGasPrice();
|
final price = await _client!.getGasPrice();
|
||||||
|
|
||||||
final Transaction transaction;
|
final Transaction transaction = Transaction(
|
||||||
|
from: privateKey.address,
|
||||||
|
to: EthereumAddress.fromHex(toAddress),
|
||||||
|
maxGas: gas,
|
||||||
|
gasPrice: price,
|
||||||
|
value: _isEthereum ? EtherAmount.inWei(BigInt.parse(amount)) : EtherAmount.zero(),
|
||||||
|
);
|
||||||
|
|
||||||
|
final signedTransaction = await _client!.signTransaction(privateKey, transaction);
|
||||||
|
|
||||||
|
final Function _sendTransaction;
|
||||||
|
|
||||||
if (_isEthereum) {
|
if (_isEthereum) {
|
||||||
transaction = Transaction(
|
_sendTransaction = () async => await sendTransaction(signedTransaction);
|
||||||
from: privateKey.address,
|
|
||||||
to: EthereumAddress.fromHex(toAddress),
|
|
||||||
maxGas: gas,
|
|
||||||
gasPrice: price,
|
|
||||||
value: EtherAmount.inWei(BigInt.parse(amount)),
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
/// ERC-20 currency
|
final erc20 = Erc20(
|
||||||
final String abi = await rootBundle.loadString("assets/abi_json/erc20_abi.json");
|
client: _client!,
|
||||||
final contractAbi = ContractAbi.fromJson(abi, "ERC20");
|
address: EthereumAddress.fromHex(_erc20Currencies[currency]!),
|
||||||
|
|
||||||
final contract = DeployedContract(
|
|
||||||
contractAbi,
|
|
||||||
EthereumAddress.fromHex(_erc20Currencies[currency]!),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
final originalAmount = BigInt.parse(amount) / BigInt.from(pow(10, 18));
|
final originalAmount = BigInt.parse(amount) / BigInt.from(pow(10, 18));
|
||||||
|
final int exponent = (await erc20.decimals()).toInt();
|
||||||
int exponent = await _getDecimalPlacesForContract(contract);
|
|
||||||
|
|
||||||
final _amount = BigInt.from(originalAmount * pow(10, exponent));
|
final _amount = BigInt.from(originalAmount * pow(10, exponent));
|
||||||
|
|
||||||
final transferFunction = contract.function('transfer');
|
_sendTransaction = () async {
|
||||||
final transferData =
|
await erc20.transfer(
|
||||||
transferFunction.encodeCall([EthereumAddress.fromHex(toAddress), _amount]);
|
EthereumAddress.fromHex(toAddress),
|
||||||
|
_amount,
|
||||||
transaction = Transaction(
|
credentials: privateKey,
|
||||||
from: privateKey.address,
|
);
|
||||||
to: contract.address,
|
};
|
||||||
maxGas: gas,
|
|
||||||
gasPrice: price,
|
|
||||||
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(transaction);
|
|
||||||
print(transaction.maxGas);
|
|
||||||
print(transaction.gasPrice);
|
|
||||||
print(transaction.value);
|
|
||||||
print("^^^^^^^^^^^^^^^^^^");
|
|
||||||
print(exponent);
|
|
||||||
print(originalAmount * pow(10, exponent));
|
|
||||||
print(_amount);
|
|
||||||
print((BigInt.from(transaction.maxGas!) * transaction.gasPrice!.getInWei) +
|
|
||||||
transaction.value!.getInWei);
|
|
||||||
|
|
||||||
sendERC20Token(EthereumAddress.fromHex(toAddress), currency, BigInt.parse(amount));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final signedTransaction = await _client!.signTransaction(privateKey, transaction);
|
|
||||||
|
|
||||||
return PendingEthereumTransaction(
|
return PendingEthereumTransaction(
|
||||||
signedTransaction: signedTransaction,
|
signedTransaction: signedTransaction,
|
||||||
amount: amount,
|
amount: amount,
|
||||||
fee: estimatedGas * price.getInWei,
|
fee: estimatedGas * price.getInWei,
|
||||||
sendTransaction: () => sendTransaction(signedTransaction),
|
sendTransaction: _sendTransaction,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,35 +166,19 @@ I/flutter ( 4474): Gas Used: 53000
|
||||||
// }
|
// }
|
||||||
|
|
||||||
Future<Map<CryptoCurrency, ERC20Balance>> fetchERC20Balances(EthereumAddress userAddress) async {
|
Future<Map<CryptoCurrency, ERC20Balance>> fetchERC20Balances(EthereumAddress userAddress) async {
|
||||||
final String abi = await rootBundle.loadString("assets/abi_json/erc20_abi.json");
|
|
||||||
final contractAbi = ContractAbi.fromJson(abi, "ERC20");
|
|
||||||
|
|
||||||
final Map<CryptoCurrency, ERC20Balance> erc20Balances = {};
|
final Map<CryptoCurrency, ERC20Balance> erc20Balances = {};
|
||||||
|
|
||||||
for (var currency in _erc20Currencies.keys) {
|
for (var currency in _erc20Currencies.keys) {
|
||||||
final contractAddress = _erc20Currencies[currency]!;
|
final contractAddress = _erc20Currencies[currency]!;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final contract = DeployedContract(
|
final erc20 = Erc20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
|
||||||
contractAbi,
|
final balance = await erc20.balanceOf(userAddress);
|
||||||
EthereumAddress.fromHex(contractAddress),
|
|
||||||
);
|
|
||||||
|
|
||||||
final balanceFunction = contract.function('balanceOf');
|
int exponent = (await erc20.decimals()).toInt();
|
||||||
final balance = await _client!.call(
|
|
||||||
contract: contract,
|
|
||||||
function: balanceFunction,
|
|
||||||
// test address: 0x1715a3E4A142d8b698131108995174F37aEBA10D
|
|
||||||
params: [userAddress],
|
|
||||||
);
|
|
||||||
|
|
||||||
BigInt tokenBalance = BigInt.parse(balance.first.toString());
|
erc20Balances[currency] = ERC20Balance(balance, exponent: exponent);
|
||||||
int exponent = await _getDecimalPlacesForContract(contract);
|
} catch (e) {
|
||||||
|
|
||||||
erc20Balances[currency] = ERC20Balance(tokenBalance, exponent: exponent);
|
|
||||||
} catch (e, s) {
|
|
||||||
print(e);
|
|
||||||
print(s);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,43 +186,43 @@ I/flutter ( 4474): Gas Used: 53000
|
||||||
return erc20Balances;
|
return erc20Balances;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> sendERC20Token(
|
// Future<bool> sendERC20Token(
|
||||||
EthereumAddress to, CryptoCurrency erc20Currency, BigInt amount) async {
|
// EthereumAddress to, CryptoCurrency erc20Currency, BigInt amount) async {
|
||||||
if (_erc20Currencies[erc20Currency] == null) {
|
// if (_erc20Currencies[erc20Currency] == null) {
|
||||||
throw "Unsupported ERC20 token";
|
// throw "Unsupported ERC20 token";
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
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");
|
||||||
|
//
|
||||||
final contract = DeployedContract(
|
// final contract = DeployedContract(
|
||||||
contractAbi,
|
// contractAbi,
|
||||||
EthereumAddress.fromHex(_erc20Currencies[erc20Currency]!),
|
// EthereumAddress.fromHex(_erc20Currencies[erc20Currency]!),
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
final transferFunction = contract.function('transfer');
|
// final transferFunction = contract.function('transfer');
|
||||||
final success = await _client!.call(
|
// final success = await _client!.call(
|
||||||
contract: contract,
|
// contract: contract,
|
||||||
function: transferFunction,
|
// function: transferFunction,
|
||||||
params: [to, amount],
|
// params: [to, amount],
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
return success.first as bool;
|
// return success.first as bool;
|
||||||
} catch (e) {
|
// } catch (e) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
Future<int> _getDecimalPlacesForContract(DeployedContract contract) async {
|
// Future<int> _getDecimalPlacesForContract(DeployedContract contract) async {
|
||||||
final decimalsFunction = contract.function('decimals');
|
// final decimalsFunction = contract.function('decimals');
|
||||||
final decimals = await _client!.call(
|
// final decimals = await _client!.call(
|
||||||
contract: contract,
|
// contract: contract,
|
||||||
function: decimalsFunction,
|
// function: decimalsFunction,
|
||||||
params: [],
|
// params: [],
|
||||||
);
|
// );
|
||||||
|
//
|
||||||
int exponent = int.parse(decimals.first.toString());
|
// int exponent = int.parse(decimals.first.toString());
|
||||||
return exponent;
|
// return exponent;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ class PendingEthereumTransaction with PendingTransaction {
|
||||||
EtherAmount.inWei(BigInt.parse(amount)).getValueInUnit(EtherUnit.ether).toString();
|
EtherAmount.inWei(BigInt.parse(amount)).getValueInUnit(EtherUnit.ether).toString();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> commit() async => sendTransaction();
|
Future<void> commit() async => await sendTransaction();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get feeFormatted => EtherAmount.inWei(fee).getValueInUnit(EtherUnit.ether).toString();
|
String get feeFormatted => EtherAmount.inWei(fee).getValueInUnit(EtherUnit.ether).toString();
|
||||||
|
|
|
@ -122,7 +122,6 @@ flutter:
|
||||||
- assets/text/
|
- assets/text/
|
||||||
- assets/faq/
|
- assets/faq/
|
||||||
- assets/animation/
|
- assets/animation/
|
||||||
- assets/abi_json/
|
|
||||||
|
|
||||||
fonts:
|
fonts:
|
||||||
- family: Lato
|
- family: Lato
|
||||||
|
|
Loading…
Reference in a new issue