import { Ecanna, BinTools, BN, Buffer } from "ecanna/dist" import { TKNVMAPI, KeyChain as TKNVMKeyChain } from "ecanna/dist/apis/tknvm" import { EVMAPI, KeyChain as EVMKeyChain, UnsignedTx, Tx, EVMInput, ExportTx, SECPTransferOutput, TransferableOutput } from "ecanna/dist/apis/evm" import { RequestResponseData } from "ecanna/dist/common" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults } 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 evmchain: EVMAPI = ecanna.EVMChain() const bintools: BinTools = BinTools.getInstance() const xKeychain: TKNVMKeyChain = tknchain.keyChain() const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` const cKeychain: EVMKeyChain = evmchain.keyChain() xKeychain.importKey(privKey) cKeychain.importKey(privKey) const xAddresses: Buffer[] = tknchain.keyChain().getAddresses() const cAddresses: Buffer[] = evmchain.keyChain().getAddresses() const tknChainBlockchainIdStr: string = Defaults.network[networkID].TKN.blockchainID const tknChainBlockchainIdBuf: Buffer = bintools.cb58Decode( tknChainBlockchainIdStr ) const evmChainBlockchainIdStr: string = Defaults.network[networkID].EVM.blockchainID const evmChainBlockchainIdBuf: Buffer = bintools.cb58Decode( evmChainBlockchainIdStr ) const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC" const ecnaAssetID: string = Defaults.network[networkID].TKN.ecnaAssetID const ecnaAssetIDBuf: Buffer = bintools.cb58Decode(ecnaAssetID) const evmInputs: EVMInput[] = [] let exportedOuts: TransferableOutput[] = [] const Web3 = require("web3") const path: string = "/ext/bc/STK/rpc" const web3 = new Web3(`${protocol}://${ip}:${port}${path}`) const threshold: number = 1 const main = async (): Promise => { const antAssetIDStr: string = "verma4Pa9biWKbjDGNsTXU47cYCyDSNGSU1iBkxucfVSFVXdv" const antAssetIDBuf: Buffer = bintools.cb58Decode(antAssetIDStr) const antAssetBalanceResponse: RequestResponseData = await evmchain.callMethod( "eth_getAssetBalance", [cHexAddress, "latest", antAssetIDStr], "ext/bc/STK/rpc" ) const antAssetBalance: number = parseInt( antAssetBalanceResponse.data.result, 16 ) let ecnaBalance: BN = await web3.eth.getBalance(cHexAddress) ecnaBalance = new BN(ecnaBalance.toString().substring(0, 17)) const fee: BN = evmchain.getDefaultTxFee() const txcount = await web3.eth.getTransactionCount(cHexAddress) const nonce: number = txcount const locktime: BN = new BN(0) let evmInput: EVMInput = new EVMInput( cHexAddress, ecnaBalance, ecnaAssetID, nonce ) evmInput.addSignatureIdx(0, cAddresses[0]) evmInputs.push(evmInput) evmInput = new EVMInput(cHexAddress, antAssetBalance, antAssetIDStr, nonce) evmInput.addSignatureIdx(0, cAddresses[0]) evmInputs.push(evmInput) let secpTransferOutput: SECPTransferOutput = new SECPTransferOutput( ecnaBalance.sub(fee), xAddresses, locktime, threshold ) let transferableOutput: TransferableOutput = new TransferableOutput( ecnaAssetIDBuf, secpTransferOutput ) exportedOuts.push(transferableOutput) secpTransferOutput = new SECPTransferOutput( new BN(antAssetBalance), xAddresses, locktime, threshold ) transferableOutput = new TransferableOutput(antAssetIDBuf, secpTransferOutput) exportedOuts.push(transferableOutput) exportedOuts = exportedOuts.sort(TransferableOutput.comparator()) const exportTx: ExportTx = new ExportTx( networkID, evmChainBlockchainIdBuf, tknChainBlockchainIdBuf, evmInputs, exportedOuts ) const unsignedTx: UnsignedTx = new UnsignedTx(exportTx) const tx: Tx = unsignedTx.sign(cKeychain) const txid: string = await evmchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()