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): 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)
def run(options, benefactor): entity1 = Entity() # create the APIs api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 1000000, 1000)) contracts = [] for i in range(20): contracts += [Contract(CONTRACT_TEXT, entity1)] contract_terminal = Contract(TERMINAL_CONTRACT_TEXT, entity1) for contract in contracts: api.sync(api.contracts.create(entity1, contract, 2000)) api.sync(api.contracts.create(entity1, contract_terminal, 2000)) for i in range(len(contracts) - 1): api.sync(contracts[i].action(api, 'set_callee', 400, entity1, str(contracts[i + 1].address))) api.sync(contracts[-1].action(api, 'set_callee', 400, entity1, str(contract_terminal.address))) try: api.sync(contracts[0].action(api, 'c2c_call', 400, entity1)) assert False, \ 'Expected transaction to fail' except RuntimeError: pass
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 test_create(self, mock_shard_mask): # create contract owner = Entity() contract = Contract(CONTRACT_TEXT, owner) # Mock api for providing number of lanes and receiving create call api = mock.Mock(spec=LedgerApi) api.server = mock.Mock() lane_number = 2 api.server.num_lanes.side_effect = [lane_number] api.contracts = mock.Mock(spec=ContractsApi) # Mock shard mask static method dummy_shard_mask = mock.Mock() mock_shard_mask.side_effect = [dummy_shard_mask] contract.create(api, owner, 1000) # Check shard mask gen called with contract digest address mock_shard_mask.assert_called_once_with( ['fetch.contract.state.{}'.format(contract.digest.to_hex())], lane_number) # Check api create method called api.contracts.create.assert_called_once_with( owner, contract, 1000, shard_mask=dummy_shard_mask)
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 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 main(source, name): # Constellation HOST = '127.0.0.1' PORT = 8100 # deploy the contract to the network api = LedgerApi(HOST, PORT) # api = LedgerApi(network='alphanet') # Create keypair for the contract owner owner = Entity() owner_addr = Address(owner) # print(owner_addr) # Need funds to deploy contract api.sync(api.tokens.wealth(owner, 20000)) # Create contract contract = Contract(source, owner) # Deploy contract api.sync(api.contracts.create(owner, contract, 10000)) # Save the contract to the disk with open('sample.contract', 'w') as contract_file: contract.dump(contract_file) # Save the contract owner's private key to disk with open('owner_private.key', 'w') as private_key_file: owner.dump(private_key_file) print(f"Contract {name}.etch has successfully been deployed.")
def main(): # create our first private key pair entity1 = Entity() address1 = Address(entity1) # create a second private key pair entity2 = Entity() address2 = Address(entity2) # build the ledger API api = LedgerApi('127.0.0.1', 8000) # create wealth so that we have the funds to be able to create contracts on the network api.sync(api.tokens.wealth(entity1, 10000)) # create the smart contract contract = Contract(CONTRACT_TEXT, entity1) with track_cost(api.tokens, entity1, "Cost of creation: "): api.sync(contract.create(api, entity1, 4000)) # print the current status of all the tokens print('-- BEFORE --') print_address_balances(api, contract, [address1, address2]) # transfer from one to the other using our newly deployed contract tok_transfer_amount = 200 fet_tx_fee = 160 with track_cost(api.tokens, entity1, "Cost of transfer: "): api.sync(contract.action(api, 'transfer', fet_tx_fee, [entity1], address1, address2, tok_transfer_amount)) print('-- AFTER --') print_address_balances(api, contract, [address1, address2])
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(): # In examples we use addresses which already have funds entity1 = Entity.from_hex( '6e8339a0c6d51fc58b4365bf2ce18ff2698d2b8c40bb13fcef7e1ba05df18e4b') entity2 = Entity.from_hex( 'e833c747ee0aeae29e6823e7c825d3001638bc30ffe50363f8adf2693c3286f8') address1 = Address(entity1) # create a second private key pair address2 = Address(entity2) # build the ledger API api = LedgerApi('127.0.0.1', 8000) # create the smart contract contract = Contract(CONTRACT_TEXT, entity1) with track_cost(api.tokens, entity1, "Cost of creation: "): api.sync(contract.create(api, entity1, 4000)) # print the current status of all the tokens print('-- BEFORE --') print_address_balances(api, contract, [address1, address2]) # transfer from one to the other using our newly deployed contract tok_transfer_amount = 200 fet_tx_fee = 160 with track_cost(api.tokens, entity1, "Cost of transfer: "): api.sync( contract.action(api, 'transfer', fet_tx_fee, entity1, address1, address2, tok_transfer_amount)) print('-- AFTER --') print_address_balances(api, contract, [address1, address2])
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 main(): print('Loading private key...') # load up the previously created private key with open('private.key', 'r') as private_key_file: entity1 = Entity.prompt_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 = Contract(CONTRACT_TEXT, entity1) 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 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 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): entity1 = Entity() api = LedgerApi(options['host'], options['port']) api.sync(api.tokens.transfer(benefactor, entity1, 100000, 1000)) contract = Contract(CONTRACT_TEXT, entity1) try: api.sync([contract.action(api, 'some_action', 100, entity1)]) assert False, 'Expected action to fail' except RuntimeError as e: assert 'Contract Lookup Failure' in str(e), \ 'Unexpected error message from server: {}'.format(e)
def test_dumps_and_loads_without_owner(self): # create the contract orig = Contract(CONTRACT_TEXT) # encode the contract encoded = orig.dumps() # re-create the contract new = Contract.loads(encoded) # checks self.assertIsInstance(new, Contract) self.assertEqual(orig.owner, new.owner) self.assertEqual(orig.digest, new.digest) self.assertEqual(orig.source, new.source)
def test_dumps_and_loads(self): owner = Entity() orig = Contract(CONTRACT_TEXT, owner, b'this is a nonce') # encode the contract encoded = orig.dumps() # re-create the contract new = Contract.loads(encoded) # checks self.assertIsInstance(new, Contract) self.assertEqual(orig.owner, new.owner) self.assertEqual(orig.digest, new.digest) self.assertEqual(orig.source, new.source)
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 main(): # load up the previously created private key with open('private.key', 'r') as private_key_file: entity1 = Entity.prompt_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 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)))
def setup(api, benefactor): entity1 = Entity() api.sync(api.tokens.transfer(benefactor, entity1, 100000, 1000)) contract1 = Contract(TRANSFER_CONTRACT_TEXT, entity1) contract2 = Contract(CONTRACT_TEXT, entity1) initial_owner_balance = api.tokens.balance(Address(entity1)) assert initial_owner_balance == 100000, \ 'Expected initial directly-queried balance to be 0, found {}'.format( 100000, initial_owner_balance) api.sync(api.contracts.create(entity1, contract1, 2000)) api.sync(api.contracts.create(entity1, contract2, 2000)) return entity1, contract1, contract2
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) # deploy the contract to the network api.sync(api.contracts.create(entity1, contract, 2000)) api.sync(contract.action(api, 'set_block_number_state', 400, [entity1])) assert contract.query(api, 'query_block_number_state') > 0
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): 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 deploy(self, contract_name, contract_text, owner=Entity(), top_up=20000, tx_fee=10000): contract = Contract(contract_text, owner) self.api.sync(self.api.tokens.wealth(owner, top_up)) self.api.sync(self.api.contracts.create(owner, contract, tx_fee)) self.add_contract(contract_name, contract_text, contract, owner) self.add_entity(f"{contract_name}_owner", owner, top_up=0)
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)
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) api.sync(api.contracts.create(entity1, contract, 2000)) try: api.sync( contract.action(api, 'c2c_call', 400, entity1, str(contract.address))) assert False, \ 'Expected transaction to fail' except RuntimeError: pass
def deploy(self, contract : str): """ Deploy contract. Feeds back compilation errors. """ try: contract = Contract(contract) with self.track_cost(self.api.tokens, self.entity, "Cost of creation: "): result = contract.create(self.api, self.entity, 4000) self.api.sync(result) return result except Exception as e: print(e)
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)