Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created January 25, 2021 23:29
Show Gist options
  • Save cgcardona/a1637ecfbf3d73b65c063c6601de4a33 to your computer and use it in GitHub Desktop.
Save cgcardona/a1637ecfbf3d73b65c063c6601de4a33 to your computer and use it in GitHub Desktop.
import {
Avalanche,
BinTools,
BN,
Buffer
} from "avalanche"
import {
AVMAPI,
KeyChain as AVMKeyChain,
SECPTransferOutput,
SECPTransferInput,
TransferableOutput,
TransferableInput,
UTXOSet,
UTXO,
AmountOutput,
UnsignedTx,
Tx,
BaseTx
} from "avalanche/dist/apis/avm"
import {
iAVMUTXOResponse
} from "avalanche/dist/apis/avm/interfaces"
import { Defaults } from "avalanche/dist/utils"
const ip: string = "localhost"
const port: number = 9650
const protocol: string = "http"
const networkID: number = 12345
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
const xchain: AVMAPI = avalanche.XChain()
const bintools: BinTools = BinTools.getInstance()
const xKeychain: AVMKeyChain = xchain.keyChain()
let privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN"
// X-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u
xKeychain.importKey(privKey)
privKey = "PrivateKey-R6e8f5QSa89DjpvL9asNdhdJ4u8VqzMJStPV8VVdDmLgPd8a4"
// X-local15s7p7mkdev0uajrd0pzxh88kr8ryccztek57g0
xKeychain.importKey(privKey)
privKey = "PrivateKey-24b2s6EqkBp9bFG5S3Xxi4bjdxFqeRk56ck7QdQArVbwKkAvxz"
// X-local1aekly2mwnsz6lswd6u0jqvd9u6yddt58duwcc9
xKeychain.importKey(privKey)
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
console.log(xAddressStrings)
const blockchainid: string = Defaults.network['12345'].X.blockchainID
const outputs: TransferableOutput[] = []
const inputs: TransferableInput[] = []
const fee: BN = xchain.getDefaultTxFee()
const threshold: number = 3
const locktime: BN = new BN(0)
const memo: Buffer = bintools.stringToBuffer("AVM manual BaseTx to send AVAX")
const main = async (): Promise<any> => {
const avaxAssetID: Buffer = await xchain.getAVAXAssetID()
const getBalanceResponse: any = await xchain.getBalance(xAddressStrings[0], bintools.cb58Encode(avaxAssetID))
const balance: BN = new BN(getBalanceResponse['balance'])
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(balance.sub(fee), xAddresses, locktime, threshold)
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput)
outputs.push(transferableOutput)
const avmUTXOResponse: any= await xchain.getUTXOs(xAddressStrings)
const utxoSet: UTXOSet = avmUTXOResponse.utxos
const utxos: UTXO[] = utxoSet.getAllUTXOs()
utxos.forEach((utxo: UTXO) => {
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput
const amt: BN = amountOutput.getAmount().clone()
const txid: Buffer = utxo.getTxID()
const outputidx: Buffer = utxo.getOutputIdx()
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt)
secpTransferInput.addSignatureIdx(0, xAddresses[0])
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput)
inputs.push(input)
})
const baseTx: BaseTx = new BaseTx (
networkID,
bintools.cb58Decode(blockchainid),
outputs,
inputs,
memo
)
const unsignedTx: UnsignedTx = new UnsignedTx(baseTx)
const tx: Tx = unsignedTx.sign(xKeychain)
const id: string = await xchain.issueTx(tx)
console.log(id)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment