def initialize_datatrust(self): """ Confirm or create role as datatrust backend in protocol """ log.info('Getting current datatrust address from network') self.datatrust = Datatrust(self.datatrust_wallet) self.datatrust.at(self.w3, self.datatrust_contract) self.voting = Voting(self.datatrust_wallet) self.voting.at(self.w3, self.voting_contract) backend = call(self.datatrust.get_backend_address()) datatrust_hash = self.w3.sha3(text=self.datatrust_host) if backend == self.datatrust_wallet: log.info('This server is the datatrust host. Resolving registration') resolve = send( self.w3, self.datatrust_key, self.datatrust.resolve_registration( self.w3.sha3(text=self.datatrust_host) ) ) log.info(f'Resolved, transaction id: {resolve}') else: # backend not set, or is set to a different host datatrust_url = call(self.datatrust.get_backend_url()) is_candidate = call(self.voting.is_candidate(datatrust_hash)) candidate_is = call(self.voting.candidate_is(datatrust_hash, constants.PROTOCOL_REGISTRATION)) if datatrust_url == self.datatrust_host: log.info('Server has been registered as datatrust, but not voted in') elif is_candidate and candidate_is: log.info('This datatrust is a candidate but has not been voted in') poll_status = call(self.voting.poll_closed(datatrust_hash)) if poll_status: log.info('This datatrust was a candidate, but was not voted in before the poll closed') resolve = send( self.w3, self.datatrust_key, self.datatrust.resolve_registration(datatrust_hash) ) log.info(f'Resolved any prior registration, transaction id: {resolve.hex()}') self.wait_for_mining(resolve) register = self.register_host() log.info(f'Datatrust has been registered.') else: log.info('This datatrust is a candidate. Voting polls are still open.') else: log.info('No backend or different host set. Resolving prior registrations and Submitting this one for voting') resolve = send( self.w3, self.datatrust_key, self.datatrust.resolve_registration(datatrust_hash) ) log.info(f'Resolved any prior registration, transaction id: {resolve.hex()}') self.wait_for_mining(resolve) register = self.register_host()
def deploy_ether_token(w3): abi = None bc = None with open('ethertoken/ethertoken.abi') as f: abi = json.loads(f.read()) with open('ethertoken/ethertoken.bin') as f: bc = f.read() if abi is None: print(colored('Error reading Ether Token ABI', 'red')) sys.exit(1) if bc is None: print(colored('Error reading Ether Token Bytecode', 'red')) sys.exit(1) deployed = w3.eth.contract(abi=abi, bytecode=bc.rstrip('\n')) # computable.py sets the nonce # curr_nonce = w3.eth.getTransactionCount(w3.eth.defaultAccount) # build the args for the send method args = get_deployment_args(w3, deployed.constructor()) # use computable.py's ability to sign offline tx_hash = send(w3, PRIVATE_KEY, args) # wait for it... TODO adjust timeout tx_rct = w3.eth.waitForTransactionReceipt(tx_hash) return tx_rct['contractAddress']
def deploy_parameterizer(w3, market_token_address, voting_address): parameterizer_opts = get_parameterizer_opts() abi = None bc = None with open('parameterizer/parameterizer.abi') as f: abi = json.loads(f.read()) with open('parameterizer/parameterizer.bin') as f: bc = f.read() if abi is None: print(colored('Error reading Parameterizer ABI', 'red')) sys.exit(1) if bc is None: print(colored('Error reading Parameterizer Bytecode', 'red')) sys.exit(1) deployed = w3.eth.contract(abi=abi, bytecode=bc.rstrip('\n')) args = get_deployment_args( w3, deployed.constructor( market_token_address, voting_address, parameterizer_opts['price_floor'], parameterizer_opts['spread'], parameterizer_opts['list_reward'], parameterizer_opts['stake'], parameterizer_opts['vote_by'], parameterizer_opts['plurality'], parameterizer_opts['backend_payment'], parameterizer_opts['maker_payment'], parameterizer_opts['cost_per_byte'])) tx_hash = send(w3, PRIVATE_KEY, args) tx_rcpt = w3.eth.waitForTransactionReceipt(tx_hash) return tx_rcpt['contractAddress']
def test_build_sign_send_transaction(w3, pk, user, ether_token): """ transactions may be broken apart into each individual step in order to facilitate various signing methods (like crypto sticks for instance) """ lock(w3, user) # assure the account is locked other_user = w3.eth.accounts[1] # we'll send some tokens to this target other_user_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_bal == 0 # Let's deposit 1 ether token in user's account tup = ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}) tx = tx_helpers.send(w3, pk.to_bytes(), tup) rct = w3.eth.waitForTransactionReceipt(tx) user_bal = tx_helpers.call(ether_token.balance_of(user)) assert user_bal == Web3.toWei(1, 'gwei') # all computable.py HOC methods return a tuple in the form: (tx, opts) tup = ether_token.transfer(other_user, Web3.toWei(1, 'gwei'), {'from': user}) # these may be passed directly to build... built_tx = tx_helpers.build_transaction(w3, tup) assert built_tx['from'] == user # sign it signed_tx = tx_helpers.sign_transaction(w3, pk.to_bytes(), built_tx) assert signed_tx.hash is not None assert signed_tx.r is not None assert signed_tx.s is not None assert signed_tx.v is not None # a signed transaction can then be broadcast final_tx = tx_helpers.send_raw_transaction(w3, signed_tx) rct = w3.eth.waitForTransactionReceipt(final_tx) other_user_new_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_new_bal == Web3.toWei(1, 'gwei')
def transact(self, args): """ once the computable HOC args tuple is assembled this method is the same everywhere """ private_key = os.environ.get('private_key') if private_key: set_gas_prices(self.w3, args) tx = send(self.w3, private_key, args) return self.w3.toHex(tx)
def test_send_some_ether_token(w3, pk, user, ether_token): user_eth_bal = w3.eth.getBalance(user) assert user_eth_bal > 0 user_bal = tx_helpers.call(ether_token.balance_of(user)) assert user_bal == 0 # Let's deposit 1 ether token in user's account tup = ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}) tx = tx_helpers.send(w3, pk.to_bytes(), tup) rct = w3.eth.waitForTransactionReceipt(tx) new_user_bal = tx_helpers.call(ether_token.balance_of(user)) assert new_user_bal == Web3.toWei(1, 'gwei') # Let's send this to the default account tup = ether_token.transfer(w3.eth.defaultAccount, Web3.toWei(1, 'gwei'), {'from': user}) tx2 = tx_helpers.send(w3, pk.to_bytes(), tup) final_user_bal = tx_helpers.call(ether_token.balance_of(user)) ##assert new_user_bal == Web3.toWei(1, 'gwei') assert final_user_bal == 0
def test_send(w3, pk, user, ether_token): """ we do provide an all-in-one convenience method for offline signing """ other_user = w3.eth.accounts[2] # we'll send some tokens to this target other_user_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_bal == 0 # Let's deposit 1 ether token in user's account tup = ether_token.deposit(Web3.toWei(1, 'gwei'), {'from': user}) tx = tx_helpers.send(w3, pk.to_bytes(), tup) rct = w3.eth.waitForTransactionReceipt(tx) user_bal = tx_helpers.call(ether_token.balance_of(user)) assert user_bal == Web3.toWei(1, 'gwei') tx = tx_helpers.send( w3, pk.to_bytes(), ether_token.transfer(other_user, Web3.toWei(1, 'gwei'), {'from': user})) rct = w3.eth.waitForTransactionReceipt(tx) other_user_new_bal = tx_helpers.call(ether_token.balance_of(other_user)) assert other_user_new_bal == Web3.toWei(1, 'gwei')
def send_data_hash(self, listing, data_hash): """ On a successful post to the API db, send the data hash to protocol """ datatrust_hash = self.w3.sha3(text=self.datatrust_host) is_candidate = call(self.voting.is_candidate(datatrust_hash)) candidate_is = call(self.voting.candidate_is(datatrust_hash, constants.PROTOCOL_APPLICATION)) if is_candidate and candidate_is: receipt = send(self.w3, self.datatrust_key, self.datatrust.set_data_hash(listing, data_hash)) return receipt else: log.critical('This server is not the datatrust, unable to send data hash') raise ValueError('Server is not the datatrust, unable to send data hash')
def register_host(self): """ Register a host as the datatrust in protocol """ log.info('****Registering host****') register = send(self.w3, self.datatrust_key, self.datatrust.register(self.datatrust_host)) self.wait_for_mining(register) voting = Voting(self.datatrust_wallet) voting.at(self.w3, self.voting_contract) datatrust_hash = self.w3.sha3(text=self.datatrust_host) is_registered = call(voting.is_candidate(datatrust_hash)) if is_registered: log.info(f'Backend registered. Voting is now open.') else: log.error('Host attempted to register but did not succeed')
def deploy_voting(w3): abi = None bc = None with open('voting/voting.abi') as f: abi = json.loads(f.read()) with open('voting/voting.bin') as f: bc = f.read() if abi is None: print(colored('Error reading Voting ABI', 'red')) sys.exit(1) if bc is None: print(colored('Error reading Voting Bytecode', 'red')) sys.exit(1) deployed = w3.eth.contract(abi=abi, bytecode=bc.rstrip('\n')) args = get_deployment_args(w3, deployed.constructor(MARKET_TOKEN_ADDRESS)) tx_hash = send(w3, PRIVATE_KEY, args) tx_rct = w3.eth.waitForTransactionReceipt(tx_hash) return tx_rct['contractAddress']
def deploy_market_token(w3): abi = None bc = None with open('markettoken/markettoken.abi') as f: abi = json.loads(f.read()) with open('markettoken/markettoken.bin') as f: bc = f.read() if abi is None: print(colored('Error reading Market Token ABI', 'red')) sys.exit(1) if bc is None: print(colored('Error reading Ether Token Bytecode', 'red')) sys.exit(1) deployed = w3.eth.contract(abi=abi, bytecode=bc.rstrip('\n')) args = get_deployment_args(w3, deployed.constructor(PUBLIC_KEY, 0)) tx_hash = send(w3, PRIVATE_KEY, args) tx_rct = w3.eth.waitForTransactionReceipt(tx_hash) return tx_rct['contractAddress']
def deploy_datatrust(w3): abi = None bc = None with open('datatrust/datatrust.abi') as f: abi = json.loads(f.read()) with open('datatrust/datatrust.bin') as f: bc = f.read() if abi is None: print(colored('Error reading Datatrust ABI', 'red')) sys.exit(1) if bc is None: print(colored('Error reading Datatrust Bytecode', 'red')) sys.exit(1) deployed = w3.eth.contract(abi=abi, bytecode=bc.rstrip('\n')) args = get_deployment_args( w3, deployed.constructor(ETHER_TOKEN_ADDRESS, VOTING_ADDRESS, PARAMETERIZER_ADDRESS, RESERVE_ADDRESS)) tx_hash = send(w3, PRIVATE_KEY, args) tx_rct = w3.eth.waitForTransactionReceipt(tx_hash) return tx_rct['contractAddress']
def deploy_datatrust(w3, ether_token_address, voting_address, parameterizer_address, reserve_address): abi = None bc = None with open('datatrust/datatrust.abi') as f: abi = json.loads(f.read()) with open('datatrust/datatrust.bin') as f: bc = f.read() if abi is None: print(colored('Error reading Datatrust ABI', 'red')) sys.exit(1) if bc is None: print(colored('Error reading Datatrust Bytecode', 'red')) sys.exit(1) deployed = w3.eth.contract(abi=abi, bytecode=bc.rstrip('\n')) args = get_deployment_args( w3, deployed.constructor(ether_token_address, voting_address, parameterizer_address, reserve_address)) tx_hash = send(w3, PRIVATE_KEY, args) tx_rct = w3.eth.waitForTransactionReceipt(tx_hash) return tx_rct['contractAddress']
print(colored(heading('Private key check'), 'yellow')) if not PRIVATE_KEY: sys.exit(1) print('Private key available. Continuing...') print(colored(heading('Market Token Address check'), 'yellow')) if not MARKET_TOKEN_ADDRESS: sys.exit(1) print('Market Token Address available. Continuing...') print(colored(heading('Reserve Address check'), 'yellow')) if not RESERVE_ADDRESS: sys.exit(1) print('Reserve Address available. Continuing...') print(colored(heading('Listing Address check'), 'yellow')) if not LISTING_ADDRESS: sys.exit(1) print('Listing Address available. Continuing...') print(colored(heading('Set Market Token privileged'), 'yellow')) print(colored('instantiating MarketToken', 'blue')) market_token = MarketToken(PUBLIC_KEY) market_token.at(w3, MARKET_TOKEN_ADDRESS) print(colored('Setting addresses...', 'blue')) mt_gas = market_token.deployed.functions.setPrivileged(RESERVE_ADDRESS, LISTING_ADDRESS).estimateGas() mt_args = market_token.set_privileged(RESERVE_ADDRESS, LISTING_ADDRESS, {'gas': mt_gas, 'gas_price': w3.toWei(GAS_PRICE, 'gwei')}) mt_tx_hash = send(w3, PRIVATE_KEY, mt_args) mt_tx_rct = w3.eth.waitForTransactionReceipt(mt_tx_hash) print(colored(mt_tx_rct, 'green'))
print(colored(heading('Private key check'), 'yellow')) if not PRIVATE_KEY: sys.exit(1) print('Private key available. Continuing...') print(colored(heading('Parameterizer Address check'), 'yellow')) if not PARAMETERIZER_ADDRESS: sys.exit(1) print('Parameterizer Address available. Continuing...') print(colored(heading('Datatrust Address check'), 'yellow')) if not DATATRUST_ADDRESS: sys.exit(1) print('Datatrust Address available. Continuing...') print(colored(heading('Listing Address check'), 'yellow')) if not LISTING_ADDRESS: sys.exit(1) print('Listing Address available. Continuing...') print(colored(heading('Set Voting privileged'), 'yellow')) print(colored('instantiating Voting', 'blue')) voting = Voting(PUBLIC_KEY) voting.at(w3, VOTING_ADDRESS) print(colored('Setting addresses...', 'blue')) v_gas = voting.deployed.functions.setPrivileged(PARAMETERIZER_ADDRESS, DATATRUST_ADDRESS, LISTING_ADDRESS).estimateGas() v_args = voting.set_privileged(PARAMETERIZER_ADDRESS, DATATRUST_ADDRESS, LISTING_ADDRESS, {'gas': v_gas, 'gas_price': w3.toWei(GAS_PRICE, 'gwei')}) v_tx_hash = send(w3, PRIVATE_KEY, v_args) v_tx_rct = w3.eth.waitForTransactionReceipt(v_tx_hash) print(colored(v_tx_rct, 'green'))
datatrust_address) print(colored(listing_address, 'green')) print(colored(heading('Set Market Token privileged'), 'yellow')) print(colored('instantiating MarketToken', 'blue')) market_token = MarketToken(PUBLIC_KEY) market_token.at(w3, market_token_address) print(colored('Setting addresses...', 'blue')) mt_gas = market_token.deployed.functions.setPrivileged( reserve_address, listing_address).estimateGas() mt_args = market_token.set_privileged( reserve_address, listing_address, { 'gas': mt_gas, 'gas_price': w3.toWei(GAS_PRICE, 'gwei') }) mt_tx_hash = send(w3, PRIVATE_KEY, mt_args) mt_tx_rct = w3.eth.waitForTransactionReceipt(mt_tx_hash) print(colored(mt_tx_rct, 'green')) print(colored(heading('Set Voting privileged'), 'yellow')) print(colored('instantiating Voting', 'blue')) voting = Voting(PUBLIC_KEY) voting.at(w3, voting_address) print(colored('Setting addresses...', 'blue')) v_gas = voting.deployed.functions.setPrivileged( parameterizer_address, datatrust_address, listing_address).estimateGas() v_args = voting.set_privileged( parameterizer_address, datatrust_address, listing_address, { 'gas': v_gas, 'gas_price': w3.toWei(GAS_PRICE, 'gwei')
print(colored(heading('Private key check'), 'yellow')) if not PRIVATE_KEY: sys.exit(1) print('Private key available. Continuing...') print(colored(heading('Datatrust Address check'), 'yellow')) if not DATATRUST_ADDRESS: sys.exit(1) print('Datatrust Address available. Continuing...') print(colored(heading('Listing Address check'), 'yellow')) if not LISTING_ADDRESS: sys.exit(1) print('Listing Address available. Continuing...') print(colored(heading('Set Datatrust privileged'), 'yellow')) print(colored('instantiating Datatrust', 'blue')) datatrust = Datatrust(PUBLIC_KEY) datatrust.at(w3, DATATRUST_ADDRESS) print(colored('Setting addresses...', 'blue')) d_gas = datatrust.deployed.functions.setPrivileged( LISTING_ADDRESS).estimateGas() d_args = datatrust.set_privileged(LISTING_ADDRESS, { 'gas': d_gas, 'gas_price': w3.toWei(GAS_PRICE, 'gwei') }) d_tx_hash = send(w3, PRIVATE_KEY, d_args) d_tx_rct = w3.eth.waitForTransactionReceipt(d_tx_hash) print(colored(d_tx_rct, 'green'))