import { Ecanna, BN } from "ecanna/dist" import { TKNVMAPI, KeyChain as TKNVMKeyChain } from "ecanna/dist/apis/tknvm" import { EVMAPI, KeyChain as EVMKeyChain, UnsignedTx, Tx } from "ecanna/dist/apis/evm" import { PrivateKeyPrefix, DefaultLocalGenesisPrivateKey, Defaults, costExportTx } 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 privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}` const xKeychain: TKNVMKeyChain = tknchain.keyChain() const cKeychain: EVMKeyChain = evmchain.keyChain() xKeychain.importKey(privKey) cKeychain.importKey(privKey) const xAddressStrings: string[] = tknchain.keyChain().getAddressStrings() const cAddressStrings: string[] = evmchain.keyChain().getAddressStrings() const tknChainBlockchainIdStr: string = Defaults.network[networkID].TKN.blockchainID const ecnaAssetID: string = Defaults.network[networkID].TKN.ecnaAssetID const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC" 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 => { let balance: BN = await web3.eth.getBalance(cHexAddress) balance = new BN(balance.toString().substring(0, 17)) const baseFeeResponse: string = await evmchain.getBaseFee() const baseFee = new BN(parseInt(baseFeeResponse, 16)) const txcount = await web3.eth.getTransactionCount(cHexAddress) const nonce: number = txcount const locktime: BN = new BN(0) let ecnaAmount: BN = new BN(1e7) let fee: BN = baseFee.div(new BN(1e9)) fee = fee.add(new BN(1e6)) let unsignedTx: UnsignedTx = await evmchain.buildExportTx( ecnaAmount, ecnaAssetID, tknChainBlockchainIdStr, cHexAddress, cAddressStrings[0], xAddressStrings, nonce, locktime, threshold, fee ) const tx: Tx = unsignedTx.sign(cKeychain) const txid: string = await evmchain.issueTx(tx) console.log(`Success! TXID: ${txid}`) } main()