def run(options, benefactor): entity1 = Entity() # build the ledger API api = LedgerApi(options['host'], options['port']) # create funds so that we have the funds to be able to create contracts on the network api.sync(api.tokens.transfer(benefactor, entity1, 100000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) # deploy the contract to the network status = api.sync(api.contracts.create(entity1, contract, 2000))[0] api.sync(api.tokens.transfer(entity1, contract.address, 10000, 500)) block_number = status.exit_code v = contract.query(api, 'get_init_block_number_state') assert block_number > 0 assert block_number == v, \ 'Returned block number by the contract ({}) is not what expected ({})'.format( v, block_number) api.sync(contract.action(api, 'set_block_number_state', 400, entity1)) assert block_number <= contract.query(api, 'query_block_number_state')
def run(options, benefactor): entity1 = Entity() api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 100000000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) contract2 = Contract(CONTRACT_TEXT2, entity1) api.sync(api.contracts.create(entity1, contract, 10000)) api.sync(api.contracts.create(entity1, contract2, 10000)) BALANCE1 = 5000 BALANCE2 = 3000 assert BALANCE1 != BALANCE2, \ 'Contracts must have different amounts of funds' \ 'to ensure that balance() queries the balance of its respective contract' api.sync(api.tokens.transfer(entity1, contract.address, BALANCE1, 200)) api.sync(api.tokens.transfer(entity1, contract2.address, BALANCE2, 200)) api.sync( contract.action(api, 'c2c_call', 10000, entity1, str(contract2.address))) result_balance1 = contract.query(api, 'query_balance_state') assert result_balance1 == BALANCE1, \ 'Expected BALANCE1 {}, found {}'.format(BALANCE1, result_balance1) result_balance2 = contract.query(api, 'query_c2c_balance_state') assert result_balance2 == BALANCE2, \ 'Expected BALANCE2 {}, found {}'.format(BALANCE2, result_balance2)
def run(options): entity1 = Entity() # build the ledger API api = LedgerApi(options['host'], options['port']) # create wealth so that we have the funds to be able to create contracts on the network print('Create wealth...') api.sync(api.tokens.wealth(entity1, 100000000)) contract = Contract(CONTRACT_TEXT) # deploy the contract to the network print('Create contract...') api.sync(api.contracts.create(entity1, contract, 10000)) submit_synergetic_data(api, contract, [100, 20, 3], entity1) print('Query init state...') init_result = contract.query(api, 'query_init_test') print('Init state: ', init_result) assert init_result == 123 print('Execute action...') api.sync(contract.action(api, 'action_test', 10000, [entity1])) print('Query action state...') action_result = contract.query(api, 'query_action_test') print('Action state: ', action_result) assert action_result == 456
def run(options, benefactor): # Create keypair for the contract owner entity = Entity() address = Address(entity) # build the ledger API api = LedgerApi(options['host'], options['port']) # Need funds to deploy contract api.sync(api.tokens.transfer(benefactor, entity, int(1e7), 1000)) # Load contract source source_file = os.path.join(HERE, "contract.etch") with open(source_file, "r") as fb: source = fb.read() # Create contract contract = Contract(source, entity) # Deploy contract fet_tx_fee = api.tokens.balance(entity) api.sync(api.contracts.create(entity, contract, fet_tx_fee)) # Printing balance of the creating address print(contract.query(api, 'balanceOf', owner=address)) # Getting the 9'th token id. token_id = contract.query(api, 'getTokenId', number=9) # Testing contract.query(api, 'isEqual', number=9, expected=token_id) # Locating the owner of a token print("Finding the owner of ", token_id) print(contract.query(api, 'ownerOf', token_id=token_id))
def run(options, benefactor): entity1 = Entity() api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 100000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) try: contract.query(api, 'some_query') assert False, 'Expected query to fail' except RuntimeError as e: assert 'Unable to look up contract' in str(e), \ 'Unexpected error message from server: {}'.format(e)
def run(options, benefactor): # Create keypair for the contract owner entity = Entity() Address(entity) host = options['host'] port = options['port'] # create the APIs api = LedgerApi(host, port) # Transfer tokens from benefactor api.sync(api.tokens.transfer(benefactor, entity, int(1e7), 1000)) # Load contract source source_file = os.path.join(HERE, "hello_world.etch") with open(source_file, "r") as fb: source = fb.read() # Create contract contract = Contract(source, entity) # Deploy contract api.sync(api.contracts.create(entity, contract, 10000)) # Printing message print(contract.query(api, 'persistentGreeting'))
def main(): # create the API api = LedgerApi('127.0.0.1', 8000) # create an entity from a private key stored in hex entity = Entity.from_hex( '6e8339a0c6d51fc58b4365bf2ce18ff2698d2b8c40bb13fcef7e1ba05df18e4b') # create the contract on the ledger synergetic_contract = Contract(CONTRACT_TEXT, entity) print('Creating contract..') api.sync(api.contracts.create(entity, synergetic_contract, 4096)) # create a whole series of random data to submit to the DAG random_ints = [random.randint(0, 200) for _ in range(10)] fee = 100000000 api.sync( [api.contracts.submit_data(entity, synergetic_contract.digest, synergetic_contract.address, fee, value=value) \ for value in random_ints]) print('Data submitted.') print('Waiting...') api.wait_for_blocks(10) print('Issuing query...') result = synergetic_contract.query(api, 'query_result') print('Query result:', result)
def run(options): entity1 = Entity() # create the APIs api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.wealth(entity1, 1000000)) contract = Contract(CONTRACT_TEXT, entity1) contract2 = Contract(CONTRACT_TEXT2, entity1) api.sync(api.contracts.create(entity1, contract, 20000)) api.sync(api.contracts.create(entity1, contract2, 20000)) current_block = api.tokens._current_block_number() api.sync( contract.action(api, 'c2c_call', 40000, [entity1], str(contract2.address))) time.sleep(2) later_block = contract.query(api, 'query_block_number_state') assert later_block > current_block, \ 'Expected number larger than {}, found {}'.format( current_block, later_block)
class SynergeticContractTestHelper: def __init__(self, name, api, entity, workdir="."): self._api = api self._entity = entity self._name = name self.contract = None self._workdir = workdir def create_new(self, fee_limit): self.contract = Contract(_CONTRACT_TEXT, self._entity) print('Creating contract..') self._api.sync(self._api.contracts.create( self._entity, self.contract, fee_limit)) if len(self._name) > 0: with open(self._workdir+"/"+self._name+".json", "w") as f: self.contract.dump(f) def load(self): print('Loading contract..') with open(self._workdir+"/"+self._name+".json", "r") as f: self.contract = Contract.load(f) def submit_random_data(self, n, number_range, hold_state_sec=10): # create a whole series of random data to submit to the DAG random_ints = [random.randint(*number_range) for _ in range(n)] txs = [self._api.contracts.submit_data(self._entity, self.contract.address, value=value) for value in random_ints] self._api.sync(txs, hold_state_sec=hold_state_sec, extend_success_status=["Submitted"]) print('Data submitted.') def validate_execution(self): result = self.contract.query(self._api, 'query_result') return result != -1
def main(): # create the API api = LedgerApi('127.0.0.1', 8000) # create an entity and provide it some wealth print('Setup...') entity = Entity() api.sync(api.tokens.wealth(entity, 100000000)) print('Setup...complete') # create the contract on the ledger synergetic_contract = Contract(CONTRACT_TEXT) print('Creating contract..') api.sync(api.contracts.create(entity, synergetic_contract, 4096)) print('Contract submitted ({}.{}).'.format( synergetic_contract.digest.to_hex(), synergetic_contract.owner)) # create a whole series of random data to submit to the DAG random_ints = [random.randint(0, 200) for _ in range(10)] api.sync([ api.contracts.submit_data(entity, synergetic_contract.digest, value=value) for value in random_ints ]) print('Data submitted.') print('Waiting...') api.wait_for_blocks(10) print('Issuing query...') result = synergetic_contract.query(api, 'query_result') print('Query result:', result)
def print_address_balances(api: LedgerApi, contract: Contract, addresses: List[Address]): for idx, address in enumerate(addresses): print('Address{}: {:<6d} bFET {:<10d} TOK'.format( idx, api.tokens.balance(address), contract.query(api, 'balance', address=Address(address)))) print()
def run(options, benefactor): entity1 = Entity() api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 100000000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) api.sync(api.contracts.create(entity1, contract, 10000)) result = contract.query(api, 'check_x_with_default') assert result == 999,\ 'Expected to receive default value of 999, got {}'.format(result) try: contract.query(api, 'check_x') assert False, 'Expected query to fail' except RuntimeError: pass api.sync(contract.action(api, 'set_x_to_3', 200, entity1)) result = contract.query(api, 'check_x_with_default') assert result == 3, \ 'Expected to receive value of 3, got {}'.format(result) result = contract.query(api, 'check_x') assert result == 3, \ 'Expected to receive value of 3, got {}'.format(result) api.sync(contract.action(api, 'set_x_to_5', 200, entity1)) result = contract.query(api, 'check_x_with_default') assert result == 5, \ 'Expected to receive value of 5, got {}'.format(result) result = contract.query(api, 'check_x') assert result == 5, \ 'Expected to receive value of 5, got {}'.format(result)
def main(): # create our first private key pair entity1 = Entity() # build the ledger API api = LedgerApi('127.0.0.1', 8100) # create wealth so that we have the funds to be able to create contracts on the network api.sync(api.tokens.wealth(entity1, 1000000000000000)) # create the smart contract contract = Contract(CONTRACT_TEXT, entity1) # deploy the contract to the network api.sync(api.contracts.create(entity1, contract, 1000000000)) # update the graph with a new model fet_tx_fee = 100000000 with open(GRAPH_FILE_NAME, mode='rb') as file: print("reading in graph file...") rfile = file.read() print("encoding to base64 string...") b64obj = base64.b64encode(rfile) obj = b64obj.decode() print("updating smart contract graph...") api.sync( contract.action(api, 'updateGraph', fet_tx_fee, [entity1], obj)) print("finished updating smart contract graph") # set one real example input data set fet_tx_fee = 100000000 api.sync( contract.action(api, 'setHistorics', fet_tx_fee, [entity1], EXAMPLE_INPUT_HISTORICS)) current_historics = contract.query(api, 'getHistorics') print("current historics: " + current_historics) # make a prediction current_prediction = contract.query(api, 'makePrediction') print("current prediction: " + current_prediction)
def run(options, benefactor): entity1 = Entity() api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 100000000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) api.sync(api.contracts.create(entity1, contract, 10000)) api.sync(api.tokens.transfer(entity1, contract.address, 1000, 500)) assert contract.query(api, 'query_init_test') == 0 api.sync(contract.action(api, 'action_test', 200, [entity1])) v = contract.query(api, 'query_action_test') assert 800 <= v <= 1000, \ 'Expected query_action_test result to be between 800 and 1000, found {}'.format( v) submit_synergetic_data(api, contract, [100, 20, 3], entity1) v = contract.query(api, 'query_clear_test') assert 800 <= v <= 1000, \ 'Expected query_clear_test result to be between 800 and 1000, found {}'.format( v) # Provide the contract with funds api.sync(api.tokens.transfer(entity1, contract.address, 1234, 200)) api.sync(contract.action(api, 'action_test', 200, [entity1])) v = contract.query(api, 'query_action_test') assert 2000 < v < 2234, \ 'Expected query_action_test result to be between 2000 and 2234, found {}'.format( v) submit_synergetic_data(api, contract, [100, 20, 3], entity1) v = contract.query(api, 'query_clear_test') assert 2000 < v < 2234, \ 'Expected query_clear_test result to be between 2000 and 2234, found {}'.format( v)
def run(options): entity1 = Entity() # build the ledger API api = LedgerApi(options['host'], options['port']) # create wealth so that we have the funds to be able to create contracts on the network api.sync(api.tokens.wealth(entity1, 100000)) contract = Contract(CONTRACT_TEXT, entity1) # deploy the contract to the network status = api.sync(api.contracts.create(entity1, contract, 2000))[0] block_number = status.exit_code assert block_number > 0 assert block_number == contract.query(api, 'get_init_block_number_state') api.sync(contract.action(api, 'set_block_number_state', 400, [entity1])) assert block_number <= contract.query(api, 'query_block_number_state')
def run(options): entity1 = Entity() api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.wealth(entity1, 100000000)) contract = Contract(CONTRACT_TEXT, entity1) api.sync(api.contracts.create(entity1, contract, 10000)) assert contract.query(api, 'query_init_test') == 0 api.sync(contract.action(api, 'action_test', 10000, [entity1])) assert contract.query(api, 'query_action_test') == 0 submit_synergetic_data(api, contract, [100, 20, 3], entity1) assert contract.query(api, 'query_clear_test') == 0 # Provide the contract with funds api.tokens.transfer(entity1, contract.address, 1234, 200) api.sync(contract.action(api, 'action_test', 10000, [entity1])) assert contract.query(api, 'query_action_test') == 1234 submit_synergetic_data(api, contract, [100, 20, 3], entity1) assert contract.query(api, 'query_clear_test') == 1234
def run(options): entity1 = Entity() # build the ledger API api = LedgerApi(options['host'], options['port']) # create wealth so that we have the funds to be able to create contracts on the network api.sync(api.tokens.wealth(entity1, 100000)) contract = Contract(CONTRACT_TEXT, entity1) # deploy the contract to the network status = api.sync(api.contracts.create(entity1, contract, 2000))[0] saved_address = contract.query(api, 'query_owner_address') assert str(Address(entity1)) == saved_address, \ 'Expected owner address {} but found {}'.format( Address(entity1), saved_address)
def run(options, benefactor): entity1 = Entity() # create the APIs api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 1000000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) contract2 = Contract(CONTRACT_TEXT2, entity1) api.sync(api.contracts.create(entity1, contract, 2000)) api.sync(api.contracts.create(entity1, contract2, 2000)) api.sync(contract.action(api, 'c2c_call', 400, entity1, str(contract2.address))) result = contract.query(api, 'query_eleven') assert result == 11, \ 'Expected 11, found {}'.format(result)
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 print("\nContract deployed:\n") print("Address:", contract.address) print(" Owner:", contract.owner) # Confirm by querying the contract print(" Output:", contract.query(api, 'sayHello'))