Skip to content

Commit

Permalink
feat(contract): contract expanded with estimateFee method
Browse files Browse the repository at this point in the history
  • Loading branch information
MilGard91 committed Mar 15, 2022
1 parent c61232d commit fbaf4ba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
6 changes: 6 additions & 0 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ describe('class Contract {}', () => {
expect(res).toHaveProperty('signature');
});

test('estimate gas fee for `mint`', async () => {
const res = await erc20.estimateFee.mint(wallet, '10');
expect(res).toHaveProperty('amount');
expect(res).toHaveProperty('unit');
});

test('read initial balance of that account', async () => {
const result = await erc20.balance_of(wallet);
const [res] = result;
Expand Down
24 changes: 8 additions & 16 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,23 +595,15 @@ export class Contract implements ContractInterface {
.then((x) => this.parseResponse(method, x.result));
}

public async estimate(_method: string, _args: Array<any> = []) {
public async estimate(method: string, args: Array<any> = []) {
// TODO; remove error as soon as estimate fees are supported
throw Error('Estimation of the fees are not yet supported');
// // ensure contract is connected
// assert(this.address !== null, 'contract isnt connected to an address');

// // validate method and args
// // this.validateMethodAndArgs('CALL', method, args);
// const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;

// // compile calldata
// const calldata = this.compileCalldata(args, inputs);
// return this.providerOrAccount.estimateFee({
// contractAddress: this.address as string,
// calldata,
// entrypoint: method,
// });
// ensure contract is connected
assert(this.address !== null, 'contract isnt connected to an address');

// validate method and args
this.validateMethodAndArgs('INVOKE', method, args);
const invocation = this.populateTransaction[method](...args);
return this.providerOrAccount.estimateFee(invocation);
}

public populate(method: string, args: Array<any> = []): Invocation {
Expand Down
4 changes: 1 addition & 3 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,8 @@ export class Provider implements ProviderInterface {

public estimateFee(invocation: Invocation): Promise<any> {
return this.fetchEndpoint('estimate_fee', undefined, {
// TODO: change the TYPE of the call
type: 'INVOKE_FUNCTION',
contract_address: invocation.contractAddress,
entry_point_selector: getSelectorFromName(invocation.entrypoint),
entry_point_selector: invocation.entrypoint,
calldata: bigNumberishArrayToDecimalStringArray(invocation.calldata ?? []),
signature: bigNumberishArrayToDecimalStringArray(invocation.signature ?? []),
});
Expand Down
13 changes: 13 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ export abstract class ProviderInterface {
*/
public abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;

/**
* Estimate Fee for a method on starknet
*
* @param invocation the invocation object containing:
* - contractAddress - the address of the contract
* - entrypoint - the entrypoint of the contract
* - calldata - (defaults to []) the calldata
* - signature - (defaults to []) the signature
*
* @returns response from addTransaction
*/
public abstract estimateFee(invocation: Invocation): Promise<any>;

public abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type Endpoints = {
};
estimate_fee: {
QUERY: never;
REQUEST: Transaction;
REQUEST: CallContractTransaction;
RESPONSE: EstimateFeeResponse;
};
};
Expand Down

0 comments on commit fbaf4ba

Please sign in to comment.