Created
October 20, 2023 17:41
-
-
Save cgcardona/022ae44c42638aa253f5c1b23e1b6162 to your computer and use it in GitHub Desktop.
Run via: `ts-node examples/avm/buildBaseTx-avax.ts`
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, BN, Buffer } from "avalanche/dist" | |
import { | |
AVMAPI, | |
KeyChain, | |
UTXOSet, | |
UnsignedTx, | |
Tx | |
} from "avalanche/dist/apis/avm" | |
import { | |
GetBalanceResponse, | |
GetUTXOsResponse | |
} from "avalanche/dist/apis/avm/interfaces" | |
import { Defaults } from "avalanche/dist/utils" | |
import { | |
PrivateKeyPrefix, | |
DefaultLocalGenesisPrivateKey, | |
UnixNow | |
} from "avalanche/dist/utils" | |
const ip: string = "api.avax-test.network/ext/bc/X" | |
const port: number = 443 | |
const protocol: string = "https" | |
const networkID: number = 5 | |
const xBlockchainID: string = Defaults.network[networkID].X.blockchainID | |
const avaxAssetID = Defaults.network[networkID].X.avaxAssetID as string | |
const avalanche: Avalanche = new Avalanche( | |
ip, | |
port, | |
protocol, | |
networkID, | |
xBlockchainID | |
) | |
const xchain: AVMAPI = avalanche.XChain() | |
const xKeychain: KeyChain = xchain.keyChain() | |
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` | |
xKeychain.importKey(privKey) | |
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings() | |
const asOf: BN = UnixNow() | |
const threshold: number = 1 | |
const locktime: BN = new BN(0) | |
const memo: Buffer = Buffer.from("AVM utility method buildBaseTx to send AVAX") | |
const fee: BN = xchain.getDefaultTxFee() | |
const main = async (): Promise<any> => { | |
const getBalanceResponse: GetBalanceResponse = await xchain.getBalance( | |
xAddressStrings[0], | |
avaxAssetID | |
) | |
const balance: BN = new BN(getBalanceResponse.balance) | |
const avmUTXOResponse: GetUTXOsResponse = await xchain.getUTXOs( | |
xAddressStrings | |
) | |
const utxoSet: UTXOSet = avmUTXOResponse.utxos | |
const amount: BN = balance.sub(fee) | |
const unsignedTx: UnsignedTx = await xchain.buildBaseTx( | |
utxoSet, | |
amount, | |
avaxAssetID, | |
xAddressStrings, | |
xAddressStrings, | |
xAddressStrings, | |
memo, | |
asOf, | |
locktime, | |
threshold | |
) | |
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