Replies: 1 comment
ERC-20 トークンを transfer する
import json
import os
from web3 import HTTPProvider, Web3
w3 = Web3(HTTPProvider(os.environ["WEB3_PROVIDER_URI"]))
account = w3.eth.account.from_key(os.environ["PRIVATE_KEY"])
# WETH
# https://etherscan.io/address/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2#code
with open("./abi.json") as fp:
abi = json.load(fp)
contract = w3.eth.contract("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", abi=abi)
# ゼロアドレスだと見積もりが低くなるのでイチアドレスで見積もる。
gas = contract.functions.transfer(
"0x0000000000000000000000000000000000000001", 1
).estimate_gas()
transaction = contract.functions.transfer(
"0x0000000000000000000000000000000000000000", 1
).build_transaction(
{"nonce": w3.eth.get_transaction_count(account.address), "gas": gas}
)
signed = w3.eth.account.sign_transaction(transaction, account.key)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
tx = w3.eth.get_transaction(tx_hash)
w3.eth.wait_for_transaction_receipt(tx_hash) |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Today I Learned ...
All reactions