mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 12:29:51 +00:00
parent
b77c22b0df
commit
d332377a2b
36 changed files with 402 additions and 667 deletions
|
@ -235,7 +235,7 @@ class TronClient {
|
|||
String contractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
|
||||
String constantAmount =
|
||||
'0'; // We're using 0 as the base amount here as we get an error when balance is zero i.e for new wallets.
|
||||
final contract = ContractABI.fromJson(trc20Abi);
|
||||
final contract = ContractABI.fromJson(trc20Abi, isTron: true);
|
||||
|
||||
final function = contract.functionFromName("transfer");
|
||||
|
||||
|
@ -405,7 +405,7 @@ class TronClient {
|
|||
String contractAddress,
|
||||
BigInt tronBalance,
|
||||
) async {
|
||||
final contract = ContractABI.fromJson(trc20Abi);
|
||||
final contract = ContractABI.fromJson(trc20Abi, isTron: true);
|
||||
|
||||
final function = contract.functionFromName("transfer");
|
||||
|
||||
|
@ -483,7 +483,7 @@ class TronClient {
|
|||
|
||||
final tokenAddress = TronAddress(contractAddress);
|
||||
|
||||
final contract = ContractABI.fromJson(trc20Abi);
|
||||
final contract = ContractABI.fromJson(trc20Abi, isTron: true);
|
||||
|
||||
final function = contract.functionFromName("balanceOf");
|
||||
|
||||
|
@ -510,7 +510,7 @@ class TronClient {
|
|||
|
||||
final ownerAddress = TronAddress(userAddress);
|
||||
|
||||
final contract = ContractABI.fromJson(trc20Abi);
|
||||
final contract = ContractABI.fromJson(trc20Abi, isTron: true);
|
||||
|
||||
final name =
|
||||
(await getTokenDetail(contract, "name", ownerAddress, tokenAddress) as String?) ?? '';
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import '.secrets.g.dart' as secrets;
|
||||
import 'package:on_chain/tron/tron.dart';
|
||||
import '.secrets.g.dart' as secrets;
|
||||
|
||||
class TronHTTPProvider implements TronServiceProvider {
|
||||
TronHTTPProvider(
|
||||
|
@ -8,37 +10,34 @@ class TronHTTPProvider implements TronServiceProvider {
|
|||
http.Client? client,
|
||||
this.defaultRequestTimeout = const Duration(seconds: 30)})
|
||||
: client = client ?? http.Client();
|
||||
|
||||
@override
|
||||
final String url;
|
||||
final http.Client client;
|
||||
final Duration defaultRequestTimeout;
|
||||
|
||||
@override
|
||||
Future<TronServiceResponse<T>> doRequest<T>(TronRequestDetails params,
|
||||
{Duration? timeout}) async {
|
||||
if (!params.type.isPostRequest) {
|
||||
final response = await client.get(
|
||||
params.toUri(url),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey,
|
||||
if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey,
|
||||
},
|
||||
).timeout(timeout ?? defaultRequestTimeout);
|
||||
return params.toResponse(response.bodyBytes, response.statusCode);
|
||||
}
|
||||
Future<Map<String, dynamic>> get(TronRequestDetails params, [Duration? timeout]) async {
|
||||
final response = await client.get(Uri.parse(params.url(url)), headers: {
|
||||
'Content-Type': 'application/json',
|
||||
if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey,
|
||||
if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey,
|
||||
}).timeout(timeout ?? defaultRequestTimeout);
|
||||
final data = json.decode(response.body) as Map<String, dynamic>;
|
||||
return data;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> post(TronRequestDetails params, [Duration? timeout]) async {
|
||||
final response = await client
|
||||
.post(
|
||||
params.toUri(url),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey,
|
||||
if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey,
|
||||
},
|
||||
body: params.body(),
|
||||
)
|
||||
.post(Uri.parse(params.url(url)),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
if (url.contains("trongrid")) 'TRON-PRO-API-KEY': secrets.tronGridApiKey,
|
||||
if (url.contains("nownodes")) 'api-key': secrets.tronNowNodesApiKey,
|
||||
},
|
||||
body: params.toRequestBody())
|
||||
.timeout(timeout ?? defaultRequestTimeout);
|
||||
return params.toResponse(response.bodyBytes, response.statusCode);
|
||||
final data = json.decode(response.body) as Map<String, dynamic>;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import 'package:blockchain_utils/hex/hex.dart';
|
||||
import 'package:on_chain/on_chain.dart';
|
||||
import 'package:on_chain/solidity/address/core.dart';
|
||||
|
||||
class TronTRC20TransactionModel extends TronTransactionModel {
|
||||
String? transactionId;
|
||||
|
@ -189,7 +188,7 @@ class Value {
|
|||
output = output.replaceFirst('0x', '').substring(8);
|
||||
final abiCoder = ABICoder.fromType('address');
|
||||
final decoded = abiCoder.decode(AbiParameter.bytes, hex.decode(output));
|
||||
final tronAddress = TronAddress.fromEthAddress((decoded.result as SolidityAddress).toBytes());
|
||||
final tronAddress = TronAddress.fromEthAddress((decoded.result as ETHAddress).toBytes());
|
||||
|
||||
return tronAddress.toString();
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import 'package:cw_tron/tron_transaction_info.dart';
|
|||
import 'package:cw_tron/tron_wallet_addresses.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:on_chain/on_chain.dart' as on_chain;
|
||||
import 'package:on_chain/on_chain.dart';
|
||||
|
||||
part 'tron_wallet.g.dart';
|
||||
|
||||
|
@ -74,13 +74,13 @@ abstract class TronWalletBase
|
|||
|
||||
late final Box<TronToken> tronTokensBox;
|
||||
|
||||
late final on_chain.TronPrivateKey _tronPrivateKey;
|
||||
late final TronPrivateKey _tronPrivateKey;
|
||||
|
||||
late final on_chain.TronPublicKey _tronPublicKey;
|
||||
late final TronPublicKey _tronPublicKey;
|
||||
|
||||
on_chain.TronPublicKey get tronPublicKey => _tronPublicKey;
|
||||
TronPublicKey get tronPublicKey => _tronPublicKey;
|
||||
|
||||
on_chain.TronPrivateKey get tronPrivateKey => _tronPrivateKey;
|
||||
TronPrivateKey get tronPrivateKey => _tronPrivateKey;
|
||||
|
||||
late String _tronAddress;
|
||||
|
||||
|
@ -190,7 +190,7 @@ abstract class TronWalletBase
|
|||
|
||||
String idFor(String name, WalletType type) => '${walletTypeToString(type).toLowerCase()}_$name';
|
||||
|
||||
Future<on_chain.TronPrivateKey> getPrivateKey({
|
||||
Future<TronPrivateKey> getPrivateKey({
|
||||
String? mnemonic,
|
||||
String? privateKey,
|
||||
required String password,
|
||||
|
@ -198,7 +198,7 @@ abstract class TronWalletBase
|
|||
}) async {
|
||||
assert(mnemonic != null || privateKey != null);
|
||||
|
||||
if (privateKey != null) return on_chain.TronPrivateKey(privateKey);
|
||||
if (privateKey != null) return TronPrivateKey(privateKey);
|
||||
|
||||
final seed = bip39.mnemonicToSeed(mnemonic!, passphrase: passphrase ?? '');
|
||||
|
||||
|
@ -207,7 +207,7 @@ abstract class TronWalletBase
|
|||
|
||||
final childKey = bip44.deriveDefaultPath;
|
||||
|
||||
return on_chain.TronPrivateKey.fromBytes(childKey.privateKey.raw);
|
||||
return TronPrivateKey.fromBytes(childKey.privateKey.raw);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -242,10 +242,10 @@ abstract class TronWalletBase
|
|||
|
||||
Future<void> _getEstimatedFees() async {
|
||||
final nativeFee = await _getNativeTxFee();
|
||||
nativeTxEstimatedFee = on_chain.TronHelper.fromSun(BigInt.from(nativeFee));
|
||||
nativeTxEstimatedFee = TronHelper.fromSun(BigInt.from(nativeFee));
|
||||
|
||||
final trc20Fee = await _getTrc20TxFee();
|
||||
trc20EstimatedFee = on_chain.TronHelper.fromSun(BigInt.from(trc20Fee));
|
||||
trc20EstimatedFee = TronHelper.fromSun(BigInt.from(trc20Fee));
|
||||
|
||||
log('Native Estimated Fee: $nativeTxEstimatedFee');
|
||||
log('TRC20 Estimated Fee: $trc20EstimatedFee');
|
||||
|
@ -323,7 +323,7 @@ abstract class TronWalletBase
|
|||
totalAmount = walletBalanceForCurrency;
|
||||
} else {
|
||||
final totalOriginalAmount = double.parse(output.cryptoAmount ?? '0.0');
|
||||
totalAmount = on_chain.TronHelper.toSun(totalOriginalAmount.toString());
|
||||
totalAmount = TronHelper.toSun(totalOriginalAmount.toString());
|
||||
}
|
||||
|
||||
if (walletBalanceForCurrency < totalAmount || totalAmount < BigInt.zero) {
|
||||
|
@ -338,7 +338,7 @@ abstract class TronWalletBase
|
|||
toAddress: tronCredentials.outputs.first.isParsedAddress
|
||||
? tronCredentials.outputs.first.extractedAddress!
|
||||
: tronCredentials.outputs.first.address,
|
||||
amount: on_chain.TronHelper.fromSun(totalAmount),
|
||||
amount: TronHelper.fromSun(totalAmount),
|
||||
currency: transactionCurrency,
|
||||
tronBalance: tronBalance,
|
||||
sendAll: shouldSendAll,
|
||||
|
@ -355,9 +355,9 @@ abstract class TronWalletBase
|
|||
|
||||
final Map<String, TronTransactionInfo> result = {};
|
||||
|
||||
final contract = on_chain.ContractABI.fromJson(trc20Abi);
|
||||
final contract = ContractABI.fromJson(trc20Abi, isTron: true);
|
||||
|
||||
final ownerAddress = on_chain.TronAddress(_tronAddress);
|
||||
final ownerAddress = TronAddress(_tronAddress);
|
||||
|
||||
for (var transactionModel in transactions) {
|
||||
if (transactionModel.isError) {
|
||||
|
@ -371,7 +371,7 @@ abstract class TronWalletBase
|
|||
|
||||
String? tokenSymbol;
|
||||
if (transactionModel.contractAddress != null) {
|
||||
final tokenAddress = on_chain.TronAddress(transactionModel.contractAddress!);
|
||||
final tokenAddress = TronAddress(transactionModel.contractAddress!);
|
||||
|
||||
tokenSymbol = (await _client.getTokenDetail(
|
||||
contract,
|
||||
|
@ -385,10 +385,9 @@ abstract class TronWalletBase
|
|||
result[transactionModel.hash] = TronTransactionInfo(
|
||||
id: transactionModel.hash,
|
||||
tronAmount: transactionModel.amount ?? BigInt.zero,
|
||||
direction:
|
||||
on_chain.TronAddress(transactionModel.from!, visible: false).toAddress() == address
|
||||
? TransactionDirection.outgoing
|
||||
: TransactionDirection.incoming,
|
||||
direction: TronAddress(transactionModel.from!, visible: false).toAddress() == address
|
||||
? TransactionDirection.outgoing
|
||||
: TransactionDirection.incoming,
|
||||
blockTime: transactionModel.date,
|
||||
txFee: transactionModel.fee,
|
||||
tokenSymbol: tokenSymbol ?? "TRX",
|
||||
|
@ -605,13 +604,11 @@ abstract class TronWalletBase
|
|||
if (address == null) {
|
||||
return false;
|
||||
}
|
||||
on_chain.TronPublicKey pubKey =
|
||||
on_chain.TronPublicKey.fromPersonalSignature(ascii.encode(message), signature)!;
|
||||
TronPublicKey pubKey = TronPublicKey.fromPersonalSignature(ascii.encode(message), signature)!;
|
||||
return pubKey.toAddress().toString() == address;
|
||||
}
|
||||
|
||||
String getTronBase58AddressFromHex(String hexAddress) =>
|
||||
on_chain.TronAddress(hexAddress).toAddress();
|
||||
String getTronBase58AddressFromHex(String hexAddress) => TronAddress(hexAddress).toAddress();
|
||||
|
||||
void updateScanProviderUsageState(bool isEnabled) {
|
||||
if (isEnabled) {
|
||||
|
|
|
@ -18,11 +18,11 @@ dependencies:
|
|||
on_chain:
|
||||
git:
|
||||
url: https://github.com/cake-tech/on_chain.git
|
||||
ref: cake-update-v4
|
||||
ref: cake-update-v2
|
||||
blockchain_utils:
|
||||
git:
|
||||
url: https://github.com/cake-tech/blockchain_utils
|
||||
ref: cake-update-v4
|
||||
ref: cake-update-v2
|
||||
mobx: ^2.3.0+1
|
||||
bip39: ^1.0.6
|
||||
hive: ^2.2.3
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue