Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feeProxy.callWithFeePreferences that wraps around xrplBridge.withdrawXrp #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/substrate/use-feeProxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ pnpm call:callProxyExtrinsic
# `feeProxy.callWithFeePreferences` that wraps around `futurepass.proxyExtrinsic` then `emv.call`
pnpm call:callProxyExtrinsicEVMCall

# `feeProxy.callWithFeePreferences` that wraps around `xrplBridge.withdrawXrp`
pnpm call:callXRPLBridgeWithdraw

```
3 changes: 2 additions & 1 deletion examples/substrate/use-feeProxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"call:callProxyExtrinsic": "pnpm call src/callProxyExtrinsic.ts",
"call:callBatchAll": "pnpm call src/callBatchAll.ts",
"call:callEVMCall": "pnpm call src/callEVMCall.ts",
"call:callProxyExtrinsicEVMCall": "pnpm call src/callProxyExtrinsicEVMCall.ts"
"call:callProxyExtrinsicEVMCall": "pnpm call src/callProxyExtrinsicEVMCall.ts",
"call:callXRPLBridgeWithdraw": "pnpm call src/callXRPLBridgeWithdraw.ts"
}
}
93 changes: 93 additions & 0 deletions examples/substrate/use-feeProxy/src/callXRPLBridgeWithdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { filterExtrinsicEvents } from "@trne/utils/filterExtrinsicEvents";
import { formatEventData } from "@trne/utils/formatEventData";
import { ROOT_ASSET_ID, XRP_ASSET_ID } from "@trne/utils/porcini-assets";
import { sendExtrinsic } from "@trne/utils/sendExtrinsic";
import { withChainApi } from "@trne/utils/withChainApi";

interface AmountsIn {
Ok: [number, number];
}

/**
* Use `feeProxy.callWithFeePreferences` to trigger `xrplBridge.withdrawXrp` call, and pay gas
* in ROOT token.
*
* Assumes the caller has ROOT balance.
*/
withChainApi("porcini", async (api, caller, logger) => {
/**
* 1. Create `xrplBridge.withdrawXrp` call
*/
//
logger.info(
{
parameters: {
amount: 1000000,
destination: "0x72ee785458b89d5ec64bec8410c958602e6f7673",
},
},
`create a "xrplBridge.withdrawXrp"`
);
const bridgeWithdrawalCall = api.tx.xrplBridge.withdrawXrp(10, "0x72ee785458b89d5ec64bec8410c958602e6f7673");

/**
* 2. Determine the `maxPayment` in ROOT by estimate the gas cost and use `dex` to get a quote
*/
// we need a dummy feeProxy call (with maxPayment=0) to do a proper fee estimation
const feeProxyCallForEstimation = api.tx.feeProxy.callWithFeePreferences(
ROOT_ASSET_ID,
0,
bridgeWithdrawalCall
);
const paymentInfo = await feeProxyCallForEstimation.paymentInfo(caller.address);
const estimatedFee = paymentInfo.partialFee.toString();

// query the the `dex` to determine the `maxPayment` you are willing to pay
const {
Ok: [amountIn],
} = (await api.rpc.dex.getAmountsIn(estimatedFee, [
ROOT_ASSET_ID,
XRP_ASSET_ID,
])) as unknown as AmountsIn;

// allow a buffer to avoid slippage, 5%
const maxPayment = Number(amountIn * 1.05).toFixed();

/**
* 3. Create and dispatch `feeProxy.callWithFeePreferences` extrinsic
*/
logger.info(
{
parameters: {
paymentAsset: ROOT_ASSET_ID,
maxPayment,
call: bridgeWithdrawalCall.toJSON(),
},
},
`create a "feeProxy.callWithFeePreferences"`
);
const feeProxyCall = api.tx.feeProxy.callWithFeePreferences(
ROOT_ASSET_ID,
maxPayment,
bridgeWithdrawalCall
);

logger.info(`dispatch extrinsic from caller="${caller.address}"`);
const { result, extrinsicId } = await sendExtrinsic(feeProxyCall, caller, { log: logger });
const [proxyEvent, withdrawalEvent] = filterExtrinsicEvents(result.events, [
"FeeProxy.CallWithFeePreferences",
"XrplBridge.WithdrawRequest"
]);

logger.info(
{
result: {
extrinsicId,
blockNumber: result.blockNumber,
proxyEvent: formatEventData(proxyEvent.event),
xrplWithdrawlEvent: formatEventData(withdrawalEvent.event),
},
},
"receive result"
);
});
1 change: 1 addition & 0 deletions packages/utils/src/porcini-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
export const SYLO_ASSET_ID = 3_172;
export const ASTO_ASSET_ID = 17_508;
export const XRP_ASSET_ID = 2;
export const ROOT_ASSET_ID = 1;
Loading