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

Support estimate fee on declare v2 #361

Merged
merged 5 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/test-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trap 'for killable in $(jobs -p); do kill -9 $killable; done' EXIT

# setup example repo
rm -rf starknet-hardhat-example
EXAMPLE_REPO_BRANCH="plugin"
EXAMPLE_REPO_BRANCH="estimate-declarev2-fee"
if [[ "$CIRCLE_BRANCH" == "master" ]] && [[ "$EXAMPLE_REPO_BRANCH" != "plugin" ]]; then
echo "Invalid example repo branch: $EXAMPLE_REPO_BRANCH"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions src/account-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export function calculateDeclareV2TxHash(

export async function sendDeclareV2Tx(
signatures: string[],
classHash: string,
compiledClassHash: string,
maxFee: Numeric,
senderAddress: string,
version: Numeric,
Expand All @@ -229,7 +229,7 @@ export async function sendDeclareV2Tx(
contract_class: contractClass.getCompiledClass(),
signature: signatures,
sender_address: senderAddress,
compiled_class_hash: number.toHex(number.toBN(classHash)),
compiled_class_hash: compiledClassHash,
version: numericToHexString(version),
nonce: numericToHexString(nonce),
max_fee: numericToHexString(maxFee)
Expand Down
61 changes: 55 additions & 6 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,56 @@ export abstract class Account {
);
}

private async estimateDeclareV2Fee(
contractFactory: StarknetContractFactory,
options: EstimateFeeOptions = {}
): Promise<starknet.FeeEstimation> {
const maxFee = (options.maxFee || 0).toString();
const version = DECLARE_VERSION;
const nonce = options.nonce == null ? await this.getNonce() : options.nonce;

const hre = await import("hardhat");
const chainId = hre.starknet.networkConfig.starknetChainId;

const compiledClassHash = await hre.starknetWrapper.getCompiledClassHash(
contractFactory.casmPath
);

const classHash = await hre.starknetWrapper.getSierraContractClassHash(
contractFactory.metadataPath
);

const calldata = [classHash];
const messageHash = calculateDeclareV2TxHash(
this.address,
calldata,
maxFee.toString(),
chainId,
[nonce.toString(), compiledClassHash]
);

const signatures = this.getSignatures(messageHash);
const data = {
type: "DECLARE",
sender_address: this.address,
compiled_class_hash: compiledClassHash,
contract_class: readCairo1Contract(contractFactory.metadataPath).getCompiledClass(),
signature: bnToDecimalStringArray(signatures || []),
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
version: numericToHexString(version),
nonce: numericToHexString(nonce)
};

return await sendEstimateFeeTx(data);
}

async estimateDeclareFee(
contractFactory: StarknetContractFactory,
options: EstimateFeeOptions = {}
): Promise<starknet.FeeEstimation> {
if (contractFactory.isCairo1()) {
return await this.estimateDeclareV2Fee(contractFactory, options);
}

const nonce = options.nonce == null ? await this.getNonce() : options.nonce;
const maxFee = (options.maxFee || 0).toString();

Expand Down Expand Up @@ -464,16 +510,19 @@ export abstract class Account {
contractFactory: StarknetContractFactory,
options: DeclareOptions = {}
): Promise<string> {
const maxFee = options?.maxFee;
if (!maxFee) {
const msg =
"maxFee must be provided to send declare transactions.\n" +
"A value of '0' for 'maxFee' is not supported.";
let maxFee = options?.maxFee;
if (maxFee && options?.overhead) {
const msg = "maxFee and overhead cannot be specified together";
throw new StarknetPluginError(msg);
}

const version = DECLARE_VERSION;
const nonce = options.nonce == null ? await this.getNonce() : options.nonce;
if (maxFee === undefined || maxFee === null) {
const estimatedDeclareFee = await this.estimateDeclareV2Fee(contractFactory, options);
maxFee = estimatedFeeToMaxFee(estimatedDeclareFee.amount, options?.overhead);
}

const version = DECLARE_VERSION;
const hre = await import("hardhat");
const chainId = hre.starknet.networkConfig.starknetChainId;

Expand Down
12 changes: 0 additions & 12 deletions src/starknet-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,6 @@ function getFullImageName(image: Image): string {
return `${image.repository}:${image.tag}`;
}

type String2String = { [path: string]: string };

export class DockerWrapper extends StarknetWrapper {
constructor(
private image: Image,
Expand Down Expand Up @@ -612,16 +610,6 @@ export class DockerWrapper extends StarknetWrapper {
}

public async interact(options: InteractWrapperOptions): Promise<ProcessResult> {
const binds: String2String = {};

if (options.abi) {
binds[options.abi] = options.abi;
}

if (options.accountDir) {
binds[options.accountDir] = options.accountDir;
}

const preparedOptions = this.prepareInteractOptions(options);
const executed = this.execute("starknet", preparedOptions);
return executed;
Expand Down
2 changes: 1 addition & 1 deletion src/starknet_cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def get_compiled_class_hash(args):
with open(casm_path, encoding="utf-8") as casm_file:
compiled_class = CompiledClass.loads(casm_file.read())
compiled_class_hash = compute_compiled_class_hash(compiled_class)
print(compiled_class_hash)
print(hex(compiled_class_hash))
return 0
except Exception as err:
print(err, file=sys.stderr)
Expand Down