美文网首页以太坊
web3.py 脚本转账

web3.py 脚本转账

作者: 空即是色即是色即是空 | 来源:发表于2018-09-23 22:38 被阅读250次
from web3.auto import w3
from web3.personal import Personal
from web3.contract import Contract, ContractFunctions
from web3 import Web3


false = False
true = True
CONTRACT_ADDRESS = "0x000000000000000000000"
CONTRACT_ABI = [
    {
        "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": 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": false,
        "inputs": [
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "burn",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "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": false,
        "inputs": [
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transfer",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "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": "_owner",
                "type": "address"
            },
            {
                "indexed": true,
                "name": "_spender",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "Approval",
        "type": "event"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "name": "from",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "value",
                "type": "uint256"
            }
        ],
        "name": "Burn",
        "type": "event"
    },
    {
        "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"
    },
    {
        "inputs": [
            {
                "name": "initialSupply",
                "type": "uint256"
            },
            {
                "name": "tokenName",
                "type": "string"
            },
            {
                "name": "tokenSymbol",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            },
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "allowance",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "balanceOf",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "decimals",
        "outputs": [
            {
                "name": "",
                "type": "uint8"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "name",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "symbol",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "totalSupply",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]

GAS_PRICE = w3.eth.gasPrice
GAS_LIMIT = 100000

FROM_ADDRESS = "0x1111111111111111111111111111"
FROM_ADDRESS_PWD = "101"


def get_bonus_list(bonus_file):
    print("Read contents from %s" % bonus_file)
    contents = []
    result = []
    with open(bonus_file, "r") as fh:
        contents = fh.readlines()
    for line in contents:
        if len(line.split(",")) == 3 and line.split(",")[1].startswith("0x") and line.split(",")[2].strip():
            result.append((line.split(",")[1].strip(), line.split(",")[2].strip()))
    return result

def prepare():
    # Set account as the sender
    ps = Personal(w3)
    ps.unlockAccount(FROM_ADDRESS, FROM_ADDRESS_PWD)
    w3.eth.defaultAccount = FROM_ADDRESS

def init_contract(address=CONTRACT_ADDRESS, abi=CONTRACT_ABI):
    myContract = w3.eth.contract(address=Web3.toChecksumAddress(address), abi=abi)
    return myContract

def transfer(contract, address, amount, gasprice=GAS_PRICE, gasLimit=GAS_LIMIT):
    transaction = {"gasPrice": gasprice, "gas": gasLimit}
    contract.functions.transfer(address, w3.toWei(amount, 'ether')).transact(transaction)


if __name__ == "__main__":
    print("Start")
    prepare()
    contract = init_contract()
    for address, xdc in get_bonus_list("bonus.csv"):
        print("Transfer XDC from %s to %s with %s" % (FROM_ADDRESS, address, xdc))
        transfer(contract, address, xdc)

相关文章

  • web3.py 脚本转账

    python3环境 web3.py包https://web3py.readthedocs.io/en/stable...

  • web3js脚本编写

    转账脚本 编写转账的js脚本,保存在transactionTest.js文件中 在shell中敲如下命令 转币合约...

  • 比特币的标准交易

    引用被花费的UTXO (“交易”与“输出索引”) 输入脚本 - 公钥与签名(”解锁脚本“) 转账数量 (“总量”)...

  • 比特币的时间锁转账

    阅读本文前,建议先了解比特币转账、输入、输出、P2SH脚本等基本概念。如不了解,可先阅读我的另一篇文章《比特币转账...

  • Web3.py

    打开rpc启动区块链

  • 2018-07-20撸币转账

    今天好好学习了一下web3.py的文档https://web3py.readthedocs.io/en/lates...

  • 转账

    银行取钱的人很多,坐着的、排队的、玩手机的……排在我面前的是一对父子,儿子衣服整齐干净,父亲却头发蓬乱,满脸分不清...

  • 转账

    据说近期是牛市,会一直延续到月底。可是我不信,我觉得没有那么简单。不过如果能赚一把,也不错。还是尝试一下吧...

  • 转账

    “喂。李姐,麻烦您借我五万块钱急用。下星期一就可以还给你。” “哎呀。手上没钱。我现在跟朋友吃饭,要不,我问问他们...

  • 转账

    又到周末,天空万里无云,阳光灿烂。平常使用的工行卡里没钱了,我决定从招商银行卡里转点钱进去。 由于这张招行卡之前出...

网友评论

    本文标题:web3.py 脚本转账

    本文链接:https://www.haomeiwen.com/subject/nznqoftx.html