Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

12.4. ERC20 代币合约

		
from web3 import Web3
web3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/CsS9shwaAab0z7B4LP2d"))	

contract = web3.eth.contract(address='0x000000000000000000000000000000000000dead', abi=...)

contract.functions.balanceOf(address).call()

contract.functions.transfer(to, value).call()
		
		

12.4.1. 签名发送ERC20代币

		
from web3 import Web3
from web3 import Web3, HTTPProvider, IPCProvider
# w3 = Web3(Web3.IPCProvider("~/Library/Ethereum/geth.ipc"))	

w3 = Web3(HTTPProvider("https://ropsten.infura.io/CsS9shwaAab0z7B4LP2d"))

fromAddress = '0xB94054c174995AE2A9E7fcf6c7924635FBa8ECF7'
toAddress = '0xCdF0253d8362d6c3334c8F28A6BFd74c90d03d92'
contractAddress='0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359'
private_key = b''


# '~/.ethereum/testnet/keystore/UTC--2018-03-02T12-12-51.966823000Z--b94054c174995ae2a9e7fcf6c7924635fba8ecf7'
with open('/Users/neo/Library/Ethereum/testnet/keystore/UTC--2018-03-02T12-12-51.966823000Z--b94054c174995ae2a9e7fcf6c7924635fba8ecf7') as keyfile:
    encrypted_key = keyfile.read()
    private_key = w3.eth.account.decrypt(encrypted_key, '')

print(private_key)

interface='[ { "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": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "burn", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "burnFrom", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "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" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" }, { "name": "_extraData", "type": "bytes" } ], "name": "approveAndCall", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [ { "name": "initialSupply", "type": "uint256", "index": 0, "typeShort": "uint", "bits": "256", "displayName": "initial Supply", "template": "elements_input_uint", "value": "100000" }, { "name": "tokenName", "type": "string", "index": 1, "typeShort": "string", "bits": "", "displayName": "token Name", "template": "elements_input_string", "value": "NEO" }, { "name": "tokenSymbol", "type": "string", "index": 2, "typeShort": "string", "bits": "", "displayName": "token Symbol", "template": "elements_input_string", "value": "#" } ], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Burn", "type": "event" } ]'
contract = w3.eth.contract(address=contractAddress, abi=interface)

nonce = w3.eth.getTransactionCount(fromAddress)
print(nonce)

txn = contract.functions.transfer(toAddress,5,).buildTransaction({
    'chainId': 3,
    'gas': 30000,
    'gasPrice': w3.toWei('1', 'gwei'),
    'nonce': nonce,
    })

print(txn)

signed_txn = w3.eth.account.signTransaction(txn, private_key=private_key)
print(signed_txn.hash)
print(signed_txn.rawTransaction)

tmp = w3.eth.sendRawTransaction(signed_txn.rawTransaction)  

txhash = w3.toHex(w3.sha3(signed_txn.rawTransaction))

print("https://ropsten.etherscan.io/tx/"+txhash)