Skip to content

Commit

Permalink
feat: implement new interface on contract class
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 7, 2022
1 parent b202f37 commit e942a9d
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 88 deletions.
9 changes: 5 additions & 4 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Account extends Provider implements AccountInterface {
public async getNonce(): Promise<string> {
const { result } = await this.callContract({
contractAddress: this.address,
entryPointSelector: 'get_nonce',
entrypoint: 'get_nonce',
calldata: [],
});
return toHex(toBN(result[0]));
Expand All @@ -64,8 +64,9 @@ export class Account extends Provider implements AccountInterface {

const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
const fetchedEstimate = await super.getEstimateFee(
{ contractAddress: this.address, entryPointSelector: '__execute__', calldata, signature },
blockIdentifier
{ contractAddress: this.address, entrypoint: '__execute__', calldata },
blockIdentifier,
signature
);

const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.overallFee);
Expand Down Expand Up @@ -159,7 +160,7 @@ export class Account extends Provider implements AccountInterface {
try {
await this.callContract({
contractAddress: this.address,
entryPointSelector: 'is_valid_signature',
entrypoint: 'is_valid_signature',
calldata: compileCalldata({
hash: toBN(hash).toString(),
signature: signature.map((x) => toBN(x).toString()),
Expand Down
26 changes: 25 additions & 1 deletion src/account/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { ProviderInterface } from '../provider';
import { SignerInterface } from '../signer';
import { Abi, Call, InvocationsDetails, InvokeFunctionResponse, Signature } from '../types';
import {
Abi,
Call,
EstimateFee,
EstimateFeeDetails,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import { BigNumberish } from '../utils/number';
import { TypedData } from '../utils/typedData/types';

Expand All @@ -9,6 +17,22 @@ export abstract class AccountInterface extends ProviderInterface {

public abstract signer: SignerInterface;

/**
* 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(
calls: Call | Call[],
estimateFeeDetails?: EstimateFeeDetails
): Promise<EstimateFee>;

/**
* Invoke execute function in account contract
*
Expand Down
10 changes: 5 additions & 5 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { BlockIdentifier } from '../provider/utils';
import {
Abi,
AbiEntry,
AddTransactionResponse,
Args,
AsyncContractFunction,
Calldata,
ContractFunction,
FunctionAbi,
Invocation,
InvokeFunctionResponse,
Overrides,
ParsedStruct,
Result,
Expand Down Expand Up @@ -97,7 +97,7 @@ export class Contract implements ContractInterface {

address: string;

providerOrAccount: ProviderInterface | AccountInterface;
providerOrAccount!: ProviderInterface | AccountInterface;

deployTransactionHash?: string;

Expand Down Expand Up @@ -544,7 +544,7 @@ export class Contract implements ContractInterface {
method: string,
args: Array<any> = [],
options: Overrides = {}
): Promise<AddTransactionResponse> {
): Promise<InvokeFunctionResponse> {
// ensure contract is connected
assert(this.address !== null, 'contract isnt connected to an address');
// validate method and args
Expand Down Expand Up @@ -578,6 +578,7 @@ export class Contract implements ContractInterface {
});
}

// TODO: throw warning
return this.providerOrAccount.invokeFunction({
...invocation,
signature: options.signature || [],
Expand Down Expand Up @@ -609,13 +610,12 @@ export class Contract implements ContractInterface {
calldata,
entrypoint: method,
},
{ blockIdentifier }
blockIdentifier
)
.then((x) => this.parseResponse(method, x.result));
}

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

Expand Down
12 changes: 7 additions & 5 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { StarknetChainId } from '../constants';
import {
Call,
CallContractResponse,
DeclareContractPayload,
DeclareContractResponse,
DeployContractPayload,
DeployContractResponse,
EstimateFeeResponse,
FunctionCall,
GetBlockResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import { BigNumberish } from '../utils/number';
import { GatewayProvider, GatewayProviderOptions } from './gateway';
Expand Down Expand Up @@ -52,10 +53,11 @@ export class Provider implements ProviderInterface {
}

public async getEstimateFee(
request: FunctionCall,
blockIdentifier: BlockIdentifier
request: Call,
blockIdentifier: BlockIdentifier,
signature?: Signature
): Promise<EstimateFeeResponse> {
return this.provider.getEstimateFee(request, blockIdentifier);
return this.provider.getEstimateFee(request, blockIdentifier, signature);
}

public async getStorageAt(
Expand All @@ -75,7 +77,7 @@ export class Provider implements ProviderInterface {
}

public async callContract(
request: FunctionCall,
request: Call,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<CallContractResponse> {
return this.provider.callContract(request, blockIdentifier);
Expand Down
15 changes: 8 additions & 7 deletions src/provider/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import urljoin from 'url-join';

import { ONE, StarknetChainId, ZERO } from '../constants';
import {
Call,
CallContractResponse,
CompiledContract,
DeclareContractPayload,
DeclareContractResponse,
DeployContractPayload,
DeployContractResponse,
EstimateFeeResponse,
FunctionCall,
Gateway,
GetBlockResponse,
GetContractAddressesResponse,
Expand All @@ -20,6 +20,7 @@ import {
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
Expand Down Expand Up @@ -205,7 +206,7 @@ export class GatewayProvider implements ProviderInterface {
}

public async callContract(
{ contractAddress, entryPointSelector, calldata = [] }: FunctionCall,
{ contractAddress, entrypoint: entryPointSelector, calldata = [] }: Call,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<CallContractResponse> {
return this.fetchEndpoint(
Expand Down Expand Up @@ -322,17 +323,17 @@ export class GatewayProvider implements ProviderInterface {
}

public async getEstimateFee(
request: FunctionCall,
call: Call,
blockIdentifier: BlockIdentifier = 'pending',
signature?: Array<string>
signature?: Signature
): Promise<EstimateFeeResponse> {
return this.fetchEndpoint(
'estimate_fee',
{ blockIdentifier },
{
contract_address: request.contractAddress,
entry_point_selector: getSelectorFromName(request.entryPointSelector),
calldata: bigNumberishArrayToDecimalStringArray(request.calldata ?? []),
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: bigNumberishArrayToDecimalStringArray(call.calldata ?? []),
signature: bigNumberishArrayToDecimalStringArray(signature || []),
}
).then(this.responseParser.parseFeeEstimateResponse);
Expand Down
12 changes: 7 additions & 5 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { StarknetChainId } from '../constants';
import type {
Call,
CallContractResponse,
DeclareContractPayload,
DeclareContractResponse,
DeployContractPayload,
DeployContractResponse,
EstimateFeeResponse,
FunctionCall,
GetBlockResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
Signature,
} from '../types';
import type { BigNumberish } from '../utils/number';
import { BlockIdentifier } from './utils';
Expand All @@ -28,7 +29,7 @@ export abstract class ProviderInterface {
* @returns the result of the function on the smart contract.
*/
public abstract callContract(
request: FunctionCall,
request: Call,
blockIdentifier?: BlockIdentifier
): Promise<CallContractResponse>;

Expand Down Expand Up @@ -122,12 +123,13 @@ export abstract class ProviderInterface {
*/
public abstract invokeFunction(
invocation: Invocation,
details: InvocationsDetails
details?: InvocationsDetails
): Promise<InvokeFunctionResponse>;

public abstract getEstimateFee(
request: FunctionCall,
blockIdentifier: BlockIdentifier
request: Call,
blockIdentifier: BlockIdentifier,
signature?: Signature
): Promise<EstimateFeeResponse>;

public abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
Expand Down
22 changes: 12 additions & 10 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import fetch from 'cross-fetch';

import { StarknetChainId } from '../constants';
import {
Call,
CallContractResponse,
CompiledContract,
DeclareContractPayload,
DeclareContractResponse,
DeployContractPayload,
DeployContractResponse,
EstimateFeeResponse,
FunctionCall,
GetBlockResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
InvocationsDetails,
InvokeFunctionResponse,
RPC,
Signature,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { parse, stringify } from '../utils/json';
Expand Down Expand Up @@ -135,10 +136,11 @@ export class RPCProvider implements ProviderInterface {
}

public async getEstimateFee(
request: FunctionCall,
blockIdentifier: BlockIdentifier = 'pending'
call: Call,
blockIdentifier: BlockIdentifier = 'pending',
_signature: Signature = []
): Promise<EstimateFeeResponse> {
const parsedCalldata = request.calldata.map((data) => {
const parsedCalldata = call.calldata?.map((data) => {
if (typeof data === 'string' && isHex(data as string)) {
return data;
}
Expand All @@ -147,8 +149,8 @@ export class RPCProvider implements ProviderInterface {

return this.fetchEndpoint('starknet_estimateFee', [
{
contract_address: request.contractAddress,
entry_point_selector: getSelectorFromName(request.entryPointSelector),
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: parsedCalldata,
},
blockIdentifier,
Expand Down Expand Up @@ -223,10 +225,10 @@ export class RPCProvider implements ProviderInterface {
}

public async callContract(
request: FunctionCall,
call: Call,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<CallContractResponse> {
const parsedCalldata = request.calldata.map((data) => {
const parsedCalldata = call.calldata?.map((data) => {
if (typeof data === 'string' && isHex(data as string)) {
return data;
}
Expand All @@ -235,8 +237,8 @@ export class RPCProvider implements ProviderInterface {

const result = await this.fetchEndpoint('starknet_call', [
{
contract_address: request.contractAddress,
entry_point_selector: getSelectorFromName(request.entryPointSelector),
contract_address: call.contractAddress,
entry_point_selector: getSelectorFromName(call.entrypoint),
calldata: parsedCalldata,
},
blockIdentifier,
Expand Down
Loading

0 comments on commit e942a9d

Please sign in to comment.