|
| 1 | +import { coston2 } from "@flarenetwork/flare-wagmi-periphery-package"; |
| 2 | +import { parseEventLogs, type Address } from "viem"; |
| 3 | +import { account, publicClient, walletClient } from "./utils/client"; |
| 4 | +import { getContractAddressByName } from "./utils/flare-contract-registry"; |
| 5 | + |
| 6 | +// 1. Amount to redeem in UBA (underlying base units; 1 XRP = 1_000_000 UBA). |
| 7 | +const REDEEM_AMOUNT_UBA = 5000000n; |
| 8 | +// 2. Redeemer underlying (XRPL) address that will receive the redeemed XRP. |
| 9 | +const REDEEMER_UNDERLYING_ADDRESS_STRING = "rSHYuiEvsYsKR8uUHhBTuGP5zjRcGt4nm"; |
| 10 | +// 3. Executor (not used here, so the zero address). |
| 11 | +const EXECUTOR_ZERO_ADDRESS: Address = "0x0000000000000000000000000000000000000000"; |
| 12 | +// 4. XRPL destination tag the agent must include in the redemption payment |
| 13 | +// (must fit in 32 bits; e.g. an exchange deposit tag). |
| 14 | +const REDEMPTION_DESTINATION_TAG = 72n; |
| 15 | + |
| 16 | +async function main() { |
| 17 | + // 5. Resolve the AssetManagerFXRP address from the Flare Contract Registry. |
| 18 | + const assetManagerAddress = await getContractAddressByName("AssetManagerFXRP"); |
| 19 | + console.log("AssetManagerFXRP address:", assetManagerAddress, "\n"); |
| 20 | + |
| 21 | + // 6. Read the protocol-wide minimum redemption amount and assert that the |
| 22 | + // requested amount is above it. Smaller redemptions are rejected on-chain. |
| 23 | + const minimumRedeemAmountUBA = await publicClient.readContract({ |
| 24 | + address: assetManagerAddress, |
| 25 | + abi: coston2.iAssetManagerAbi, |
| 26 | + functionName: "minimumRedeemAmountUBA", |
| 27 | + }); |
| 28 | + console.log("minimumRedeemAmountUBA:", minimumRedeemAmountUBA.toString(), "\n"); |
| 29 | + console.log("Requested redeem amount UBA:", REDEEM_AMOUNT_UBA.toString(), "\n"); |
| 30 | + console.log("Redemption destination tag:", REDEMPTION_DESTINATION_TAG.toString(), "\n"); |
| 31 | + |
| 32 | + if (REDEEM_AMOUNT_UBA < minimumRedeemAmountUBA) { |
| 33 | + throw new Error( |
| 34 | + `Redeem amount (${REDEEM_AMOUNT_UBA.toString()}) must be greater than minimumRedeemAmountUBA (${minimumRedeemAmountUBA.toString()}).` |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + // 7. Simulate the redeemWithTag call to validate args and produce a request |
| 39 | + // that walletClient can submit. |
| 40 | + const { request } = await publicClient.simulateContract({ |
| 41 | + account, |
| 42 | + address: assetManagerAddress, |
| 43 | + abi: coston2.iAssetManagerAbi, |
| 44 | + functionName: "redeemWithTag", |
| 45 | + args: [ |
| 46 | + REDEEM_AMOUNT_UBA, |
| 47 | + REDEEMER_UNDERLYING_ADDRESS_STRING, |
| 48 | + EXECUTOR_ZERO_ADDRESS, |
| 49 | + REDEMPTION_DESTINATION_TAG, |
| 50 | + ], |
| 51 | + }); |
| 52 | + |
| 53 | + // 8. Submit the redemption request transaction on Flare. |
| 54 | + const txHash = await walletClient.writeContract(request); |
| 55 | + console.log("redeemWithTag tx hash:", txHash, "\n"); |
| 56 | + |
| 57 | + // 9. Wait for the transaction receipt. |
| 58 | + const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash }); |
| 59 | + console.log("redeemWithTag status:", receipt.status, "\n"); |
| 60 | + |
| 61 | + // 10. Decode RedemptionWithTagRequested events from the receipt logs and |
| 62 | + // pick the one that belongs to this redeemer. |
| 63 | + const redemptionLogs = parseEventLogs({ |
| 64 | + abi: coston2.iAssetManagerAbi, |
| 65 | + eventName: "RedemptionWithTagRequested", |
| 66 | + logs: receipt.logs, |
| 67 | + }); |
| 68 | + |
| 69 | + const redemptionEvent = redemptionLogs.find( |
| 70 | + (log) => log.args.redeemer.toLowerCase() === account.address.toLowerCase(), |
| 71 | + ); |
| 72 | + |
| 73 | + if (!redemptionEvent) { |
| 74 | + throw new Error("RedemptionWithTagRequested event not found for this transaction and redeemer"); |
| 75 | + } |
| 76 | + |
| 77 | + console.log("RedemptionWithTagRequested event:", redemptionEvent, "\n"); |
| 78 | +} |
| 79 | + |
| 80 | +void main() |
| 81 | + .then(() => process.exit(0)) |
| 82 | + .catch((error) => { |
| 83 | + console.error(error); |
| 84 | + process.exit(1); |
| 85 | + }); |
0 commit comments