web3.py 腳本轉(zhuǎn)賬

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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末氏身,一起剝皮案震驚了整個濱河市巍棱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蛋欣,老刑警劉巖航徙,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異陷虎,居然都是意外死亡到踏,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門尚猿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來窝稿,“玉大人,你說我怎么就攤上這事凿掂“槔疲” “怎么了?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵庄萎,是天一觀的道長踪少。 經(jīng)常有香客問我,道長糠涛,這世上最難降的妖魔是什么援奢? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮忍捡,結(jié)果婚禮上集漾,老公的妹妹穿的比我還像新娘切黔。我一直安慰自己,他們只是感情好帆竹,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布绕娘。 她就那樣靜靜地躺著,像睡著了一般栽连。 火紅的嫁衣襯著肌膚如雪险领。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天秒紧,我揣著相機與錄音竟趾,去河邊找鬼夷陋。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的疆股。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼互亮,長吁一口氣:“原來是場噩夢啊……” “哼汉操!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鹰霍,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤闻鉴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茂洒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孟岛,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年督勺,在試婚紗的時候發(fā)現(xiàn)自己被綠了渠羞。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡智哀,死狀恐怖次询,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瓷叫,我是刑警寧澤屯吊,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站赞辩,受9級特大地震影響雌芽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辨嗽,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一世落、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦屉佳、人聲如沸谷朝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽圆凰。三九已至,卻和暖如春体箕,著一層夾襖步出監(jiān)牢的瞬間专钉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工累铅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留跃须,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓娃兽,卻偏偏與公主長得像菇民,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子投储,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354

推薦閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應該記得 GitHub 上有一個 Awesome - XXX 系列...
    小邁克閱讀 2,984評論 1 3
  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007閱讀 2,210評論 0 3
  • 點我查看本文集的說明及目錄第练。 本項目相關(guān)內(nèi)容包括: 實現(xiàn)過程: CH7 創(chuàng)建在線商店 CH8 管理支付和訂單 CH...
    學以致用123閱讀 3,572評論 0 6
  • 點我查看本文集的說明及目錄。 本項目相關(guān)內(nèi)容包括: 實現(xiàn)過程: CH7 創(chuàng)建在線商店 CH8 管理支付和訂單 CH...
    學以致用123閱讀 2,310評論 1 6
  • 深秋還是初冬玛荞,已不再去深究娇掏,道路的兩旁,是一排排整齊的樹木冲泥, 有核桃樹驹碍,銀杏樹壁涎,還有碧桃和紅地像火的楓葉凡恍,...
    JH素年錦時閱讀 145評論 0 2