So web3 interpret "wad" as "wei".
from web3 import Web3
my_wallet_address = "xxx"
wallet_address = Web3.toChecksumAddress(my_wallet_address)
private_key = "xxx_tellme_xxx"
EIP20_ABI = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"} ... blah ...]'
wcro_address = Web3.toChecksumAddress("0x5C7F8A570d578ED84E63fdFA7b1eE72dEae1AE23")
wcro_contract = w3.eth.contract(address=wcro_address, abi=EIP20_ABI)
ONE_BILLION : int = 1000000000
approval_amount_in_usdt : int = 100
cro_usdt = 0.42 # rate on 20220413
approval_amount_in_cro : int = int(approval_amount_in_usdt * cro_usdt)
default_gas_limit = 800_000
gas_price = w3.eth.gasPrice
'''
If you're interacting with CRONOS chain,
allowance_amount_in_wei = approval_amount_in_cro * ONE_BILLION * ONE_BILLION
If you're interacting with Etherum chain,
allowance_amount_in_wei = approval_amount_in_eth * ONE_BILLION * ONE_BILLION
'''
allowance_amount_in_wei = approval_amount_in_cro * ONE_BILLION * ONE_BILLION
approval_transaction = wcro_contract.functions.approve( # If you want to swap USDT to SINGLE, change here.
router_address,
allowance_amount_in_wei,
)
nonce = w3.eth.getTransactionCount(wallet_address)
approval_trx_params = approval_transaction.buildTransaction({
'from': wallet_address,
'gas': default_gas_limit, # here we use the default gas limit. See the swap trx below for estimated gas
'gasPrice': gas_price,
'nonce': nonce,
})
signed = w3.eth.account.sign_transaction(approval_trx_params, private_key)
approval_trx_hash = w3.eth.sendRawTransaction(signed.rawTransaction)
receipt = w3.eth.waitForTransactionReceipt(approval_trx_hash)
assert receipt["status"] == 1 # trx success
assert wcro_contract.functions.allowance(wallet_address, router_address).call() == allowance_amount_in_wei
References:
https://www.thebalance.com/gwei-5194614
https://norman-lm-fung.medium.com/interact-with-cronos-single-usdc-lp-with-web3-py-1e14c62a0d9c