Blockchain

智能合約 - 批准功能:wad

  • April 13, 2022

在智能合約中“批准”功能的上下文中https://cronos.org/explorer/address/0x66e428c3f67a68878562e79A0234c1F83c208770/contracts

function approve(address guy, uint wad) public stoppable returns (bool) 

什麼是“沃德”?顯然,從參考資料來看,它是一個 18 位數字。行。但它在 Gwei 和 Wei ..etc 中嗎?

謝謝

類似的問題,什麼是estimate_gas 返回?桂還是魏?

所以 web3 將“ wad ”解釋為“ 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 

參考:

https://www.thebalance.com/gwei-5194614

https://norman-lm-fung.medium.com/interact-with-cronos-single-usdc-lp-with-web3-py-1e14c62a0d9c

引用自:https://ethereum.stackexchange.com/questions/126085