def build_contract(self): web3 = self.get_web3() contract_name = self.CONTRACT_NAME if self.type == Game.TYPE_30_DAYS: contract_name = self.BONUS_CONTRACT_NAME elif self.type == Game.TOKEN_GAME: contract_name = self.TOKEN_CONTRACT_NAME contract = web3.eth.contract( abi=ContractHelper.get_abi(contract_name), bytecode=ContractHelper.get_bytecode(contract_name), contract_name=contract_name) """ :rtype contract: Contract """ AccountHelper.unlock_base_account() def get_contract_address(event_log): """ :param event_log: :type dict: :return: """ if self.smart_contract_id in (None, '0'): self.smart_contract_id = event_log['address'] if self.status == self.STATUS_NEW: self.status = self.STATUS_PUBLISHED self.save() self.add_bet_tracking() transfer_filter = contract.on('GameStarted') transfer_filter.watch(get_contract_address) # TODO move prices to config self.transaction_id = contract.deploy( transaction={ 'from': AccountHelper.get_base_account(), "gasPrice": ContractHelper.getGasPrice() }, args=[ web3.toWei(self.bet_amount, 'ether'), ContractHelper.getCalculatorContractAddress() ])
def get_smart_contract(self): """ :rtype: Contract """ web3 = self.get_web3() contract = web3.eth.contract(contract_name=self.CONTRACT_NAME, abi=ContractHelper.get_abi( self.CONTRACT_NAME), address=self.smart_contract_id) return contract
def finish(self): if self.smart_contract_id in (None, '0'): raise AttributeError('Invalid smart contract address') if self.status != Game.STATUS_PUBLISHED: raise RuntimeError('Invalid status') num_players = self.get_stat().get('numPlayers', 0) if num_players < 5: self.ending_at += timezone.timedelta(hours=24) self.save() push_message = push.GameUpdatedPushMessage(payload=self) PushHelper.inform_all_devices(push_message) return None self.status = Game.STATUS_FINISHING self.ending_at += timezone.timedelta(hours=1) keep_trying = True while keep_trying: try: AccountHelper.unlock_base_account() keep_trying = False except socket.timeout: keep_trying = True keep_trying = True while keep_trying: try: contract = self.get_smart_contract() tx = contract.transact( transaction={ 'from': AccountHelper.get_base_account(), 'gasPrice': ContractHelper.getGasPrice() }).finish() keep_trying = False except socket.timeout: keep_trying = True self.save() push_message = push.GameUnpublishedPushMessage(payload=self) PushHelper.inform_all_devices(push_message) return tx
def get_smart_contract(self): """ :rtype: Contract """ contract_name = self.CONTRACT_NAME if self.type == Game.TYPE_30_DAYS: contract_name = self.BONUS_CONTRACT_NAME elif self.type == Game.TOKEN_GAME: contract_name = self.TOKEN_CONTRACT_NAME web3 = self.get_web3() contract = web3.eth.contract(contract_name=contract_name, abi=ContractHelper.get_abi(contract_name), address=self.smart_contract_id) return contract
def revoke(self): if self.smart_contract_id in (None, '0'): raise AttributeError('Invalid smart contract address') if self.status != Game.STATUS_PUBLISHED: raise RuntimeError('Invalid status') self.status = Game.STATUS_CANCELED contract = self.get_smart_contract() AccountHelper.unlock_base_account() tx = contract.transact( transaction={ 'from': AccountHelper.get_base_account(), 'gasPrice': ContractHelper.getGasPrice() }).revoke() self.save() return tx