Last active
November 1, 2022 14:21
-
-
Save gimre-xymcity/d942b2afbc58f9cdaff3953b27258e48 to your computer and use it in GitHub Desktop.
Transfer sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests # imported to get last block time | |
from symbolchain.symbol.IdGenerator import generate_mosaic_alias_id | |
from symbolchain.facade.SymbolFacade import SymbolFacade | |
from symbolchain.sc import Amount, Timestamp | |
facade = SymbolFacade('mainnet') | |
# create inner transaction | |
amount = 1 | |
descriptor = { | |
'type': 'transfer_transaction_v1', | |
'signer_public_key': str(signer_key_pair.public_key), | |
'recipient_address': recipient, | |
'mosaics': [ | |
{ | |
'mosaic_id': generate_mosaic_alias_id('symbol.xym'), | |
'amount': int(amount * 1_000_000) | |
} | |
] | |
} | |
embedded = facade.transaction_factory.create_embedded(descriptor) | |
# create an aggregate | |
transaction = facade.transaction_factory.create({ | |
'type': 'aggregate_complete_transaction_v2', | |
'signer_public_key': str(signer_key_pair.public_key), | |
'transactions_hash': facade.hash_embedded_transactions([embedded]).bytes, | |
'transactions': [embedded] | |
}) | |
# get latest block timestamp | |
HOST = 'http://node.shin.gg:3000' | |
chain_info_results = requests.get(f'{HOST}/chain/info').json() | |
height = int(chain_info_results['height']) | |
block_results = requests.get(f'{HOST}/blocks/{height}').json() | |
timestamp = int(block_results['block']['timestamp']) | |
current_block_timestamp =facade.network.network_timestamp_class(timestamp) | |
# set deadline 1h ahead | |
# set fee to something sensible | |
transaction.deadline = Timestamp(current_block_timestamp.add_hours(1).timestamp) | |
transaction.fee = Amount(100 * transaction.size) | |
signature = facade.sign_transaction(key_pair, transaction) | |
facade.transaction_factory.attach_signature(transaction, signature) | |
print(f'transaction ash: {facade.hash_transaction(transaction)}\n') | |
# optionally POST the transaction | |
# json_data = {'payload': hexlify(transaction.serialize()).decode('utf8')} | |
# response = requests.put(f'{HOST}/transactions', json=json_data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment