Utilizing JUNEO-JS to do cross chain transaction

Raven Arts - Sep 1 - - Dev Community

Follow these steps to perform a cross-chain transaction using the JUNEO-JS library.

Create a New Node.js Project
First, create a new Node.js project by running the following command in your terminal:

mkdir juneo-project && cd juneo-project && npm init -y && npm install typescript juneojs && npx tsc --init
Enter fullscreen mode Exit fullscreen mode

Set Up Environment Variables
Next, create an environment file to securely store your mnemonic. Use the following command:

nano .env
Enter fullscreen mode Exit fullscreen mode

Then, add your mnemonic to the .env file like this:

MNEMONIC=yourmnemonichere
Enter fullscreen mode Exit fullscreen mode

Create a Cross-Chain Transaction Script
Create a new TypeScript file to handle the cross-chain transaction:

nano crossJvmToPlatformChain.ts
Enter fullscreen mode Exit fullscreen mode

Copy and paste the following code into the crossJvmToPlatformChain.ts file:

import * as dotenv from 'dotenv'
import {
    CrossOperation,
    type ExecutableOperation,
    type JEVMBlockchain,
    type JVMBlockchain,
    MCNAccount,
    MCNProvider,
    type OperationSummary,
    PlatformBlockchain,
    SocotraNetwork,
} from 'juneojs'

dotenv.config()
async function main() {
    const provider: MCNProvider = new MCNProvider(SocotraNetwork)
    const account: MCNAccount = provider.recoverAccount(process.env.MNEMONIC!)

    // the chain which we will perform the cross from
    const sourceChain: JEVMBlockchain = provider.juneChain

    // the chain we will perform the cross to
    const destinationChain: PlatformBlockchain = provider.platformChain

    // asset id of JUNE Chain is JUNE asset id
    const assetId: string = sourceChain.assetId

    const amount = BigInt(1_000_000_000) // 1 JUNE

    // we instantiate a cross operation that we want to perform
    const cross = new CrossOperation(
        sourceChain,
        destinationChain,
        assetId,
        amount,
    )

    // estimate the operation
    const summary: OperationSummary = await account.estimate(cross)
    console.log(summary.fees)

    // execute the operation
    await account.execute(summary)
    const executable: ExecutableOperation = summary.getExecutable()

    // the receipts should contain multiple transaction ids
    // that were performed to complete the cross operation
    console.log(executable.receipts)
}

main().catch((error) => {
    console.error(error)
    process.exitCode = 1
})
Enter fullscreen mode Exit fullscreen mode

Save the file by pressing Ctrl + O, then exit with Ctrl + X.

Execute the Script
Finally, run the script to perform the cross-chain transaction:

ts-node crossJvmToPlatformChain.ts
Enter fullscreen mode Exit fullscreen mode

Example Output

Upon successful execution, you should see output similar to the following:

[
  BaseFeeData {
    chain: JEVMBlockchain {
      name: 'JUNE-Chain',
      id: 'BUDQJ63154EiJZwwvukRB1tX3yQCDQdoEYYuCNKEruQ9MjRs4',
      vm: [ChainVM],
      asset: [JEVMGasToken],
      assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5',
      aliases: [Array],
      registeredAssets: [Map],
      chainId: 101003n,
      baseFee: 144000000000n,
      ethProvider: JsonRpcProvider {},
      jrc20Assets: [Array],
      wrappedAsset: [WrappedAsset],
      contractManager: [ContractManager]
    },
    asset: JEVMGasToken {
      type: 'gas',
      assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5',
      name: 'JUNE',
      symbol: 'JUNE',
      decimals: 18,
      nativeAsset: [JNTAsset]
    },
    amount: 1617120000000000n,
    type: 'Export transaction',
    assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5',
    spending: BaseSpending {
      chain: [JEVMBlockchain],
      amount: 1617120000000000n,
      assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5'
    }
  },
  BaseFeeData {
    chain: PlatformBlockchain {
      name: 'Platform-Chain',
      id: '11111111111111111111111111111111LpoYY',
      vm: [ChainVM],
      asset: [JNTAsset],
      assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5',
      aliases: [Array],
      registeredAssets: [Map],
      stakeConfig: [StakeConfig],
      rewardConfig: [RewardConfig],
      rewardCalculator: [RewardCalculator]
    },
    asset: JNTAsset {
      type: 'jnt',
      assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5',
      name: 'JUNE',
      symbol: 'JUNE',
      decimals: 9,
      mintable: false
    },
    amount: 10000000n,
    type: 'Import transaction',
    assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5',
    spending: BaseSpending {
      chain: [PlatformBlockchain],
      amount: 10000000n,
      assetId: 'HviVNFzh2nCqyi7bQxw6pt5fUPjZC8r3DCDrt7mRmScZS2zp5'
    }
  }
]
[
  TransactionReceipt {
    chainId: 'BUDQJ63154EiJZwwvukRB1tX3yQCDQdoEYYuCNKEruQ9MjRs4',
    transactionType: 'Export transaction',
    transactionStatus: 'Accepted',
    transactionId: '27KkRyMMrJKvaCNetNQ7Cba3tJABP2nRcsb1aFZm6AJpGsEeLZ'
  },
  TransactionReceipt {
    chainId: '11111111111111111111111111111111LpoYY',
    transactionType: 'Import transaction',
    transactionStatus: 'Committed',
    transactionId: '2htbcJB44LASeoHqwQNMRT5hBRvmEuLpjaJkGP4pvw5vjvEcKr'
  }
]
Enter fullscreen mode Exit fullscreen mode
.
Terabox Video Player