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