import { ethers } from "ethers";
import algosdk, { Account, Algodv2, assignGroupID, waitForConfirmation } from "algosdk";
import { approveEth, transferFromEth, CHAIN_ID_ALGORAND, getEmitterAddressEth, parseSequenceFromLogEth, redeemOnAlgorand } from "@algo-foundry/messina-sdk";
import { getSignedVAA } from "@algo-foundry/messina-sdk/lib/cjs/rpc/getSignedVAAUpdate";
import { TransactionSignerPair } from "@algo-foundry/messina-sdk/lib/cjs/algorand";
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 allowance: ethers.BigNumber = await getAllowanceEth("0xf72a04B891bA46819D83c75687d31f22f6d646c1","0xbF0482884043B7ea1eB55516770C217C77CC7A48", signer);
if(allowance.lt(AMOUNT_TO_TRANSFER)){
await approveEth(
"0xf72a04B891bA46819D83c75687d31f22f6d646c1",
"0xbF0482884043B7ea1eB55516770C217C77CC7A48",
signer,
AMOUNT_TO_TRANSFER
);
}
const receipt: ethers.ContractReceipt = await transferFromEth(
"0xf72a04B891bA46819D83c75687d31f22f6d646c1",
signer,
"0xbF0482884043B7ea1eB55516770C217C77CC7A48",
BigInt(10000000), // AMOUNT_TO_TRANSFER * (10 ^ ASSET_DECIMALS)
CHAIN_ID_ALGORAND,
algosdk.decodeAddress(wallet.addr).publicKey
);
const emitterAddr = getEmitterAddressEth("0xf72a04B891bA46819D83c75687d31f22f6d646c1");
const sequence: string = parseSequenceFromLogEth(receipt, "0xE4DF23D087ef706a536527a41bef769a43656C91");
const signedVAA = await getSignedVAA(<GUARDIAN_GRPC>, emitterAddr, 'CHAIN_ID_ETHEREUM', sequence, 1000, 20)
// @ts-ignore
const vaaBytes = new Uint8Array(signedVAA!.match(/.{1,2}/g)!.map((byte: any) => parseInt(byte, 16)))
const redeemTxs = await redeemOnAlgorand(
client,
BigInt(114976173),
BigInt(114976156),
vaaBytes,
wallet.addr
);
await signSendAndConfirmAlgorand(client, redeemTxs, wallet, 4);
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;
}