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

Add estimate message fee #312

Merged
merged 17 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CONFIG_FILE_NAME="hardhat.config.ts"

# setup example repo
rm -rf starknet-hardhat-example
EXAMPLE_REPO_BRANCH="plugin"
EXAMPLE_REPO_BRANCH="estimate-message-fee"
if [[ "$CIRCLE_BRANCH" == "master" ]] && [[ "$EXAMPLE_REPO_BRANCH" != "plugin" ]]; then
echo "Invalid example repo branch: $EXAMPLE_REPO_BRANCH"
exit 1
Expand Down
31 changes: 30 additions & 1 deletion src/devnet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import axios, { AxiosResponse, Method } from "axios";
import { StarknetPluginError } from "./starknet-plugin-error";
import { Devnet, HardhatRuntimeEnvironment } from "hardhat/types";

import { Block, MintResponse, L2ToL1Message } from "./starknet-types";
import { Block, MintResponse, L2ToL1Message, FeeEstimation } from "./starknet-types";
import { REQUEST_TIMEOUT } from "./constants";
import { hash } from "starknet";
import { numericToHexString } from "./utils";
import { hexToDecimalString } from "starknet/utils/number";

interface L1ToL2Message {
address: string;
Expand Down Expand Up @@ -117,6 +118,34 @@ Make sure you really want to interact with Devnet and that it is running and ava
return response.data;
}

public async estimateMessageFee(
fromAddress: string,
toAddress: string,
funcionName: string,
payload: number[]
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
) {
const body = {
from_address: hexToDecimalString(fromAddress),
to_address: toAddress,
entry_point_selector: hash.getSelectorFromName(funcionName),
payload: payload.map((item) => numericToHexString(item))
};

const response = await this.requestHandler<FeeEstimation>(
"/feeder_gateway/estimate_message_fee",
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
"POST",
body
);

const { gas_price, gas_usage, overall_fee, unit } = response.data;
return {
amount: BigInt(overall_fee),
unit,
gas_price: BigInt(gas_price),
gas_usage: BigInt(gas_usage)
};
}

public async loadL1MessagingContract(networkUrl: string, address?: string, networkId?: string) {
const body = {
networkId,
Expand Down
17 changes: 16 additions & 1 deletion src/types/devnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
L1ToL2MockTxResponse,
L2ToL1MockTxResponse
} from "../devnet-utils";
import { Block, MintResponse } from "../starknet-types";
import { Block, FeeEstimation, MintResponse } from "../starknet-types";

export interface Devnet {
/**
Expand All @@ -23,6 +23,21 @@ export interface Devnet {
*/
flush: () => Promise<FlushResponse>;

/**
* Computes message fee estimation
* @param {string} fromAddress - Address of message source
* @param {string} toAddress - Address of message destination
* @param {string} functionName - Function name for entry point selector
* @param {Array<number>} payload - Payload to send
* @returns Fee estimation
*/
estimateMessageFee: (
fromAddress: string,
toAddress: string,
funcionName: string,
payload: Array<number>
) => Promise<FeeEstimation>;

/**
* Deploys or loads the L1 messaging contract.
* @param {string} networkUrl - L1 network url.
Expand Down
8 changes: 8 additions & 0 deletions www/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ it("should estimate fee", async function () {

const invokeFee = await account.estimateFee(contract, "method", { arg1: 10n });
await account.invoke(contract, "method", { arg1: 10n }); // computes max fee implicitly

// computes message estimate fee
const estimatedMessageFee = await starknet.devnet.estimateMessageFee(
L1_CONTRACT_ADDRESS,
l2contract.address,
"deposit",
[556, 123]
);
});
```

Expand Down