def __init__(self, *args, **kwargs): super(ChargerAgent, self).__init__(*args, **kwargs) self._entity = Entity.from_hex(sys.argv[5]) self._address = Address(self._entity)
except Exception as e: sys.exit(e) # Load the contract and perform some basic checks on it try: contract_text = open(contract_name, 'r').read() except IOError as e: sys.exit('File not found') if 0 == len(contract_text): print("Contract is zero length.") sys.exit("Invalid contract") # Private key of the address to deploy from # WARNING: Unencrypted private keys should not be present in production code entity = Entity(b'... your private key here ...') address = Address(entity) print("Deploying contract:", contract_name, '\n') print(" Owner:", address) print(" Length:", len(contract_text), "byte(s)") # Perform the deployment now try: contract = Contract(contract_text, entity) gas_fee = 600000 api.sync(contract.create(api, entity, gas_fee), None, 0) except Exception as e: sys.exit(e) # Deployed, so we can now announce address and owner
def add_entity(self, name, entity=None, top_up=10000): if entity is None: entity = Entity() self.entities[name] = entity self.api.sync(self.api.tokens.wealth(entity, top_up))
def setUp(self) -> None: self.entity = Entity() self.address = Address(self.entity) self.board = [Entity() for _ in range(4)]
def _generate_private_key(self) -> Entity: entity = Entity() return entity
def on_message(self, msg_id: int, dialogue_id: int, origin: str, content: bytes): data = json.loads(content.decode()) print ("message received: " + json.dumps(data)) if __name__ == '__main__': #define the ledger parameters api = LedgerApi('127.0.0.1', 8100) #checl if entity has already been generated if(os.path.exists('./workdir/Agent3/agent/server_private.key')): #locate the agent account entity for interacting with the ledger. with open ('./workdir/Agent3/agent/server_private.key', 'r') as private_key_file: server_agentID = Entity.load(private_key_file) else: #create the enity for the agent server_agentID = Entity() #store the private key of the newly created entity with open('./workdir/Agent3/agent/server_private.key', 'w') as private_key_file: server_agentID.dump(private_key_file) startBalance = api.tokens.balance(server_agentID) #set trading values fet_tx_fee = 40 wanted_price = 1000 lowest_price = 600
def on_decline(self, msg_id: int, dialogue_id: int, origin: str, target: int) : print("[{}] Received a decline!".format(self.public_key)) self.received_declines += 1 if __name__ == '__main__': #define the ledger parameters api = LedgerApi('127.0.0.1', 8100) #check if entity has already been created if(os.path.exists('./workdir/Agent_Auction/client/client_private.key')): #locate the agent account entity for interacting with the ledger. with open ('./workdir/Agent_Auction/client/client_private.key', 'r') as private_key_file: client_agentID = Entity.load(private_key_file) else: #create new entity for the agent client_agentID = Entity() #store private key of newly formed entity with open('./workdir/Agent_Auction/client/client_private.key', 'w') as private_key_file: client_agentID.dump(private_key_file) #give the account starting tokens api.sync(api.tokens.wealth(client_agentID, 1000)) startBalance = api.tokens.balance(client_agentID) # define an OEF Agent client_agent = ClientAgent(str(Address(client_agentID)), 50, 5, client_agentID, oef_addr="127.0.0.1", oef_port=10000)
def main(): # create the APIs api = LedgerApi(HOST, PORT) # generate an identity from a known key, which contains funds. multi_sig_identity = Entity.from_hex( "6e8339a0c6d51fc58b4365bf2ce18ff2698d2b8c40bb13fcef7e1ba05df18e4b") # generate a board to control multi-sig account, with variable voting weights. # we use keys for accounts which already have funds. board = [] board.append( Entity.from_hex( "e833c747ee0aeae29e6823e7c825d3001638bc30ffe50363f8adf2693c3286f8") ) board.append( Entity.from_hex( "4083a476c4872f25cb40839ac8d994924bcef12d83e2ba4bd3ed6c9705959860") ) board.append( Entity.from_hex( "20293422c4b5faefba3422ed436427f2d37f310673681e98ac8637b04e756de3") ) board.append( Entity.from_hex( "d5f10ad865fff147ae7fcfdc98b755452a27a345975c8b9b3433ff16f23495fb") ) voting_weights = { board[0]: 1, board[1]: 1, board[2]: 1, board[3]: 2, } # generate another entity as a target for transfers other_identity = Entity.from_hex( "e833c747ee0aeae29e6823e7c825d3001638bc30ffe50363f8adf2693c3286f8") print('Original balance of multi_sig_identity:', api.tokens.balance(multi_sig_identity)) # transfers can happen normally without a deed print('\nSubmitting pre-deed transfer with original signature...') api.sync(api.tokens.transfer(multi_sig_identity, other_identity, 250, 20)) print('Balance 1:', api.tokens.balance(multi_sig_identity)) print('Balance 2:', api.tokens.balance(other_identity)) # submit the original deed print("\nCreating deed...") deed = Deed() for sig, weight in voting_weights.items(): deed.set_signee(sig, weight) # set our initial voting thresholds deed.set_operation(Operation.transfer, 2) deed.set_operation(Operation.amend, 4) api.sync(api.tokens.deed(multi_sig_identity, deed, 6000)) # original address can no longer validate transfers print("\nTransfer with original signature should fail...") try: api.sync( api.tokens.transfer(multi_sig_identity, other_identity, 250, 20)) except RuntimeError as e: print("Transaction failed as expected") else: print("Transaction succeeded, it shouldn't have") # sufficient voting power required to sign transfers print("\nSubmitting transfer with two signatures with total 2 votes...") print_signing_votes(voting_weights, board[:2]) # since we now want to create a transaction which has only been signed by a subset of the board, we must use # the factory interface in order to build out the transaction we are after tx = TokenTxFactory.transfer(multi_sig_identity, other_identity, 250, 20, board[:2]) tx.valid_until = api.tokens.current_block_number() + 100 # the signatories to sign the transaction for signatory in board[:2]: tx.sign(signatory) api.sync(api.submit_signed_tx(tx)) print('Balance 1:', api.tokens.balance(multi_sig_identity)) print('Balance 2:', api.tokens.balance(other_identity)) # some entities may have more voting power print("\nSubmitting transfer with single signature with 2 votes...") print_signing_votes(voting_weights, board[3]) tx = TokenTxFactory.transfer(multi_sig_identity, other_identity, 250, 20, [board[3]]) tx.valid_until = api.tokens.current_block_number() + 100 tx.sign(board[3]) api.sync(api.submit_signed_tx(tx)) print('Balance 1:', api.tokens.balance(multi_sig_identity)) print('Balance 2:', api.tokens.balance(other_identity)) # amend the deed print("\nAmending deed to increase transfer threshold to 3 votes...") deed.set_operation(Operation.transfer, 3) tx = TokenTxFactory.deed(multi_sig_identity, deed, 400, board) tx.valid_until = api.tokens.current_block_number() + 100 for member in board: tx.sign(member) api.sync(api.submit_signed_tx(tx)) # single member no longer has enough voting power print("\nSingle member transfer with 2 votes should no longer succeed...") try: print_signing_votes(voting_weights, board[3]) tx = TokenTxFactory.transfer(multi_sig_identity, other_identity, 250, 20, [board[3]]) tx.valid_until = api.tokens.current_block_number() + 100 tx.sign(board[3]) api.sync(api.submit_signed_tx(tx)) except RuntimeError as e: print("Transaction failed as expected") else: print("Transaction succeeded, it shouldn't have") # correct number of signatory votes print("\nSuccesful transaction with sufficient voting weight...") print_signing_votes(voting_weights, board[1:]) tx = TokenTxFactory.transfer(multi_sig_identity, other_identity, 250, 20, board[1:]) tx.valid_until = api.tokens.current_block_number() + 100 for member in board[1:]: tx.sign(member) api.sync(api.submit_signed_tx(tx)) print('Balance 1:', api.tokens.balance(multi_sig_identity)) print('Balance 2:', api.tokens.balance(other_identity)) # warning: if no amend threshold is set, future amendments are impossible print("\nAmending deed to remove threshold...") deed.remove_operation(Operation.amend) deed.require_amend = False tx = TokenTxFactory.deed(multi_sig_identity, deed, 400, board) tx.valid_until = api.tokens.current_block_number() + 100 for member in board: tx.sign(member) api.sync(api.submit_signed_tx(tx)) deed.set_operation(Operation.amend, 1) print("\nExpecting further amendment to fail...") try: tx = TokenTxFactory.deed(multi_sig_identity, deed, 400, board) tx.valid_until = api.tokens.current_block_number() + 100 for member in board: tx.sign(member) api.sync(api.submit_signed_tx(tx)) except RuntimeError as e: print("Transaction failed as expected") else: print("Transaction succeeded, it shouldn't have")
def __init__(self, address: str, port: int): self.api = LedgerApi(address, port) self.entity = Entity() # Create wealth so that we have the funds to be able to create contracts on the network self.api.sync(self.api.tokens.wealth(self.entity, 10000))