import { Lux, BN, Buffer } from "lux/dist" import { XVMAPI, KeyChain as XVMKeyChain } from "lux/dist/apis/xvm" import { PlatformVMAPI, KeyChain, UTXOSet, UnsignedTx, Tx } from "lux/dist/apis/platformvm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, UnixNow } from "lux/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const lux: Lux = new Lux(ip, port, protocol, networkID) const xchain: XVMAPI = lux.XChain() const pchain: PlatformVMAPI = lux.PChain() const xKeychain: XVMKeyChain = xchain.keyChain() const pKeychain: KeyChain = 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 cChainBlockchainID: string = Defaults.network[networkID].C.blockchainID const fee: BN = pchain.getDefaultTxFee() const threshold: number = 1 const locktime: BN = new BN(0) const memo: Buffer = Buffer.from( "PlatformVM utility method buildExportTx to export LUX from the P-Chain to the C-Chain" ) const asOf: BN = UnixNow() const main = async (): Promise => { const getBalanceResponse: any = await pchain.getBalance(pAddressStrings[0]) const unlocked: BN = new BN(getBalanceResponse.unlocked) const platformVMUTXOResponse: any = await pchain.getUTXOs(pAddressStrings) const utxoSet: UTXOSet = platformVMUTXOResponse.utxos const unsignedTx: UnsignedTx = await pchain.buildExportTx( utxoSet, unlocked.sub(fee), cChainBlockchainID, xAddressStrings, pAddressStrings, pAddressStrings, memo, asOf, locktime, threshold ) const tx: Tx = unsignedTx.sign(pKeychain) const txid: string = await pchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()