fix eth token abi endpoint and some logic clean up

This commit is contained in:
julian 2024-05-07 09:49:12 -06:00
parent d747347414
commit 0ac8885aa8
3 changed files with 50 additions and 94 deletions

View file

@ -686,7 +686,7 @@ abstract class EthereumAPI {
try {
final response = await client.get(
url: Uri.parse(
"$stackBaseServer/abis?addrs=$contractAddress&verbose=true",
"$stackBaseServer/abis?addrs=$contractAddress",
),
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()

View file

@ -92,16 +92,8 @@ class EthTokenWallet extends Wallet {
);
}
// String? mnemonicString = await ethWallet.getMnemonic();
//
// //Get private key for given mnemonic
// String privateKey = getPrivateKey(
// mnemonicString,
// (await ethWallet.getMnemonicPassphrase()),
// );
// _credentials = web3dart.EthPrivateKey.fromHex(privateKey);
try {
// try parse abi and extract transfer function
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
@ -109,90 +101,51 @@ class EthTokenWallet extends Wallet {
),
contractAddress,
);
} catch (_) {
rethrow;
}
try {
_sendFunction = _deployedContract.function('transfer');
} catch (_) {
//====================================================================
// final list = List<Map<String, dynamic>>.from(
// jsonDecode(tokenContract.abi!) as List);
// final functionNames = list.map((e) => e["name"] as String);
//
// if (!functionNames.contains("balanceOf")) {
// list.add(
// {
// "encoding": "0x70a08231",
// "inputs": [
// {"name": "account", "type": "address"}
// ],
// "name": "balanceOf",
// "outputs": [
// {"name": "val_0", "type": "uint256"}
// ],
// "signature": "balanceOf(address)",
// "type": "function"
// },
// );
// }
//
// if (!functionNames.contains("transfer")) {
// list.add(
// {
// "encoding": "0xa9059cbb",
// "inputs": [
// {"name": "dst", "type": "address"},
// {"name": "rawAmount", "type": "uint256"}
// ],
// "name": "transfer",
// "outputs": [
// {"name": "val_0", "type": "bool"}
// ],
// "signature": "transfer(address,uint256)",
// "type": "function"
// },
// );
// }
//--------------------------------------------------------------------
//====================================================================
// function not found so likely a proxy so we need to fetch the impl
//====================================================================
// final updatedToken = tokenContract.copyWith(abi: jsonEncode(list));
// // Store updated contract
// final id = await MainDB.instance.putEthContract(updatedToken);
// _tokenContract = updatedToken..id = id;
//--------------------------------------------------------------------
final contractAddressResponse =
await EthereumAPI.getProxyTokenImplementationAddress(
contractAddress.hex);
if (contractAddressResponse.value != null) {
_tokenContract = await _updateTokenABI(
forContract: tokenContract,
usingContractAddress: contractAddressResponse.value!,
);
} else {
throw contractAddressResponse.exception!;
}
//====================================================================
}
try {
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
// some failure so first try to make sure we have the latest abi
_tokenContract = await _updateTokenABI(
forContract: tokenContract,
usingContractAddress: contractAddress.hex,
);
} catch (_) {
rethrow;
}
_sendFunction = _deployedContract.function('transfer');
try {
// try again to parse abi and extract transfer function
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
);
_sendFunction = _deployedContract.function('transfer');
} catch (_) {
// if it fails again we check if there is a proxy token impl and
// then try one last time to update and parse the abi
final contractAddressResponse =
await EthereumAPI.getProxyTokenImplementationAddress(
contractAddress.hex);
if (contractAddressResponse.value != null) {
_tokenContract = await _updateTokenABI(
forContract: tokenContract,
usingContractAddress: contractAddressResponse.value!,
);
} else {
throw contractAddressResponse.exception!;
}
_deployedContract = web3dart.DeployedContract(
ContractAbiExtensions.fromJsonList(
jsonList: tokenContract.abi!,
name: tokenContract.name,
),
contractAddress,
);
_sendFunction = _deployedContract.function('transfer');
}
}
} catch (e, s) {
Logging.instance.log(
"$runtimeType wallet failed init(): $e\n$s",

View file

@ -22,10 +22,10 @@ import 'package:stackwallet/wallets/isar/providers/wallet_info_provider.dart';
class WalletInfoRowBalance extends ConsumerWidget {
const WalletInfoRowBalance({
Key? key,
super.key,
required this.walletId,
this.contractAddress,
}) : super(key: key);
});
final String walletId;
final String? contractAddress;
@ -45,8 +45,11 @@ class WalletInfoRowBalance extends ConsumerWidget {
} else {
contract = MainDB.instance.getEthContractSync(contractAddress!)!;
totalBalance = ref
.watch(pTokenBalance(
(contractAddress: contractAddress!, walletId: walletId)))
.watch(
pTokenBalance(
(walletId: walletId, contractAddress: contractAddress!),
),
)
.total;
}