Last active
August 3, 2020 19:54
-
-
Save cgcardona/3e786d07fb24b3851f5b75856c5c9675 to your computer and use it in GitHub Desktop.
Example of creating a BaseTx using Avalanche.js with multiple Secp256k1 inputs and outputs of different assetIDs for the Avalanche network
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
// The setup | |
// AVAX Balance | |
// curl -X POST --data '{ | |
// "jsonrpc":"2.0", | |
// "id" : 1, | |
// "method" :"avm.getBalance", | |
// "params" :{ | |
// "address":"X-EKpEPX56YA1dsaHBsW8X5nGqNSwJ7JrWH", | |
// "assetID": "AVA" | |
// } | |
// }' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/X | |
// {"jsonrpc":"2.0","result":{"balance":"20000","utxoIDs":[{"txID":"2QYFsMGEgNPsy4gy2wFnNMYmVkLQg9wMLmns5kTH3YJv6FiXLi","outputIndex":0}]},"id":1} | |
// | |
// 2pYGetDWyKdHxpFxh2LHeoLNCH6H5vxxCxHQtFnnFaYxLsqtHC Balance | |
// X-Address: X-EKpEPX56YA1dsaHBsW8X5nGqNSwJ7JrWH | |
// assetID: 2pYGetDWyKdHxpFxh2LHeoLNCH6H5vxxCxHQtFnnFaYxLsqtHC | |
// curl -X POST --data '{ | |
// "jsonrpc":"2.0", | |
// "id" : 1, | |
// "method" :"avm.getBalance", | |
// "params" :{ | |
// "address":"X-EKpEPX56YA1dsaHBsW8X5nGqNSwJ7JrWH", | |
// "assetID": "2pYGetDWyKdHxpFxh2LHeoLNCH6H5vxxCxHQtFnnFaYxLsqtHC" | |
// } | |
// }' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/X | |
// {"jsonrpc":"2.0","result":{"balance":"299999999999900","utxoIDs":[{"txID":"dGFvs8JyGLhAUV4N5BY6VotbrqvjqnXfDPk1gdPBVY8HBvnrB","outputIndex":0}]},"id":1} | |
// | |
// imports | |
import {Avalanche, BinTools, AVM, AVMKeyChain, TransferableInput, TransferableOutput, BaseTx, UnsignedTx, SecpInput, SecpOutput, Tx} from "avalanche" | |
import BN from 'bn.js'; | |
import { Buffer } from 'buffer/'; | |
const ip: string = "ip.address.of.node" | |
const port: number = 9650 | |
const protocol: string = "http" | |
const networkId: number = 3 | |
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkId) | |
const avm: AVM = avalanche.AVM() | |
const bintools: BinTools = BinTools.getInstance() | |
const myKeychain: AVMKeyChain = avm.keyChain() | |
// the goods | |
const main = async (): Promise<any> => { | |
const mypk: Buffer = bintools.avaDeserialize("privkey") | |
myKeychain.importKey(mypk) | |
const txid1: Buffer = bintools.avaDeserialize("2QYFsMGEgNPsy4gy2wFnNMYmVkLQg9wMLmns5kTH3YJv6FiXLi") | |
const txid2: Buffer = bintools.avaDeserialize("dGFvs8JyGLhAUV4N5BY6VotbrqvjqnXfDPk1gdPBVY8HBvnrB") | |
// assetid1 is the AVAX assetID | |
const assetid1: Buffer = bintools.avaDeserialize("21d7KVtPrubc5fHr6CGNcgbUb4seUjmZKr35ZX7BZb5iP8pXWA") | |
const assetid2: Buffer = bintools.avaDeserialize("2pYGetDWyKdHxpFxh2LHeoLNCH6H5vxxCxHQtFnnFaYxLsqtHC") | |
// blockchainID is the Denali default subnet's X-Chain ID | |
const blockchainID: Buffer = bintools.avaDeserialize("rrEWX7gc7D9mwcdrdBxBTdqh1a7WDVsMuadhTZgyXfFcRz45L") | |
const addresses: Buffer[] = myKeychain.getAddresses() | |
const ins: TransferableInput[] = [] | |
const outs: TransferableOutput[] = [] | |
const amt1: BN = new BN(20000) | |
const amt2: BN = new BN(299999999999900) | |
const output1: SecpOutput = new SecpOutput(amt1, new BN(1), 1, addresses) | |
const output2: SecpOutput = new SecpOutput(amt2, new BN(1), 1, addresses) | |
const transferableOutput1: TransferableOutput = new TransferableOutput(assetid1, output1) | |
const transferableOutput2: TransferableOutput = new TransferableOutput(assetid2, output2) | |
outs.push(transferableOutput1) | |
outs.push(transferableOutput2) | |
const input1: SecpInput = new SecpInput(amt1) | |
const input2: SecpInput = new SecpInput(amt2) | |
input1.addSignatureIdx(0, addresses[0]) | |
input2.addSignatureIdx(0, addresses[0]) | |
const outputIndex1: Buffer = Buffer.from(bintools.fromBNToBuffer(new BN(0), 4)) | |
const transferableInput1: TransferableInput = new TransferableInput(txid1, outputIndex1, assetid1, input1) | |
const outputIndex2: Buffer = Buffer.from(bintools.fromBNToBuffer(new BN(0), 4)) | |
const transferableInput2: TransferableInput = new TransferableInput(txid2, outputIndex2, assetid2, input2) | |
ins.push(transferableInput1) | |
ins.push(transferableInput2) | |
const baseTx: BaseTx = new BaseTx(networkId, blockchainID, outs, ins) | |
const unsignedTx: UnsignedTx = new UnsignedTx(baseTx) | |
const tx: Tx = unsignedTx.sign(myKeychain) | |
const transactionId: string = await avm.issueTx(tx) | |
console.log(transactionId) | |
// 224FTv4PTTgvdc5T1tioZ45u4NbKXmEqbjdnBkPrGzX3C9LkRE | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment