Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions cw_evm/lib/evm_chain_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,43 @@ abstract class EVMChainWalletBase
8453 => CryptoCurrency.baseEth,
_ => CryptoCurrency.eth,
};
final nativeBal = balance[nativeCurrency]?.balance ?? BigInt.zero;
final requiredNative = valueWei + BigInt.from(gas.estimatedGasFee);

if (requiredNative > nativeBal) {
throw EVMChainTransactionFeesException(nativeCurrency.title);
}


bool _startsWith(String s, String p) => s.length >= p.length && s.substring(0, p.length).toLowerCase() == p.toLowerCase();

if (_startsWith(dataHex, '0xa9059cbb') && to.isNotEmpty) {
// Try find the token by contract address == `to`
Erc20Token? tokenObj;
for (final c in balance.keys) {
if (c is Erc20Token && c.contractAddress.toLowerCase() == to.toLowerCase()) {
tokenObj = c;
break;
}
}

if (tokenObj != null) {
final hex = dataHex.startsWith('0x') ? dataHex.substring(2) : dataHex;
if (hex.length >= 8 + 64 + 64) {
final amountHex = hex.substring(hex.length - 64);
final requiredToken = BigInt.parse(amountHex, radix: 16);
final tokenBal = balance[tokenObj]?.balance ?? BigInt.zero;

if (tokenBal < requiredToken) {
throw EVMChainTransactionCreationException(tokenObj);
}
}
}
}


// Fallback for nodes that fail estimate (non-zero)
final gasUnits = gas.estimatedGasUnits == 0 ? 65000 : gas.estimatedGasUnits;

// Sign raw (native) tx with callData
return _client.signTransaction(
privateKey: _evmChainPrivateKey,
toAddress: to,
Expand Down
Loading
Loading