def __init_debot3(self, keypair: KeyPair) -> str: signer = Signer.Keys(keys=keypair) debot_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'Debot3.abi.json')) with open(os.path.join(SAMPLES_DIR, 'Debot3.tvc'), 'rb') as fp: debot_tvc = base64.b64encode(fp.read()).decode() call_set = CallSet(function_name='constructor') deploy_set = DeploySet(tvc=debot_tvc) encode_params = ParamsOfEncodeMessage( abi=debot_abi, signer=signer, deploy_set=deploy_set, call_set=call_set) message = async_custom_client.abi.encode_message(params=encode_params) # Check if debot contract does not exists debot_address = self.__check_address(address=message.address) if not debot_address: process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) debot = async_custom_client.processing.process_message( params=process_params) debot_address = debot.transaction['account_addr'] # Set ABI call_set = CallSet( function_name='setABI', input={'dabi': debot_abi.value.encode().hex()}) async_custom_client.processing.process_message( params=ParamsOfProcessMessage( message_encode_params=ParamsOfEncodeMessage( abi=debot_abi, signer=signer, address=debot_address, call_set=call_set), send_events=False)) return debot_address
def test_encode_internal_message_run(self): address = '0:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef' abi = Abi.from_path(path=os.path.join(SAMPLES_DIR, 'Hello.abi.json')) result = async_core_client.abi.encode_internal_message( params=ParamsOfEncodeInternalMessage(abi=abi, value='1000000000', address=address, call_set=CallSet( function_name='sayHello'), bounce=True)) self.assertEqual(address, result.address) self.assertEqual( 'te6ccgEBAQEAOgAAcGIACRorPEhV5veJGis8SFXm94kaKzxIVeb3iRorPEhV5veh3NZQAAAAAAAAAAAAAAAAAABQy+0X', result.message) boc_hash = async_core_client.boc.get_boc_hash( params=ParamsOfGetBocHash(boc=result.message)) self.assertEqual(result.message_id, boc_hash.hash) parsed = async_core_client.boc.parse_message(params=ParamsOfParse( boc=result.message)) self.assertEqual('internal', parsed.parsed['msg_type_name']) self.assertEqual('', parsed.parsed['src']) self.assertEqual(address, parsed.parsed['dst']) self.assertEqual('0x3b9aca00', parsed.parsed['value']) self.assertEqual(True, parsed.parsed['bounce']) self.assertEqual(True, parsed.parsed['ihr_disabled'])
def setUp(self) -> None: self.keypair = KeyPair.load( path=os.path.join(SAMPLES_DIR, 'keys_raw.json'), is_binary=False) self.target_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'DebotTarget.abi.json')) with open(os.path.join(SAMPLES_DIR, 'DebotTarget.tvc'), 'rb') as fp: self.target_tvc = base64.b64encode(fp.read()).decode()
def send_grams(address: str): giver_abi = Abi.from_path(path=os.path.join(SAMPLES_DIR, 'Giver.abi.json')) call_set = CallSet(function_name='grant', input={'dest': address}) encode_params = ParamsOfEncodeMessage(abi=giver_abi, signer=Signer.NoSigner(), address=GIVER_ADDRESS, call_set=call_set) process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) async_custom_client.processing.process_message(params=process_params)
def __init_debot5(self, count: int) -> Tuple[List[str], str]: debot_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'Debot5.abi.json')) with open(os.path.join(SAMPLES_DIR, 'Debot5.tvc'), 'rb') as fp: debot_tvc = base64.b64encode(fp.read()).decode() result = async_custom_client.boc.get_code_from_tvc( params=ParamsOfGetCodeFromTvc(tvc=debot_tvc)) result = async_custom_client.boc.get_boc_hash( params=ParamsOfGetBocHash(boc=result.code)) contract_hash = result.hash call_set = CallSet( function_name='constructor', input={'codeHash': f'0x{contract_hash}'}) deploy_set = DeploySet(tvc=debot_tvc) deploy_params = ParamsOfEncodeMessage( abi=debot_abi, deploy_set=deploy_set, call_set=call_set, signer=Signer.NoSigner()) addresses = [] for i in range(count): # Set signer for deploy params keypair = async_custom_client.crypto.generate_random_sign_keys() deploy_params.signer = Signer.Keys(keys=keypair) # Calculate address message = async_custom_client.abi.encode_message( params=deploy_params) self.__check_address(address=message.address) # Deploy address process_params = ParamsOfProcessMessage( message_encode_params=deploy_params, send_events=False) result = async_custom_client.processing.process_message( params=process_params) debot_address = result.transaction['account_addr'] addresses.append(debot_address) if i > 0: continue # Set ABI call_set = CallSet( function_name='setABI', input={'dabi': debot_abi.value.encode().hex()}) async_custom_client.processing.process_message( params=ParamsOfProcessMessage( message_encode_params=ParamsOfEncodeMessage( abi=debot_abi, signer=deploy_params.signer, address=debot_address, call_set=call_set), send_events=False)) return addresses, contract_hash
def setUp(self) -> None: # Events contract params self.events_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'Events.abi.json')) self.keypair = KeyPair( public= '4c7c408ff1ddebb8d6405ee979c716a14fdd6cc08124107a61d3c25597099499', secret= 'cc8929d635719612a9478b9cd17675a39cfad52d8959e8a177389b8c0b9122a7') with open(os.path.join(SAMPLES_DIR, 'Events.tvc'), 'rb') as fp: self.events_tvc = base64.b64encode(fp.read()).decode() self.events_time = 1599458364291 self.events_expire = 1599458404
def test_encode_internal_message_deploy(self): abi = Abi.from_path(path=os.path.join(SAMPLES_DIR, 'Hello.abi.json')) with open(os.path.join(SAMPLES_DIR, 'Hello.tvc'), 'rb') as fp: tvc = base64.b64encode(fp.read()).decode() result = async_core_client.abi.encode_internal_message( params=ParamsOfEncodeInternalMessage( abi=abi, value='0', deploy_set=DeploySet(tvc=tvc), call_set=CallSet(function_name='constructor'))) self.assertEqual( 'te6ccgECHAEABG0AAmliADYO5IoxskLmUfURre2fOB04OmP32VjPwA/lDM/Cpvh8AAAAAAAAAAAAAAAAAAIxotV8/gYBAQHAAgIDzyAFAwEB3gQAA9AgAEHYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQCJv8A9KQgIsABkvSg4YrtU1gw9KEJBwEK9KQg9KEIAAACASAMCgHo/38h0wABjiaBAgDXGCD5AQFw7UTQ9AWAQPQO8orXC/8B7Ucib3XtVwMB+RDyqN7tRNAg10nCAY4W9ATTP9MA7UcBb3EBb3YBb3MBb3LtV44Y9AXtRwFvcnBvc3BvdsiAIM9AydBvce1X4tM/Ae1HbxMhuSALAGCfMCD4I4ED6KiCCBt3QKC53pntRyFvUyDtVzCUgDTy8OIw0x8B+CO88rnTHwHxQAEC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fB9sw4CMh8UABXwc=', result.message) boc_hash = async_core_client.boc.get_boc_hash( params=ParamsOfGetBocHash(boc=result.message)) self.assertEqual(result.message_id, boc_hash.hash) parsed = async_core_client.boc.parse_message(params=ParamsOfParse( boc=result.message)) code = async_core_client.boc.get_code_from_tvc( params=ParamsOfGetCodeFromTvc(tvc=tvc)) self.assertEqual(code.code, parsed.parsed['code'])
def test_suspend_resume(self): # Data for contract deployment keypair = async_custom_client.crypto.generate_random_sign_keys() abi = Abi.from_path(path=os.path.join(SAMPLES_DIR, 'Hello.abi.json')) with open(os.path.join(SAMPLES_DIR, 'Hello.tvc'), 'rb') as fp: tvc = base64.b64encode(fp.read()).decode() signer = Signer.Keys(keys=keypair) deploy_set = DeploySet(tvc=tvc) call_set = CallSet(function_name='constructor') # Prepare deployment params encode_params = ParamsOfEncodeMessage( abi=abi, signer=signer, deploy_set=deploy_set, call_set=call_set) encode = async_custom_client.abi.encode_message(params=encode_params) # Subscribe for address deploy transaction status transactions = [] def __callback(response_data, response_type, *args): if response_type == SubscriptionResponseType.OK: result = ResultOfSubscription(**response_data) transactions.append(result.result) self.assertEqual(encode.address, result.result['account_addr']) if response_type == SubscriptionResponseType.ERROR: logging.info(ClientError(**response_data).__str__()) subscribe_params = ParamsOfSubscribeCollection( collection='transactions', result='id account_addr', filter={'account_addr': {'eq': encode.address}, 'status_name': {'eq': 'Finalized'}}) subscribe = async_custom_client.net.subscribe_collection( params=subscribe_params, callback=__callback) # Send grams to new account to create first transaction send_grams(address=encode.address) # Give some time for subscription to receive all data time.sleep(2) # Suspend subscription async_custom_client.net.suspend() time.sleep(2) # Wait a bit for suspend # Deploy to create second transaction. # Use another client, because of error: Fetch first block failed: # Can not use network module since it is suspended second_config = ClientConfig() second_config.network.server_address = CUSTOM_BASE_URL second_client = TonClient(config=second_config) process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) second_client.processing.process_message(params=process_params) second_client.destroy_context() # Check that second transaction is not received when # subscription suspended self.assertEqual(1, len(transactions)) # Resume subscription async_custom_client.net.resume() time.sleep(2) # Wait a bit for resume # Run contract function to create third transaction call_set = CallSet(function_name='touch') encode_params = ParamsOfEncodeMessage( abi=abi, signer=signer, address=encode.address, call_set=call_set) process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) async_custom_client.processing.process_message(params=process_params) # Give some time for subscription to receive all data time.sleep(2) # Check that third transaction is now received after resume self.assertEqual(2, len(transactions)) self.assertNotEqual(transactions[0]['id'], transactions[1]['id']) # Unsubscribe async_custom_client.net.unsubscribe(params=subscribe)
def setUp(self) -> None: self.events_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'Events.abi.json')) with open(os.path.join(SAMPLES_DIR, 'Events.tvc'), 'rb') as fp: self.events_tvc = base64.b64encode(fp.read()).decode() self.deploy_set = DeploySet(tvc=self.events_tvc)
def __init_debot_pair(self, keypair: KeyPair) -> Tuple[str, str]: signer = Signer.Keys(keys=keypair) debot_a_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'DebotPairA.abi.json')) with open(os.path.join(SAMPLES_DIR, 'DebotPairA.tvc'), 'rb') as fp: debot_a_tvc = base64.b64encode(fp.read()).decode() debot_b_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'DebotPairB.abi.json')) with open(os.path.join(SAMPLES_DIR, 'DebotPairB.tvc'), 'rb') as fp: debot_b_tvc = base64.b64encode(fp.read()).decode() # Deploy debot B call_set = CallSet(function_name='constructor') deploy_set = DeploySet(tvc=debot_b_tvc) encode_params = ParamsOfEncodeMessage( abi=debot_b_abi, signer=signer, deploy_set=deploy_set, call_set=call_set) message = async_custom_client.abi.encode_message(params=encode_params) b_address = message.address self.__check_address(address=b_address) process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) debot_b = async_custom_client.processing.process_message( params=process_params) b_address = debot_b.transaction['account_addr'] # Set ABI call_set = CallSet( function_name='setAbi', input={'debotAbi': debot_b_abi.value.encode().hex()}) async_custom_client.processing.process_message( params=ParamsOfProcessMessage( message_encode_params=ParamsOfEncodeMessage( abi=debot_b_abi, signer=signer, address=b_address, call_set=call_set), send_events=False)) # Deploy debot A call_set = CallSet( function_name='constructor', input={ 'targetAddr': b_address }) deploy_set = DeploySet(tvc=debot_a_tvc) encode_params = ParamsOfEncodeMessage( abi=debot_a_abi, signer=signer, deploy_set=deploy_set, call_set=call_set) message = async_custom_client.abi.encode_message(params=encode_params) a_address = message.address self.__check_address(address=a_address) process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) debot_a = async_custom_client.processing.process_message( params=process_params) a_address = debot_a.transaction['account_addr'] # Set ABI call_set = CallSet( function_name='setAbi', input={'debotAbi': debot_a_abi.value.encode().hex()}) async_custom_client.processing.process_message( params=ParamsOfProcessMessage( message_encode_params=ParamsOfEncodeMessage( abi=debot_a_abi, signer=signer, address=a_address, call_set=call_set), send_events=False)) return a_address, b_address
def __init_debot4(self, keypair: KeyPair) -> Tuple[str, str]: signer = Signer.Keys(keys=keypair) debot_abi = Abi.from_path( path=os.path.join(SAMPLES_DIR, 'Debot4.abi.json')) with open(os.path.join(SAMPLES_DIR, 'Debot4.tvc'), 'rb') as fp: debot_tvc = base64.b64encode(fp.read()).decode() # Init target call_set = CallSet(function_name='constructor') deploy_set = DeploySet(tvc=self.target_tvc) encode_params = ParamsOfEncodeMessage( abi=self.target_abi, signer=signer, deploy_set=deploy_set, call_set=call_set) message = async_custom_client.abi.encode_message(params=encode_params) target_address = message.address self.__check_address(address=target_address) # Deploy debot call_set = CallSet( function_name='constructor', input={ 'targetAbi': self.target_abi.value.encode().hex(), 'targetAddr': target_address }) deploy_set = DeploySet(tvc=debot_tvc) encode_params = ParamsOfEncodeMessage( abi=debot_abi, signer=signer, deploy_set=deploy_set, call_set=call_set) message = async_custom_client.abi.encode_message(params=encode_params) # Check if debot contract does not exists debot_address = self.__check_address(address=message.address) if not debot_address: process_params = ParamsOfProcessMessage( message_encode_params=encode_params, send_events=False) debot = async_custom_client.processing.process_message( params=process_params) debot_address = debot.transaction['account_addr'] # Set ABI call_set = CallSet( function_name='setAbi', input={'debotAbi': debot_abi.value.encode().hex()}) async_custom_client.processing.process_message( params=ParamsOfProcessMessage( message_encode_params=ParamsOfEncodeMessage( abi=debot_abi, signer=signer, address=debot_address, call_set=call_set), send_events=False)) # Set image call_set = CallSet( function_name='setImage', input={ 'image': self.target_tvc, 'pubkey': f'0x{keypair.public}' }) async_custom_client.processing.process_message( params=ParamsOfProcessMessage( message_encode_params=ParamsOfEncodeMessage( abi=debot_abi, signer=signer, address=debot_address, call_set=call_set), send_events=False)) return debot_address, target_address
def _ABI(name): return Abi.from_path(Path.cwd() / 'core' / 'package' / 'ton' / 'abi' / f'{name}.abi.json')