Skip to content

Commit

Permalink
Add example for Fee Preferences and proxy extrinsic which is a fpass …
Browse files Browse the repository at this point in the history
…proxy call (#20)

* Add example for Fee Preferences and proxy extrinsic which is a fpass proxy call


Co-authored-by: Ken Vu <[email protected]>
  • Loading branch information
KarishmaBothara and ken-futureverse authored Jul 26, 2023
1 parent fff0534 commit 95f709b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/substrate/use-feeProxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@ Run the command below to execute the example script, passing in a Payment Asset
```
pnpm call src/callWithFeePreferences.ts --paymentAsset=<Payment Asset ID>
```

### 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=<Payment Asset ID>
```
52 changes: 52 additions & 0 deletions examples/substrate/use-feeProxy/src/feeProxyWithProxyExtrinsic.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 95f709b

Please sign in to comment.