-
-
Save ivanmolto/93c5dcc6e309e7d7a83e5738af8b8d98 to your computer and use it in GitHub Desktop.
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 { | |
Avalanche, | |
BinTools, | |
BN, | |
Buffer | |
} from "../../src"; | |
import { | |
AVMAPI, | |
KeyChain as AVMKeyChain, | |
SECPTransferOutput, | |
SECPTransferInput, | |
TransferableOutput, | |
TransferableInput, | |
UTXOSet, | |
UTXO, | |
AmountOutput, | |
UnsignedTx, | |
Tx, | |
BaseTx | |
} from "../../src/apis/avm" | |
import { Defaults } from "../../src/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() | |
const privKey: string = "PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN" | |
xKeychain.importKey(privKey) | |
const xAddresses: Buffer[] = xchain.keyChain().getAddresses() | |
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings() | |
const blockchainid: string = Defaults.network['12345'].X.blockchainID | |
const outputs: TransferableOutput[] = [] | |
const inputs: TransferableInput[] = [] | |
const fee: BN = xchain.getDefaultTxFee() | |
const threshold: number = 1 | |
const locktime: BN = new BN(0) | |
const memo: Buffer = Buffer.from("AVM manual BaseTx to send AVAX and ANT") | |
// Uncomment for codecID 00 01 | |
// const codecID: number = 1 | |
const main = async (): Promise<any> => { | |
const avaxAssetID: Buffer = await xchain.getAVAXAssetID() | |
const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings) | |
const utxoSet: UTXOSet = avmUTXOResponse.utxos | |
const utxos: UTXO[] = utxoSet.getAllUTXOs() | |
utxos.forEach((utxo: UTXO) => { | |
if(utxo.getOutput().getTypeID() != 6) { | |
const amountOutput: AmountOutput = utxo.getOutput() as AmountOutput | |
const amt: BN = amountOutput.getAmount().clone() | |
const txid: Buffer = utxo.getTxID() | |
const outputidx: Buffer = utxo.getOutputIdx() | |
const assetID: Buffer = utxo.getAssetID(); | |
if(assetID.toString('hex') === avaxAssetID.toString('hex')) { | |
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt.sub(fee), xAddresses, locktime, threshold) | |
// Uncomment for codecID 00 01 | |
// secpTransferOutput.setCodecID(codecID) | |
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetID, secpTransferOutput) | |
outputs.push(transferableOutput) | |
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) | |
// Uncomment for codecID 00 01 | |
// secpTransferInput.setCodecID(codecID) | |
secpTransferInput.addSignatureIdx(0, xAddresses[0]) | |
const input: TransferableInput = new TransferableInput(txid, outputidx, avaxAssetID, secpTransferInput) | |
inputs.push(input) | |
} else { | |
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(amt, xAddresses, locktime, threshold) | |
// Uncomment for codecID 00 01 | |
// secpTransferOutput.setCodecID(codecID) | |
const transferableOutput: TransferableOutput = new TransferableOutput(assetID, secpTransferOutput) | |
outputs.push(transferableOutput) | |
const secpTransferInput: SECPTransferInput = new SECPTransferInput(amt) | |
// Uncomment for codecID 00 01 | |
// secpTransferInput.setCodecID(codecID) | |
secpTransferInput.addSignatureIdx(0, xAddresses[0]) | |
const input: TransferableInput = new TransferableInput(txid, outputidx, assetID, secpTransferInput) | |
inputs.push(input) | |
} | |
} | |
}) | |
const baseTx: BaseTx = new BaseTx ( | |
networkID, | |
bintools.cb58Decode(blockchainid), | |
outputs, | |
inputs, | |
memo | |
) | |
// Uncomment for codecID 00 01 | |
// baseTx.setCodecID(codecID) | |
const unsignedTx: UnsignedTx = new UnsignedTx(baseTx) | |
const tx: Tx = unsignedTx.sign(xKeychain) | |
const txid: string = await xchain.issueTx(tx) | |
console.log(`Success! TXID: ${txid}`) | |
} | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment