From 95f709b795c75ed6d15f5e2c68f05384a6b9c8e6 Mon Sep 17 00:00:00 2001 From: Karishma Date: Wed, 26 Jul 2023 08:43:45 +0530 Subject: [PATCH] Add example for Fee Preferences and proxy extrinsic which is a fpass proxy call (#20) * Add example for Fee Preferences and proxy extrinsic which is a fpass proxy call Co-authored-by: Ken Vu <97480229+ken-centrality@users.noreply.github.com> --- examples/substrate/use-feeProxy/README.md | 19 +++++++ .../src/feeProxyWithProxyExtrinsic.ts | 52 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 examples/substrate/use-feeProxy/src/feeProxyWithProxyExtrinsic.ts diff --git a/examples/substrate/use-feeProxy/README.md b/examples/substrate/use-feeProxy/README.md index 425c818..da5b5ea 100644 --- a/examples/substrate/use-feeProxy/README.md +++ b/examples/substrate/use-feeProxy/README.md @@ -23,3 +23,22 @@ Run the command below to execute the example script, passing in a Payment Asset ``` pnpm call src/callWithFeePreferences.ts --paymentAsset= ``` + +### Call with Fee Preferences and proxy extrinsic + +Using the `feeProxy.callWithFeePreferences(paymentAsset, maxPayment, proxyExtrinsic)` extrinsic + +- `paymentAsset` - The token to be used for paying gas fees +- `maxPayment` - The limit of how many tokens will be used to perform the exchange +- `proxyExtrinsic` - The inner call to be performed after the exchange. Here proxy extrinsic is fpass proxy call +- `const proxyExtrinsic = api.tx.futurepass.proxyExtrinsic(futurepassAddress, innerCall);` + +``` +api.tx.feeProxy.callWithFeePreferences(1, 3_000_000, api.tx.system.remark("Hello World")); +``` + +Run the command below to execute the example script, passing in a Payment Asset ID + +``` +pnpm call src/callWithFeePreferences.ts --paymentAsset= +``` diff --git a/examples/substrate/use-feeProxy/src/feeProxyWithProxyExtrinsic.ts b/examples/substrate/use-feeProxy/src/feeProxyWithProxyExtrinsic.ts new file mode 100644 index 0000000..032ef16 --- /dev/null +++ b/examples/substrate/use-feeProxy/src/feeProxyWithProxyExtrinsic.ts @@ -0,0 +1,52 @@ +import { collectArgs } from "@trne/utils/collectArgs"; +import { createKeyring } from "@trne/utils/createKeyring"; +import { filterExtrinsicEvents } from "@trne/utils/filterExtrinsicEvents"; +import { getChainApi } from "@trne/utils/getChainApi"; +import { sendExtrinsic } from "@trne/utils/sendExtrinsic"; +import assert from "assert"; +import { cleanEnv, str } from "envalid"; + +const argv = collectArgs(); + +const env = cleanEnv(process.env, { + CALLER_PRIVATE_KEY: str(), // private key of extrinsic caller +}); + +// This example assumes, there is liquidity pool for paymentAsset AND XrpAsset (100,000,000,000 & 100,000,000,000) +// Also assumes, that the futurepassAddress has payment asset. +export async function main() { + assert("paymentAsset" in argv, "Payment asset ID is required"); + + const api = await getChainApi("porcini"); + const caller = createKeyring(env.CALLER_PRIVATE_KEY); + const fpassAddress = (await api.query.futurepass.holders(caller.address)).toString(); + const { paymentAsset } = argv as unknown as { paymentAsset: number }; + + // can be any extrinsic, using `system.remarkWithEvent` for simplicity + const innerCall = api.tx.system.remarkWithEvent("Hello World"); + + const proxyExtrinsic = api.tx.futurepass.proxyExtrinsic(fpassAddress, innerCall); + const maxPayment = 1000000; + const feeProxiedCall = api.tx.feeProxy.callWithFeePreferences( + paymentAsset, + maxPayment, + proxyExtrinsic + ); + const { result } = await sendExtrinsic(feeProxiedCall, caller, { + log: console, + }); + + const [proxyEvent, remarkEvent] = filterExtrinsicEvents(result.events, [ + "FeeProxy.CallWithFeePreferences", + // depending on what extrinsic call you have, filter out the right event here + "System.Remarked", + ]); + console.log("Extrinsic Result", { + proxy: proxyEvent.toJSON(), + remark: remarkEvent.toJSON(), + }); + + await api.disconnect(); +} + +main();