Skip to content

Instantly share code, notes, and snippets.

@AnthonyLaw
Last active April 22, 2022 13:16
Show Gist options
  • Save AnthonyLaw/762f16656f2de41d0409f612af57b4cd to your computer and use it in GitHub Desktop.
Save AnthonyLaw/762f16656f2de41d0409f612af57b4cd to your computer and use it in GitHub Desktop.
Sample Transaction for symbol-sdk v3
const { CryptoTypes } = require('symbol-sdk');
const { NemFacade } = require('symbol-sdk').facade;
// Signer Private Key
const privateKey = new CryptoTypes.PrivateKey("PRIVATE_KEY");
const keyPair = new NemFacade.KeyPair(privateKey);
// Create V1 transaction
const transferTransaction = facade.transactionFactory.create({
type: 'transfer_transaction_v1',
signerPublicKey: keyPair.publicKey.toString(),
fee: BigInt(100000), // 0.1 xem
timestamp, // Get timestamp from network
deadline: timestamp + 3600, // 1 hour deadline = 60 * 60
recipientAddress: address,
amount: BigInt(1000000), // 1 xem
messageEnvelopeSize: message.length + 4 + 4, // 4 bytes for messageType and 4 bytes for messageEnvelopeSize
message: {
messageType: 'plain',
message: 'hello world'
}
});
// Sign Transaction
const signature = facade.signTransaction(keyPair, transferTransaction);
const jsonPayload = facade.transactionFactory.constructor.attachSignature(transferTransaction, signature);
// Convert string to json payload
const payload = JSON.parse(jsonPayload);
// Announce payload in POST method
`<nodeApi>/transaction/announce`
// Symbol
const { CryptoTypes } = require('symbol-sdk');
const { SymbolFacade } = require('symbol-sdk').facade;
const facade = new SymbolFacade('testnet');
const privateKey = new CryptoTypes.PrivateKey("PRIVATE_KEY");
const keyPair = new SymbolFacade.KeyPair(privateKey);
const transaction = facade.transactionFactory.create({
type: 'transfer_transaction',
signerPublicKey: keyPair.publicKey.toString(),
fee: BigInt(100000), // 0.1 xym
deadline: BigInt(timeStamp + 3600), // 1 hour deadline = 60 * 60
recipientAddress: 'TCABUWAK5WMJ26ZPERMGWBOWAJF4XPNCJOWPAAI',
mosaics: [
{
mosaicId: BigInt('0x3A8416DB2D53B6C8'),
amount: BigInt(1000000) // 1 xym
}
]
});
// Sign Transaction
const signature = facade.signTransaction(keyPair, transaction);
const jsonPayload = facade.transactionFactory.constructor.attachSignature(transaction, signature);
// Convert string to json payload
const payload = JSON.parse(jsonPayload);
// Announce payload in PUT
`<nodeApi>/transactions`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment