0%

eth | web3.py 转账 erc20 代币

在这里还是以 hsc 公链为例。


参考资料



代码


  • web3 5.19.0

下面代码是向

  • 0xE8e069C47A45050DBA9D566CEfFA7bc7D453F0c5

地址进行归集。其中归集的合约地址是

  • 0x09e6030537f0582d51C00bdC2d590031d9b1c86c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from web3 import Web3
import json
import xlrd
from collections import defaultdict
import time

w3 = Web3(Web3.HTTPProvider('https://http-mainnet.hoosmartchain.com'))

file_name = "3000_2.xlsx"


ixt_contract = "0x80c6A3A493aFd7C52f89E6504C90cE6A639783FC"
iusdt_contract = "0x09e6030537f0582d51C00bdC2d590031d9b1c86c"

abi = json.loads(
'[{"constant": true,"inputs": [{"name": "who", "type": "address"}],"name": "balanceOf","outputs": [{"name": "", "type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"},'
'{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]')

iXT_ADDRESS = w3.toChecksumAddress('0x80c6A3A493aFd7C52f89E6504C90cE6A639783FC')
iUSDT_ADDRESS = w3.toChecksumAddress('0x09e6030537f0582d51C00bdC2d590031d9b1c86c')
iXT_CONTRACT = w3.eth.contract(address=iXT_ADDRESS, abi=abi)
iUSDT_CONTRACT = w3.eth.contract(address=iUSDT_ADDRESS, abi=abi)


def init_address():
data = defaultdict(list)
xls = xlrd.open_workbook(file_name)
for i in range(len(xls.sheets()[0].col_values(0))):
data[xls.sheets()[0].col_values(0)[i]] = xls.sheets()[0].col_values(1)[i]
return data


def get_ixt(address):
address = Web3.toChecksumAddress(address)
return w3.fromWei(iXT_CONTRACT.functions.balanceOf(address).call(), 'ether')


def get_iusdt(address):
address = Web3.toChecksumAddress(address)
return w3.fromWei(iUSDT_CONTRACT.functions.balanceOf(address).call(), 'ether')


def send_iusdt(address, private):
toAddress = Web3.toChecksumAddress('0xE8e069C47A45050DBA9D566CEfFA7bc7D453F0c5')
fromAddress = Web3.toChecksumAddress(address)
iusdt = get_iusdt(fromAddress)
if iusdt > 0:
print(iusdt)
nonce = w3.eth.getTransactionCount(fromAddress)
transaction = iUSDT_CONTRACT.functions.transfer(toAddress, w3.toWei(iusdt, "ether")).buildTransaction({
'gas': 241838,
'gasPrice': w3.toWei('1', 'gwei'),
'nonce': nonce,
})
signed_tx = w3.eth.account.signTransaction(transaction, private)
txn_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
print('txn_hash:', Web3.toHex(txn_hash))


if __name__ == '__main__':
data = init_address()
for address, private in data.items():
send_iusdt(address, private)
请我喝杯咖啡吧~