def test_unsupported_ink_env_type_handling(self): with self.assertRaises(NotImplementedError): ContractMetadata.create_from_file(metadata_file=os.path.join( os.path.dirname(__file__), 'fixtures', 'unsupported_type_metadata.json'), substrate=self.substrate)
def setUpClass(cls): logging.info("init deplay") cls.env = SubstrateTestEnv.create_europa(port=39944) cls.env.start_node() cls.substrate = SubstrateInterface(url=cls.env.url(), type_registry_preset=cls.env.typ(), type_registry=cls.env.types()) cls.contract_metadata = ContractMetadata.create_from_file( metadata_file=os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.json'), substrate=cls.substrate) cls.erc20 = ERC20.create_from_contracts( substrate=cls.substrate, contract_file=os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.wasm'), metadata_file=os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.json')) cls.alice = Keypair.create_from_uri('//Alice') cls.bob = Keypair.create_from_uri('//Bob') cls.erc20.instantiate_with_code(cls.alice, 1000000 * (10**15)) cls.observer = ERC20Observer.create_from_address( substrate=cls.substrate, contract_address=cls.erc20.contract_address, metadata_file=os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.json'))
def test_contract_api_erc20(self): contract_metadata = ContractMetadata.create_from_file( metadata_file=os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.json'), substrate=self.substrate ) erc20 = ERC20.create_from_contracts( substrate= self.substrate, contract_file= os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.wasm'), metadata_file= os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.json') ) erc20.instantiate_with_code(self.alice, 1000000 * (10 ** 15)) api = ContractAPI(erc20.contract_address, contract_metadata, self.substrate) alice_balance_old = api.balance_of(self.bob, self.alice.ss58_address) res = api.transfer(self.alice, self.bob.ss58_address, 100000, gas_limit=20000000000) logging.info(f'transfer res {res.error_message}') self.assertTrue(res.is_success) alice_balance = api.balance_of(self.bob, self.alice.ss58_address) logging.info(f'transfer alice_balance {alice_balance}') bob_balance = api.balance_of(self.bob, self.bob.ss58_address) logging.info(f'transfer bob_balance {bob_balance}') self.assertEqual(alice_balance, alice_balance_old - 100000) self.assertEqual(bob_balance, 100000)
def create_from_address(cls, contract_address: str, metadata_file: str, substrate: SubstrateInterface = None): """ Create a ContractObserver object that already exists on-chain providing a SS58-address and the path to the metadata JSON of that contract Parameters ---------- contract_address metadata_file substrate Returns ------- ContractObserver """ metadata = ContractMetadata.create_from_file(metadata_file, substrate=substrate) return cls(contract_address=contract_address, metadata=metadata, substrate=substrate)
def create_from_address(cls, contract_address: str, metadata_file: str, substrate: SubstrateInterface = None): metadata = ContractMetadata.create_from_file(metadata_file, substrate=substrate) return cls(contract_address=contract_address, metadata=metadata, substrate=substrate)
def create_from_file(cls, code_file: str, metadata_file: str, substrate: SubstrateInterface = None): metadata = ContractMetadata.create_from_file(metadata_file, substrate=substrate) code = ContractCode.create_from_contract_files(code_file, metadata_file, substrate) return cls(code=code, metadata=metadata, substrate=substrate)
def test_get_contract_event_type(self): contract_metadata = ContractMetadata.create_from_file( metadata_file=os.path.join(os.path.dirname(__file__), 'constracts', 'ink', 'erc20.json'), substrate=self.substrate ) typ14 = contract_metadata.get_type_string_for_metadata_type(14) logging.debug("typ15 {}".format(typ14)) decoder = ScaleDecoder.get_decoder_class(typ14, ScaleBytes("0x01d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"), self.substrate.runtime_config) evtTransferArgs = decoder.decode() logging.debug("evtTransfer {}".format(evtTransferArgs)) self.assertEqual(evtTransferArgs, '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') type_data = get_contract_event_type(contract_metadata) logging.debug("type_event_data {}".format(type_data)) decoder = ScaleDecoder.get_decoder_class(type_data, ScaleBytes("0x000001d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d0000a0dec5adc9353600000000000000"), self.substrate.runtime_config) evtTransfer1 = decoder.decode() self.assertEqual(evtTransfer1['Transfer']['from'], None) self.assertEqual(evtTransfer1['Transfer']['to'], '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') self.assertEqual(evtTransfer1['Transfer']['value'], 1000000000000000000000) logging.debug("evtTransfer {}".format(evtTransfer1)) decoder = ScaleDecoder.get_decoder_class(type_data, ScaleBytes("0x0001d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d018eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4810270000000000000000000000000000"), self.substrate.runtime_config) evtTransfer2 = decoder.decode() self.assertEqual(evtTransfer2['Transfer']['from'], '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') self.assertEqual(evtTransfer2['Transfer']['to'], '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty') self.assertEqual(evtTransfer2['Transfer']['value'], 10000) logging.debug("evtTransfer2 {}".format(evtTransfer2)) decoder = ScaleDecoder.get_decoder_class(type_data, ScaleBytes("0x01d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4810270000000000000000000000000000"), self.substrate.runtime_config) evtApprove1 = decoder.decode() self.assertEqual(evtApprove1['Approval']['owner'], '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') self.assertEqual(evtApprove1['Approval']['spender'], '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty') self.assertEqual(evtApprove1['Approval']['value'], 10000) logging.debug("evtApprove1 {}".format(evtApprove1))
def setUpClass(cls): logging.info("init deplay erc20") cls.env = SubstrateTestEnv.create_europa(port=39944) cls.env.start_node() cls.substrate=SubstrateInterface(url=cls.env.url(), type_registry_preset=cls.env.typ(), type_registry=cls.env.types()) cls.contract_metadata = ContractMetadata.create_from_file( metadata_file=os.path.join(os.path.dirname(__file__), example_path, 'metadata.json'), substrate=cls.substrate ) cls.factory = ContractFactory.create_from_file( substrate=cls.substrate, code_file=os.path.join(os.path.dirname(__file__), example_path, 'erc20ownable.wasm'), metadata_file=os.path.join(os.path.dirname(__file__), example_path, 'metadata.json') ) cls.alice = Keypair.create_from_uri('//Alice') cls.bob = Keypair.create_from_uri('//Bob') cls.api = cls.factory.new(cls.alice, 1000000 * (10 ** 15), endowment=10**15, gas_limit=1000000000000)
def test_incorrect_metadata_file(self): with self.assertRaises(ContractMetadataParseException): ContractMetadata.create_from_file(metadata_file=os.path.join( os.path.dirname(__file__), 'fixtures', 'incorrect_metadata.json'), substrate=self.substrate)
def setUp(self) -> None: self.contract_metadata = ContractMetadata.create_from_file( metadata_file=os.path.join(os.path.dirname(__file__), 'fixtures', 'erc20.json'), substrate=self.substrate)