Skip to content

Commit

Permalink
[Fix] Small minor fixes for evm/use-feeProxy (#37)
Browse files Browse the repository at this point in the history
* Allow specifying `local` as network

* Update logging output

* Add safe unwrap for `fpAccount`
  • Loading branch information
ken-futureverse authored Oct 29, 2023
1 parent 103a726 commit 40091b1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
18 changes: 11 additions & 7 deletions examples/evm/use-feeProxy/src/callERC20Transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { ContractReceipt, utils } from "ethers";
* Use `feeProxy.callWithFeePreferences` to trigger a `transfer` call of SYLO token, and pay gas
* in ASTO token.
*
* Assumes the caller has some ASTO balance.
* Assumes the caller has some ASTO balance to pay for gas and some SYLO balance to demonstrate
* the transfer
*/
withEthersProvider("porcini", async (provider, wallet, logger) => {
// contracts creation
Expand All @@ -21,15 +22,21 @@ withEthersProvider("porcini", async (provider, wallet, logger) => {
const asto = getERC20Contract(ASTO_ASSET_ID).connect(wallet);

// transfer call
const transferAmount = utils.parseUnits("1.0", 18); // 1 SYLO
const transferAmount = utils.parseUnits("0.1", 18); // 1 SYLO
const transferInput = new utils.Interface(ERC20_ABI).encodeFunctionData("transfer", [
ALICE,
transferAmount,
]);
const transferEstimate = await sylo.estimateGas.transfer(ALICE, transferAmount);

logger.info(
`prepare a transfer call with destination="${ALICE}", amount="${transferAmount}", and estimateGas="${transferEstimate}"`
{
parameters: {
destination: ALICE,
amount: transferAmount,
},
},
`create a transfer call`
);

// retrieve the maxPayment and maxFeePerGas parameters with slippage to be 0.05 (5%)
Expand All @@ -40,10 +47,7 @@ withEthersProvider("porcini", async (provider, wallet, logger) => {
0.05
);

logger.info(
`dispatch a feeProxy call with maxPayment="${maxPayment}", and maxFeePerGas="${maxFeePerGas}"`
);

logger.info(`dispatch transaction from wallet=${wallet.address}`);
const tx = await feeProxy.callWithFeePreferences(
asto.address,
maxPayment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ withChainApi("porcini", async (api, caller, logger) => {
/**
* 1. Create `futurepass.proxyExtrinsic` call that wraps around `evm.call`
*/
const fpAccount = (await api.query.futurepass.holders(caller.address)).unwrap();
const fpAccount = (await api.query.futurepass.holders(caller.address)).unwrapOr(undefined);
assert(fpAccount);
logger.info(
{
futurepass: {
Expand All @@ -38,7 +39,6 @@ withChainApi("porcini", async (api, caller, logger) => {
},
"futurepass details"
);
assert(fpAccount);

const { call: evmCall, estimateGasCost } = await createEVMCall(fpAccount.toString(), api, logger);
logger.info(
Expand Down
8 changes: 5 additions & 3 deletions packages/utils/src/withEthersProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const pe = new PrettyError();
export type EthersProvider = providers.JsonRpcProvider;

export async function withEthersProvider(
network: NetworkName,
network: NetworkName | "local",
callback: (provider: EthersProvider, wallet: Wallet, logger: Logger) => Promise<void>
) {
const { provider, wallet, logger } = await provideEthersProvider(network);
Expand All @@ -27,10 +27,12 @@ export async function withEthersProvider(
logger.info("call ended 🎉 ");
}

export async function provideEthersProvider(network: NetworkName) {
export async function provideEthersProvider(network: NetworkName | "local") {
const logger = getLogger();

const provider = new providers.JsonRpcProvider(getPublicProviderUrl(network));
const provider = new providers.JsonRpcProvider(
network !== "local" ? getPublicProviderUrl(network) : "http://localhost:9933"
);
logger.info(`create a JsonRpcProvider instance with network="${network}"`);

const wallet = new Wallet(env.CALLER_PRIVATE_KEY, provider);
Expand Down

0 comments on commit 40091b1

Please sign in to comment.