Created
July 20, 2018 19:34
-
-
Save janfilips/365ee29f3c58e983a3e1a9857677a766 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import time | |
import json | |
import web3 | |
from web3.auto import w3 | |
from web3 import Web3, Account | |
from web3.contract import ConciseContract | |
import eth_account | |
from eth_account.messages import defunct_hash_message | |
from web3.providers.rpc import HTTPProvider | |
from web3.providers.tester import EthereumTesterProvider | |
from solc import compile_source | |
BID = 0.777 | |
BID = BID * 10 # since we cannot do decimals in solidity | |
WINNING_PRICE = 34 | |
CONTRACT_CONSTRUCTOR = WINNING_PRICE | |
SOL_FILENAME = "dollar-auction.sol" | |
CONTRACT_SOURCE_ABSOLUTE_PATH = '/Users/jpavuk/coding/blockchain-dollar-game/contracts/'+SOL_FILENAME | |
def compile_source_file(file_path): | |
with open(file_path, 'r') as f: | |
source = f.read() | |
#print('contract source code', source) | |
compiled_source = compile_source(source) | |
print('πΊ Contract was successfully compiled..', file_path) | |
return compiled_source | |
def wait_for_receipt(w3, tx_hash, poll_interval): | |
while True: | |
tx_receipt = w3.eth.getTransactionReceipt(tx_hash) | |
if tx_receipt: | |
return tx_receipt | |
print(tx_hash.hex(),'is still processing, trying again in',poll_interval,'seconds..') | |
time.sleep(poll_interval) | |
print('πΊ Obtained transaction receipt', tx_receipt) | |
return tx_receipt | |
def deploy_contract(w3, contract_interface): | |
contract_instance = w3.eth.contract( | |
abi=contract_interface['abi'], | |
bytecode=contract_interface['bin'], | |
) | |
print('constract instance is', contract_instance) | |
gas_limit = 2000000 | |
gas_price = w3.toWei('40', 'gwei') | |
#gas_price = w3.eth.gasPrice | |
nonce = w3.eth.getTransactionCount(account=account.address,block_identifier=w3.eth.defaultBlock) | |
print('returned nonce', nonce) | |
transaction = { | |
"from": w3.eth.defaultAccount, | |
'gas':gas_limit, | |
'gasPrice':gas_price, | |
'chainId':3, | |
'nonce':nonce, | |
} | |
print('Creating contract constructor..') | |
contract_constructor = contract_instance.constructor(CONTRACT_CONSTRUCTOR) | |
# building transaction | |
print('Building transaction...') | |
deploy_transaction = contract_constructor.buildTransaction( transaction ) | |
print('πΊ Contract transaction was built', deploy_transaction) | |
print('signinng') | |
signed_transaction = w3.eth.account.signTransaction(deploy_transaction, account.privateKey) | |
print('signed_transaction', signed_transaction) | |
print('sending raw tansaction') | |
txn_hash = w3.eth.sendRawTransaction(signed_transaction.rawTransaction) | |
print('sent signed transaction', txn_hash.hex()) | |
print('wait for contract creation receipt..') | |
tx_receipt = wait_for_receipt(w3, txn_hash, 5) | |
contractAddress = tx_receipt.contractAddress | |
print('πΊ Contract was succesfully deployed at', contractAddress) | |
return tx_receipt, contract_instance | |
# load wallet | |
w3 = Web3(HTTPProvider('https://ropsten.infura.io/xSKHv68S1At0vV7kPPXL')) | |
wallet_json = {"address":"1c8a87926f5b727146d66100d144400ea274a47b","crypto":{"cipher":"aes-128-ctr","ciphertext":"df4470c3ca512bd985a5a8e99a4e46c626efeac5cad7f3e82ca6b33e07b9f8f9","cipherparams":{"iv":"318e34dff5e7baa10a206661b69055b6"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"a52caa91cf25dc04652a0029c9f2f2a589ddcfbd2bc7558b272a1b48b24be206"},"mac":"38fdac919a132d548661da31f75fcc2eac758b571bd9a4e1e29c9dc4683aa5b2"},"id":"07277414-a6aa-4750-88e6-7c5e5deefb24","version":3} | |
wallet_secret = "mrdka pica" | |
account_key = Account.decrypt(wallet_json, wallet_secret) | |
account = Account.privateKeyToAccount(account_key) | |
w3.eth.defaultAccount = account.address | |
print('π Account', w3.eth.defaultAccount) | |
print('π€© Account balanace',w3.eth.getBalance(account=account.address)) | |
# compile contract | |
compiled_sol = compile_source_file(CONTRACT_SOURCE_ABSOLUTE_PATH) | |
contract_id, contract_interface = compiled_sol.popitem() | |
# deploy contract | |
#contract_address, contract_instance = deploy_contract(w3, contract_interface) | |
#print("Deployed {0} to: {1}\n".format(contract_id, contract_address)) | |
# test our contract | |
#CONTRACT_ADDRESS = contract_address | |
CONTRACT_ADDRESS = "0xd72D12a3bD0821D44E6539898EE2a8C81d80f12e" | |
CONTRACT_ABI = """ | |
[ | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "place_bid", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "uint256" | |
} | |
], | |
"payable": true, | |
"stateMutability": "payable", | |
"type": "function" | |
}, | |
{ | |
"constant": true, | |
"inputs": [], | |
"name": "game_ends", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bytes32" | |
} | |
], | |
"payable": false, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "list_bids", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bool" | |
} | |
], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": true, | |
"inputs": [], | |
"name": "game_starts", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bytes32" | |
} | |
], | |
"payable": false, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "list_wins", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bool" | |
} | |
], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "list_all", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bool" | |
} | |
], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "__test_get_kaka", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bytes32" | |
} | |
], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": false, | |
"inputs": [], | |
"name": "list_refunds", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bool" | |
} | |
], | |
"payable": false, | |
"stateMutability": "nonpayable", | |
"type": "function" | |
}, | |
{ | |
"constant": true, | |
"inputs": [], | |
"name": "winning_price", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "uint256" | |
} | |
], | |
"payable": false, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"constant": true, | |
"inputs": [], | |
"name": "test", | |
"outputs": [ | |
{ | |
"name": "", | |
"type": "bytes32" | |
} | |
], | |
"payable": false, | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"name": "_winning_price", | |
"type": "uint256" | |
} | |
], | |
"payable": true, | |
"stateMutability": "payable", | |
"type": "constructor" | |
} | |
] | |
""" | |
contract_instance = w3.eth.contract( | |
address=CONTRACT_ADDRESS, | |
abi=CONTRACT_ABI, | |
) | |
print('contract_instance', contract_instance) | |
eth_account.local.LocalAccount = account.address | |
#transaction = contract_instance.functions.place_bid().buildTransaction() | |
#transaction['to'] = CONTRACT_ADDRESS | |
#transaction['from'] = account.address | |
#transaction['value'] = 111 | |
#transaction['gas'] = 2000000 | |
#transaction['gasPrice'] = w3.toWei('40', 'gwei') | |
#transaction['chainId'] = 3 | |
#transaction['nonce'] = w3.eth.getTransactionCount(account=account.address,block_identifier=w3.eth.defaultBlock) | |
#print('transaction', transaction) | |
#signed_transaction = account.signTransaction(transaction) | |
#print('π¦ signed_transaction.rawTransaction', signed_transaction.rawTransaction) | |
#money_sent = w3.eth.sendRawTransaction(signed_transaction.rawTransaction) | |
#print('π°π°π°π°π°π°π°π°π° money sent', w3.toHex(money_sent)) | |
#bid_return = contract_instance.functions.place_bid().transact(signed_transaction.rawTransaction) | |
#print('π©π©π©π©π©π© bid returned', bid_return) | |
print('π¦ contract_instance.functions.place_bid', dir(contract_instance.functions.place_bid())) | |
bid_return = contract_instance.functions.place_bid().call({'value':4444444444}) | |
print('π©π©π©π©π©π© bid returned but needs to be transaction function', bid_return) | |
bid_return = contract_instance.functions.place_bid().call() | |
print('π©π©π©π©π©π© bid returned', bid_return) | |
# XXX this is what I really want to do as per transaction function | |
# See the "transact" document section here http://web3py.readthedocs.io/en/latest/contracts.html | |
bid_return = contract_instance.functions.place_bid().transact({"value":4444444444,}) | |
print('π©π©π©π©π©π© bid returned', bid_return) | |
print('π€© Account balanace',w3.eth.getBalance(account=account.address)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment