Skip to content

Commit

Permalink
fix: add estimateDeployFee
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Nov 21, 2022
1 parent 572536d commit 3bc11be
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 14 deletions.
2 changes: 0 additions & 2 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { toBN } from '../src/utils/number';
import { encodeShortString } from '../src/utils/shortString';
import { randomAddress } from '../src/utils/stark';
import {
IS_DEVNET,
compiledErc20,
compiledTestDapp,
erc20ClassHash,
Expand Down Expand Up @@ -189,7 +188,6 @@ describe('deploy and test Wallet', () => {
],
salt,
unique: true, // Using true here so as not to clash with normal erc20 deploy in account and provider test
isDevnet: IS_DEVNET,
});

await provider.waitForTransaction(deployment.transaction_hash);
Expand Down
51 changes: 43 additions & 8 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ export class Account extends Provider implements AccountInterface {
};
}

public async estimateDeployFee(
{
classHash,
salt,
unique = true,
constructorCalldata = [],
additionalCalls = [],
}: UniversalDeployerContractPayload,
transactionsDetail?: InvocationsDetails | undefined
): Promise<EstimateFee> {
const compiledConstructorCallData = compileCalldata(constructorCalldata);

const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
return this.estimateInvokeFee(
[
{
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
salt,
toCairoBool(unique),
compiledConstructorCallData.length,
...compiledConstructorCallData,
],
},
...callsArray,
],
transactionsDetail
);
}

public async execute(
calls: AllowArray<Call>,
abis: Abi[] | undefined = undefined,
Expand Down Expand Up @@ -243,9 +275,8 @@ export class Account extends Provider implements AccountInterface {
salt,
unique = true,
constructorCalldata = [],
isDevnet = false,
additionalCalls = [],
}: UniversalDeployerContractPayload,
additionalCalls: AllowArray<Call> = [], // support multicall
transactionsDetail: InvocationsDetails = {}
): Promise<InvokeFunctionResponse> {
const compiledConstructorCallData = compileCalldata(constructorCalldata);
Expand All @@ -255,7 +286,7 @@ export class Account extends Provider implements AccountInterface {
return this.execute(
[
{
contractAddress: isDevnet ? UDC.ADDRESS_DEVNET : UDC.ADDRESS,
contractAddress: UDC.ADDRESS,
entrypoint: UDC.ENTRYPOINT,
calldata: [
classHash,
Expand Down Expand Up @@ -350,22 +381,26 @@ export class Account extends Provider implements AccountInterface {
}

public async getSuggestedMaxFee(
estimateFeeAction: EstimateFeeAction,
{ type, payload }: EstimateFeeAction,
details: EstimateFeeDetails
) {
let feeEstimate: EstimateFee;

switch (estimateFeeAction.type) {
switch (type) {
case 'INVOKE':
feeEstimate = await this.estimateInvokeFee(estimateFeeAction.payload, details);
feeEstimate = await this.estimateInvokeFee(payload, details);
break;

case 'DECLARE':
feeEstimate = await this.estimateDeclareFee(estimateFeeAction.payload, details);
feeEstimate = await this.estimateDeclareFee(payload, details);
break;

case 'DEPLOY_ACCOUNT':
feeEstimate = await this.estimateAccountDeployFee(estimateFeeAction.payload, details);
feeEstimate = await this.estimateAccountDeployFee(payload, details);
break;

case 'DEPLOY':
feeEstimate = await this.estimateDeployFee(payload, details);
break;

default:
Expand Down
27 changes: 25 additions & 2 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ export abstract class AccountInterface extends ProviderInterface {
estimateFeeDetails?: EstimateFeeDetails
): Promise<EstimateFeeResponse>;

/**
* Estimate Fee for executing a UDC DEPLOY transaction on starknet
* This is different from the normal DEPLOY transaction as it goes through the Universal Deployer Contract (UDC)
* @param deployContractPayload containing
* - classHash: computed class hash of compiled contract
* - salt: address salt
* - unique: bool if true ensure unique salt
* - calldata: constructor calldata
* - additionalCalls - optional additional calls array to support multicall
*
* @param transactionsDetail Invocation Details containing:
* - optional nonce
* - optional version
* - optional maxFee
*/
public abstract estimateDeployFee(
deployContractPayload: UniversalDeployerContractPayload,
transactionsDetail?: InvocationsDetails
): Promise<EstimateFeeResponse>;

/**
* Invoke execute function in account contract
*
Expand Down Expand Up @@ -123,20 +144,22 @@ export abstract class AccountInterface extends ProviderInterface {
): Promise<DeclareContractResponse>;

/**
* Deploys a given compiled contract (json) to starknet using Universal Deployer Contract (UDC)
* This is different from the normal DEPLOY transaction as it goes through the Universal Deployer Contract (UDC)
*
* @param deployContractPayload containing
* - classHash: computed class hash of compiled contract
* - salt: address salt
* - unique: bool if true ensure unique salt
* - calldata: constructor calldata
* @param additionalCalls - optional additional calls array to support multicall
* - additionalCalls - optional additional calls array to support multicall
* @param transactionsDetail Invocation Details containing:
* - optional nonce
* - optional version
* - optional maxFee
*/
public abstract deploy(
deployContractPayload: UniversalDeployerContractPayload,
additionalCalls?: AllowArray<Call>,
transactionsDetail?: InvocationsDetails
): Promise<InvokeFunctionResponse>;

Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export enum TransactionHashPrefix {
export const UDC = {
ADDRESS: '0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf',
ENTRYPOINT: 'deployContract',
ADDRESS_DEVNET: '0x25fcb74260022bd8ed7e8d542408941826b53345e478b8303d6f31744838a36',
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type UniversalDeployerContractPayload = {
salt: string;
unique: boolean;
constructorCalldata?: RawArgs;
isDevnet?: boolean;
additionalCalls?: AllowArray<Call>; // support multicall
};

export type DeployContractPayload = {
Expand Down
5 changes: 5 additions & 0 deletions src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RawCalldata,
Signature,
Status,
UniversalDeployerContractPayload,
} from './lib';

export interface GetBlockResponse {
Expand Down Expand Up @@ -131,4 +132,8 @@ export type EstimateFeeAction =
| {
type: 'DEPLOY_ACCOUNT';
payload: DeployAccountContractPayload;
}
| {
type: 'DEPLOY';
payload: UniversalDeployerContractPayload;
};

0 comments on commit 3bc11be

Please sign in to comment.