redeemOnEth()

Initiates transaction to receive tokens from the contract on Ethereum

Method Parameters

tokenBridgeAddress: string

Ethereum bridge contract address

signer: ethers.Signer

ethers js Signer

signedVAA: Uint8Array

Parsed signed VAA

Returns

Returns ethers js ethers.ContractReceipt

Usage

import { ethers } from "ethers";
import algosdk, { Account, Algodv2, assignGroupID, waitForConfirmation } from "algosdk";
import { transferFromAlgorand, nativeToHexString, CHAIN_ID_ETH, getEmitterAddressAlgorand, parseSequenceFromLogAlgorand, redeemOnEth } from "@algo-foundry/messina-sdk";
import { TransactionSignerPair } from "@algo-foundry/messina-sdk/lib/cjs/algorand";
import { getSignedVAA } from "@algo-foundry/messina-sdk/lib/cjs/rpc/getSignedVAAUpdate";

const client: algosdk.Algodv2 = new Algodv2(<ALGO_TOKEN>, <ALGO_ADDRESS>, <ALGOD_PORT>);
const wallet: Account = algosdk.mnemonicToSecretKey(<ALGO_MNEMONIC>)

const provider = new ethers.providers.WebSocketProvider(<ETH_NODE_URL>) as any;   
const signer = new ethers.Wallet(<ETH_PRIVATE_KEY>, provider);

const hexStr = nativeToHexString(
      signer.address,
      CHAIN_ID_ETH
    );

if (!hexStr) {
   throw new Error("Failed to convert to hexStr");
 }    

const transferTxs = await transferFromAlgorand(
      client,
      BigInt(114976173),
      BigInt(114976156),
      wallet.addr,
      BigInt(105449176),
      BigInt(10000000), // AMOUNT_TO_TRANSFER * (10 ^ ASSET_DECIMALS)
      hexStr,
      CHAIN_ID_ETH,
      BigInt(0)
    );

const transferResult = await signSendAndConfirmAlgorand(
      client,
      transferTxs,
      wallet,
      4
    );

const emitterAddr = getEmitterAddressAlgorand(BigInt(114976173));
const sequence = await parseSequenceFromLogAlgorand(transferResult);

let signedVAA = await getSignedVAA(<GUARDIAN_GRPC>, emitterAddr, 'CHAIN_ID_ALGORAND', sequence, 1000, 5);
// @ts-ignore
const vaaBytes = new Uint8Array(signedVAA!.match(/.{1,2}/g)!.map((byte: any) => parseInt(byte, 16)));

const redeem = await redeemOnEth(
      "0xf72a04B891bA46819D83c75687d31f22f6d646c1",
      signer, // Wallet from ethers js
      vaaBytes
    );

async function signSendAndConfirmAlgorand(
  algodClient: Algodv2,
  txs: TransactionSignerPair[],
  wallet: Account,
  waitRounds = 1
) {
  assignGroupID(txs.map((tx) => tx.tx));
  const signedTxns: Uint8Array[] = [];
  for (const tx of txs) {
    if (tx.signer) {
      signedTxns.push(await tx.signer.signTxn(tx.tx));
    } else {
      signedTxns.push(tx.tx.signTxn(wallet.sk));
    }
  }
  await algodClient.sendRawTransaction(signedTxns).do();

  const result = await waitForConfirmation(
    algodClient,
    txs[txs.length - 1].tx.txID(),
    waitRounds
  );
  return result;
}

Last updated