Python 3,以太坊 – 如何发送ERC20令牌?

我有一些脚本使用从地址发送Ethers到地址.我使用Parity和Python 3.6.它使用Flask看起来像:

from flask import Flask, render_template, json, request
import urllib
import requests
import binascii
from decimal import *
app = Flask(__name__)

def Eth(method,params=[]):
    data = {"method":method,"params":params,"id":1,"jsonrpc":"2.0"}
    headers = {'Content-type': 'application/json'}
    r = requests.post(ethrpc, data=json.dumps(data), headers=headers)
    r = r.text
    response = json.loads(r)
    return(response)
hot = str("XXXXXXX")
@app.route('/')
def index():
    ethnumbers = int(10)**int(18)
    hot = str("XXXXX")
    balance = Eth("eth_getBalance",[hot,'latest'])
    balance = balance["result"]
    balance = int(balance, 16)
    balance = float(balance)
    balance = balance / ethnumbers
    balance = str(balance)
    return render_template('index.html',hot = hot,balance=balance)


@app.route('/send/',methods=['POST','GET'])
def send():
    getcontext().prec = 28
    ethnumbers = Decimal(10)**Decimal(18)
    print(ethnumbers)
    if request.method == "POST":    
        _myaddress = request.form['_myaddress']
        _youraddress = request.form['_youraddress']
        _amount = request.form['_amount']
        _gas = request.form['_gas']
        _gas = hex(int(_gas))
        passy = str("XXXXXXXXX")
        getcontext().prec = 28
        _amount = Decimal(_amount)
        getcontext().prec = 28
        _amount = _amount * ethnumbers
        getcontext().prec = 28
        _amount = int(_amount)
        _amount = hex(_amount)
        r = [{'from':_myaddress,"to":_youraddress,"value":_amount,"gas":_gas,},str("XXXXXXXXXX!")]
        print(r)
        json.dumps(r)
        resultio = Eth("personal_sendTransaction",r)
        try: 
            resultio["result"]
            return render_template('sent.html',resultio=resultio["result"])
        except: KeyError
        return render_template('sent.html',resultio=resultio["error"]["message"])

    else:
        return render_template('index.html')







if __name__ == "__main__":
    app.run()

我很确定,我必须使用“数据”来做到这一点,但我不知道如何通过这个脚本发送ERC20令牌.令牌交易的结构类似于“我的地址 – >令牌地址 – >令牌接收器”.

有任何想法吗?

解决方法:

web3.py肯定是要走的路.如果你想手动完成并且你只想调用标准的ERC-20传输方法,则from地址应该保持不变,to地址应该是令牌契约,然后数据应该是以下连接在一起的格式化为十六进制:

> keccak256哈希“transfer(address,uint256)”的前4个字节,这是函数的签名.
>收件人的地址,左零填充为32个字节.
>转账金额. (一定要考虑令牌的小数…… 1令牌通常是10 ** 18,但小数位数因令牌而异,可以通过调用小数()函数来检索.)这也应该是格式化为32字节数字(左零填充).

web3.py会容易得多. :-) 就像是:

web3.eth.contract(address, abi=standard_token_abi).sendTransaction({
    'from': from_address
}).transfer(to_address, amount)
上一篇:如何在PHP中验证以太坊地址


下一篇:获取ip地址