Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active October 11, 2021 09:59
Show Gist options
  • Save cgcardona/147f47c88e92de061599400eb06d7e7f to your computer and use it in GitHub Desktop.
Save cgcardona/147f47c88e92de061599400eb06d7e7f to your computer and use it in GitHub Desktop.
// Manually build an EVM ExportTx which eports AVAX
import {
Avalanche,
BinTools,
BN,
Buffer
} from "avalanche"
import {
EVMAPI,
ExportTx,
KeyChain,
EVMInput,
TransferableOutput,
SECPTransferOutput,
UnsignedTx,
Tx
} from "avalanche/dist/apis/evm"
import {
AVMAPI,
KeyChain as AVMKeyChain
} from "avalanche/dist/apis/avm"
import {
Defaults,
PrivateKeyPrefix,
DefaultLocalGenesisPrivateKey
} from "avalanche/dist/utils"
const ip: string = "localhost"
const port: number = 9650
const protocol: string = "http"
const networkID: number = 12345
const avalanche: Avalanche = new Avalanche(ip, port, protocol, networkID)
const xchain: AVMAPI = avalanche.XChain()
const cchain: EVMAPI = avalanche.CChain()
const bintools: BinTools = BinTools.getInstance()
const xKeychain: AVMKeyChain = xchain.keyChain()
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
const cKeychain: KeyChain = cchain.keyChain()
xKeychain.importKey(privKey)
cKeychain.importKey(privKey)
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
const cAddresses: Buffer[] = cchain.keyChain().getAddresses()
const xChainBlockchainIdStr: string = Defaults.network['12345'].X.blockchainID
const xChainBlockchainIdBuf: Buffer = bintools.cb58Decode(xChainBlockchainIdStr)
const cChainBlockchainIdStr: string = Defaults.network['12345'].C.blockchainID
const cChainBlockchainIdBuf: Buffer = bintools.cb58Decode(cChainBlockchainIdStr)
const cHexAddress: string = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
const evmInputs: EVMInput[] = []
const exportedOuts: TransferableOutput[] = []
const Web3 = require('web3');
const path: string = '/ext/bc/C/rpc'
const web3 = new Web3(`${protocol}://${ip}:${port}${path}`)
const main = async (): Promise<any> => {
const avaxAssetIDBuf: Buffer = await xchain.getAVAXAssetID()
const avaxAssetIDStr: string = bintools.cb58Encode(avaxAssetIDBuf)
const threshold: number = 1
const avaxAmount: BN = new BN(4000001000000)
const fee: BN = new BN(1000000)
const txcount = await web3.eth.getTransactionCount(cHexAddress)
const nonce: number = txcount;
const locktime: BN = new BN(0)
const evmInput: EVMInput = new EVMInput(cHexAddress, avaxAmount, avaxAssetIDStr, nonce)
evmInput.addSignatureIdx(0, cAddresses[0])
evmInputs.push(evmInput)
const secpTransferOutput: SECPTransferOutput = new SECPTransferOutput(avaxAmount.sub(fee), xAddresses, locktime, threshold)
const transferableOutput: TransferableOutput = new TransferableOutput(avaxAssetIDBuf, secpTransferOutput)
exportedOuts.push(transferableOutput)
const exportTx: ExportTx = new ExportTx(
networkID,
cChainBlockchainIdBuf,
xChainBlockchainIdBuf,
evmInputs,
exportedOuts
)
const unsignedTx: UnsignedTx = new UnsignedTx(exportTx)
const tx: Tx = unsignedTx.sign(cKeychain)
const id: string = await cchain.issueTx(tx)
console.log(id)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment