import { Ecanna, BN, Buffer } from "ecanna/dist" import { TKNVMAPI, KeyChain as TKNVMKeyChain, UTXOSet, UnsignedTx, Tx } from "ecanna/dist/apis/tknvm" import { GetBalanceResponse, GetUTXOsResponse } from "ecanna/dist/apis/tknvm/interfaces" import { KeyChain as StakeVMKeyChain, StakeVMAPI } from "ecanna/dist/apis/stakevm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, UnixNow } from "ecanna/dist/utils" const ip: string = "localhost" const port: number = 9650 const protocol: string = "http" const networkID: number = 1337 const ecanna: Ecanna = new Ecanna(ip, port, protocol, networkID) const tknchain: TKNVMAPI = ecanna.TKNChain() const stkchain: StakeVMAPI = ecanna.STKChain() const xKeychain: TKNVMKeyChain = tknchain.keyChain() const pKeychain: StakeVMKeyChain = stkchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` xKeychain.importKey(privKey) pKeychain.importKey(privKey) const xAddressStrings: string[] = tknchain.keyChain().getAddressStrings() const pAddressStrings: string[] = stkchain.keyChain().getAddressStrings() const stkChainBlockchainID: string = Defaults.network[networkID].STK.blockchainID const ecnaAssetID: string = Defaults.network[networkID].TKN.ecnaAssetID const locktime: BN = new BN(0) const asOf: BN = UnixNow() const memo: Buffer = Buffer.from( "TKNVM utility method buildExportTx to export ECNA to the STK-Chain from the TKN-Chain" ) const fee: BN = tknchain.getDefaultTxFee() const main = async (): Promise => { const tknvmUTXOResponse: GetUTXOsResponse = await tknchain.getUTXOs( xAddressStrings ) const utxoSet: UTXOSet = tknvmUTXOResponse.utxos const getBalanceResponse: GetBalanceResponse = await tknchain.getBalance( xAddressStrings[0], ecnaAssetID ) const balance: BN = new BN(getBalanceResponse.balance) const amount: BN = balance.sub(fee) const unsignedTx: UnsignedTx = await tknchain.buildExportTx( utxoSet, amount, stkChainBlockchainID, pAddressStrings, xAddressStrings, xAddressStrings, memo, asOf, locktime ) const tx: Tx = unsignedTx.sign(xKeychain) const txid: string = await tknchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()