代码之家  ›  专栏  ›  技术社区  ›  Kamil

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

  •  5
  • Kamil  · 技术社区  · 6 年前

    我有一些脚本用于将以太从地址发送到地址。Im使用奇偶校验和Python 3.6。它使用的烧瓶看起来像:

    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令牌。令牌事务的结构类似于“我的地址->令牌地址->令牌接收方”。

    有什么想法吗?

    3 回复  |  直到 6 年前
        1
  •  5
  •   Kamil    5 年前

    伙计们,这比看起来要简单。 您只需输入:

    合同地址

    作为接收器并生成一个长的“数据”字段,它将字符串表示为:

    // method name, its constans in erc20
    
    0xa9059cbb
    
    //receiver address (you have to do it without "0x" because its needed only when
    //you have to tell "im using hexadecimal". You did it above, in method field.
    //so, receiver address:
    
    5b7b3b499fb69c40c365343cb0dc842fe8c23887
    
    // and fill it with zeros, it have to be lenght 64. So fill rest of address 
    
    0000000000000000000000005b7b3b499fb69c40c365343cb0dc842fe8c23887
    
    // then you need amount, please convert it to hexadecimal, delete "0x" and 
    // remember, you need to integer first, so if token has 18 "decimals" it need
    // to be amount / 10**18 first!!
    //1e27786570c272000 and fill that with zeros, like above:
    
    000000000000000000000000000000000000000000000001e27786570c272000
    
    //just add string, to string, to string, and you have data field: 
    
    0xa9059cbb0000000000000000000000005b7b3b499fb69c40c365343cb0dc842fe8c23887000000000000000000000000000000000000000000000001e27786570c272000
    

    交易示例: https://etherscan.io/tx/0x9c27df8af24e06edb819a8d7a380f548fad637de5edddd6155d15087d1619964

        2
  •  3
  •   user94559    6 年前

    web3.py 这绝对是一条路要走。如果您想手工操作,只需调用标准ERC-20 transfer 方法 from 地址应保持不变 to 地址应该是令牌合约,然后是 data 应将以下内容串联在一起并格式化为十六进制:

    1. “transfer(address,uint256)”的keccak256散列的前4个字节,它是函数的签名。
    2. 收件人的地址,左零填充为32字节。
    3. 要转移的金额。(一定要考虑令牌的小数点…1令牌通常是10**18,但小数点的位数因令牌而异,可以通过调用 decimals() 函数。)还应将其格式化为32字节的数字(因此保留零填充)。

    web3.py 会容易得多。:-)类似于:

    web3.eth.contract(address, abi=standard_token_abi).sendTransaction({
        'from': from_address
    }).transfer(to_address, amount)
    
        3
  •  -1
  •   jmunsch Colin Pickard    6 年前

    启动a geth 节点并向JSON-RPC接口发送post请求,如:

    data = {"method": "personal_sendTransaction",
            "params":[{
                       "from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
                       "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
                       "gas": "0x76c0", // 30400
                       "gasPrice": "0x9184e72a000", // 10000000000000
                       "value": "0x9184e72a", // 2441406250
                       "data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],
            "id":1,
            "jsonrpc":"2.0"
            }
    headers = {'Content-type': 'application/json'}
    r = requests.post('127.0.0.1:8545', data=json.dumps(data), headers=headers)
    

    就令牌契约而言,您需要找到契约部署的地址,查看ABI,并确定调用 transfer 令牌部署的方法。