Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Created August 3, 2022 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgcardona/4dc1f9815e5eade8a00f355dd6a38caf to your computer and use it in GitHub Desktop.
Save cgcardona/4dc1f9815e5eade8a00f355dd6a38caf to your computer and use it in GitHub Desktop.
Import AVAX to the X-Chain from the C-Chain
import { Avalanche, BN, Buffer } from "../../src"
import {
AVMAPI,
KeyChain,
UTXOSet,
UnsignedTx,
Tx
} from "../../src/apis/avm"
import { GetUTXOsResponse } from "../../src/apis/avm/interfaces"
import {
PrivateKeyPrefix,
DefaultLocalGenesisPrivateKey,
Defaults,
UnixNow
} from "../../src/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 xKeychain: KeyChain = xchain.keyChain()
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
xKeychain.importKey(privKey)
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()
const cChainBlockchainID: string = Defaults.network[networkID].C.blockchainID
const threshold: number = 1
const locktime: BN = new BN(0)
const asOf: BN = UnixNow()
const memo: Buffer = Buffer.from(
"AVM utility method buildImportTx to import AVAX to the X-Chain from the C-Chain"
)
const main = async (): Promise<any> => {
const avmUTXOResponse: GetUTXOsResponse = await xchain.getUTXOs(
xAddressStrings,
cChainBlockchainID
)
const utxoSet: UTXOSet = avmUTXOResponse.utxos
const unsignedTx: UnsignedTx = await xchain.buildImportTx(
utxoSet,
xAddressStrings,
cChainBlockchainID,
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