import { Avalanche, BN, Buffer } from "avalanche/dist" import { AVMAPI, KeyChain as AVMKeyChain, UTXOSet, UnsignedTx, Tx } from "avalanche/dist/apis/avm" import { GetBalanceResponse, GetUTXOsResponse } from "avalanche/dist/apis/avm/interfaces" import { KeyChain as PlatformVMKeyChain, PlatformVMAPI } from "avalanche/dist/apis/platformvm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, UnixNow } from "avalanche/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID) const xchain: AVMAPI = avalanche.XChain() const pchain: PlatformVMAPI = avalanche.PChain() const xKeychain: AVMKeyChain = xchain.keyChain() const pKeychain: PlatformVMKeyChain = pchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` xKeychain.importKey(privKey) pKeychain.importKey(privKey) const xAddressStrings: string[] = xchain.keyChain().getAddressStrings() const pAddressStrings: string[] = pchain.keyChain().getAddressStrings() const pChainBlockchainID: string = Defaults.network[networkID].P.blockchainID const avaxAssetID: string = Defaults.network[networkID].X.avaxAssetID const locktime: BN = new BN(0) const asOf: BN = UnixNow() const memo: Buffer = Buffer.from( "AVM utility method buildExportTx to export AVAX to the P-Chain from the X-Chain" ) const fee: BN = xchain.getDefaultTxFee() const main = async (): Promise => { const avmUTXOResponse: GetUTXOsResponse = await xchain.getUTXOs( xAddressStrings ) const utxoSet: UTXOSet = avmUTXOResponse.utxos const getBalanceResponse: GetBalanceResponse = await xchain.getBalance( xAddressStrings[0], avaxAssetID ) const balance: BN = new BN(getBalanceResponse.balance) const amount: BN = balance.sub(fee) const unsignedTx: UnsignedTx = await xchain.buildExportTx( utxoSet, amount, pChainBlockchainID, pAddressStrings, xAddressStrings, xAddressStrings, memo, asOf, locktime ) const tx: Tx = unsignedTx.sign(xKeychain) const txid: string = await xchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()