def main(): # load up the previously created private key with open('private.key', 'r') as private_key_file: entity1 = Entity.load(private_key_file) # load up the deployed contract with open('sample.contract', 'r') as contract_file: contract = Contract.load(contract_file) # for the purposes of this example create a second private key pair to transfer funds to entity2 = Entity() # build the ledger API api = LedgerApi('127.0.0.1', 8000) # print the current status of all the tokens print('-- BEFORE --') print_address_balances(api, contract, [entity1, entity2]) # transfer from one to the other using our newly deployed contract tok_transfer_amount = 200 fet_tx_fee = 40 api.sync( contract.action(api, 'transfer', fet_tx_fee, [entity1], Address(entity1), Address(entity2), tok_transfer_amount)) print('-- AFTER --') print_address_balances(api, contract, [entity1, entity2])
def main(): print('Loading private key...') # load up the previously created private key with open('private.key', 'r') as private_key_file: entity1 = Entity.load(private_key_file) print('Loading private key...complete') # build the ledger API api = LedgerApi('127.0.0.1', 8000) # create the smart contract contract = SmartContract(CONTRACT_TEXT) print('Deploying contract...') # deploy the contract to the network api.sync(api.contracts.create(entity1, contract, 2000)) print('Deploying contract...complete') # save the contract to the disk with open('sample.contract', 'w') as contract_file: contract.dump(contract_file)
def assertKeyIsPresentOnDisk(self, name: str, password: str, entity: Entity, ctx: TemporaryPocketBookRoot): self.assertTrue(os.path.isdir(ctx.root)) # check that the index record has been created correctly index_path = os.path.join(ctx.root, KeyStore.INDEX_FILE_NAME) self.assertTrue(os.path.isfile(index_path)) with open(index_path, 'r') as index_file: index = toml.load(index_path) # build up the key listings key_addresses = { key['name']: key['address'] for key in index.get('key', []) } # ensure the key is reference in the index self.assertIn(name, key_addresses.keys()) # also ensure the computed address is also correct expected_address = Address(entity) self.assertEqual(key_addresses[name], str(expected_address)) # check the contents of the key key_path = os.path.join(ctx.root, '{}.key'.format(name)) self.assertTrue(os.path.isfile(key_path)) with open(key_path, 'r') as key_file: recovered = Entity.load(key_file, password) self.assertEqual(recovered.private_key, entity.private_key)
def makeClient(number: int): #check if entity has already been created if (os.path.exists('./workdir/Agent_Auction/client/client' + str(number) + '_private.key')): #locate the agent account entity for interacting with the ledger. with open( './workdir/Agent_Auction/client/client' + str(number) + '_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' + str(number) + '_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) price = random.randint(25, 100) interval = random.randint(5, 20) # define an OEF Agent client = client_agent.ClientAgent(str(Address(client_agentID)), price, interval, client_agentID, oef_addr="127.0.0.1", oef_port=10000) print('Balance Before:', startBalance) # connect it to the OEF Node client.connect() # query OEF for DataService providers echo_query1 = Query( [Constraint("timezone", Eq(3)), Constraint("twentyfour", Eq(False))], TIME_AGENT()) client.search_services(0, echo_query1) client.run()
def load_key(self, name: str, password: str) -> Entity: if name not in self.list_keys(): raise KeyNotFoundError() key_file_path = self._format_key_path(name) assert os.path.exists(key_file_path) with open(key_file_path, 'r') as key_file: entity = Entity.load(key_file, password) address = Address(entity) # lookup the metadata for this metadata = self._lookup_meta_data(name) if metadata['address'] != str(address): raise UnableToDecodeKeyError() return entity
def on_decline(self, msg_id: int, dialogue_id: int, origin: str, target: int) : print("Received a decline!") self.received_declines += 1 return 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/Agent4/client/client_private.key')): #locate the agent account entity for interacting with the ledger. with open ('./workdir/Agent4/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/Agent4/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, 2000)) startBalance = api.tokens.balance(client_agentID) # define an OEF Agent client_agent = ClientAgent(str(Address(client_agentID)), oef_addr="127.0.0.1", oef_port=10000)
self.participants.clear() self.price = self.startPrice 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/Agent_Auction/agent/server_private.key')): #locate the agent account entity for interacting with the ledger. with open ('./workdir/Agent_Auction/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/Agent_Auction/agent/server_private.key', 'w') as private_key_file: server_agentID.dump(private_key_file) startBalance = api.tokens.balance(server_agentID) #set trading values price = 20 fet_tx_fee = 40 print('Price:', price)