Skip to content

Instantly share code, notes, and snippets.

@gimre-xymcity
Created November 4, 2022 20:39
Show Gist options
  • Save gimre-xymcity/402e9b346f2b2c85d948e40635480a10 to your computer and use it in GitHub Desktop.
Save gimre-xymcity/402e9b346f2b2c85d948e40635480a10 to your computer and use it in GitHub Desktop.
Sai testnet send aggregate v2 sample
# This is slightly altered send.py sample for SAI testnet (sainet)
import requests
import time
from binascii import hexlify
from datetime import datetime, timezone
from symbolchain.CryptoTypes import Hash256, PrivateKey
from symbolchain.facade.SymbolFacade import SymbolFacade
from symbolchain.sc import Amount, Timestamp
from symbolchain.symbol.IdGenerator import generate_mosaic_alias_id
from symbolchain.symbol.Network import Network
facade = SymbolFacade('testnet')
signer_key_pair = facade.KeyPair(PrivateKey('<SUPER-SECRET-PRIVATE-KEY>'))
recipient = 'TABLEXX34WA6EQI3GU2LIJ3FQOTWDYBE4CARTRA'
# 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]
})
HOST = 'https://001-sai-dual.symboltest.net:3001'
# get SAI network settings
properties = requests.get(f'{HOST}/network/properties').json()
assert properties['network']['epochAdjustment'][-1] == 's', 'epoch adjustment is not expressed in seconds'
#
# re-create facade to use proper generation hash
#
# This is imporant part, proper generation hash needs to be passed down to facade,
# otherwise generated signature will be invalid
#
sai_testnet = Network(
'sai testnet',
0x98,
datetime.fromtimestamp(int(properties['network']['epochAdjustment'][:-1]), tz=timezone.utc),
Hash256(properties['network']['generationHashSeed']))
facade = SymbolFacade(sai_testnet)
# get latest block timestamp
chain_info_results = requests.get(f'{HOST}/chain/info').json()
height = int(chain_info_results['height'])
top_block = requests.get(f'{HOST}/blocks/{height}').json()
top_block_timestamp = int(top_block['block']['timestamp'])
current_block_timestamp = facade.network.network_timestamp_class(top_block_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(signer_key_pair, transaction)
facade.transaction_factory.attach_signature(transaction, signature)
transaction_hash = facade.hash_transaction(transaction)
print(f'transaction hash: {transaction_hash}\n')
# optionally POST the transaction
json_data = {'payload': hexlify(transaction.serialize()).decode('utf8')}
response = requests.put(f'{HOST}/transactions', json=json_data)
# check status in loop
print(f'checking tx status via: {HOST}/transactionStatus/{transaction_hash}')
response = None
for i in range(10):
response = requests.get(f'{HOST}/transactionStatus/{transaction_hash}')
if 404 == response.status_code:
time.sleep(0.5)
elif 200 == response.status_code:
break
else:
print('unhandled response', response, response.text)
print('response:')
print(response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment