Skip to content

Commit e942a9d

Browse files
committed
feat: implement new interface on contract class
1 parent b202f37 commit e942a9d

File tree

9 files changed

+114
-88
lines changed

9 files changed

+114
-88
lines changed

src/account/default.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class Account extends Provider implements AccountInterface {
3838
public async getNonce(): Promise<string> {
3939
const { result } = await this.callContract({
4040
contractAddress: this.address,
41-
entryPointSelector: 'get_nonce',
41+
entrypoint: 'get_nonce',
4242
calldata: [],
4343
});
4444
return toHex(toBN(result[0]));
@@ -64,8 +64,9 @@ export class Account extends Provider implements AccountInterface {
6464

6565
const calldata = fromCallsToExecuteCalldataWithNonce(transactions, nonce);
6666
const fetchedEstimate = await super.getEstimateFee(
67-
{ contractAddress: this.address, entryPointSelector: '__execute__', calldata, signature },
68-
blockIdentifier
67+
{ contractAddress: this.address, entrypoint: '__execute__', calldata },
68+
blockIdentifier,
69+
signature
6970
);
7071

7172
const suggestedMaxFee = estimatedFeeToMaxFee(fetchedEstimate.overallFee);
@@ -159,7 +160,7 @@ export class Account extends Provider implements AccountInterface {
159160
try {
160161
await this.callContract({
161162
contractAddress: this.address,
162-
entryPointSelector: 'is_valid_signature',
163+
entrypoint: 'is_valid_signature',
163164
calldata: compileCalldata({
164165
hash: toBN(hash).toString(),
165166
signature: signature.map((x) => toBN(x).toString()),

src/account/interface.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { ProviderInterface } from '../provider';
22
import { SignerInterface } from '../signer';
3-
import { Abi, Call, InvocationsDetails, InvokeFunctionResponse, Signature } from '../types';
3+
import {
4+
Abi,
5+
Call,
6+
EstimateFee,
7+
EstimateFeeDetails,
8+
InvocationsDetails,
9+
InvokeFunctionResponse,
10+
Signature,
11+
} from '../types';
412
import { BigNumberish } from '../utils/number';
513
import { TypedData } from '../utils/typedData/types';
614

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

1018
public abstract signer: SignerInterface;
1119

20+
/**
21+
* Estimate Fee for a method on starknet
22+
*
23+
* @param invocation the invocation object containing:
24+
* - contractAddress - the address of the contract
25+
* - entrypoint - the entrypoint of the contract
26+
* - calldata - (defaults to []) the calldata
27+
* - signature - (defaults to []) the signature
28+
*
29+
* @returns response from addTransaction
30+
*/
31+
public abstract estimateFee(
32+
calls: Call | Call[],
33+
estimateFeeDetails?: EstimateFeeDetails
34+
): Promise<EstimateFee>;
35+
1236
/**
1337
* Invoke execute function in account contract
1438
*

src/contract/default.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import { BlockIdentifier } from '../provider/utils';
77
import {
88
Abi,
99
AbiEntry,
10-
AddTransactionResponse,
1110
Args,
1211
AsyncContractFunction,
1312
Calldata,
1413
ContractFunction,
1514
FunctionAbi,
1615
Invocation,
16+
InvokeFunctionResponse,
1717
Overrides,
1818
ParsedStruct,
1919
Result,
@@ -97,7 +97,7 @@ export class Contract implements ContractInterface {
9797

9898
address: string;
9999

100-
providerOrAccount: ProviderInterface | AccountInterface;
100+
providerOrAccount!: ProviderInterface | AccountInterface;
101101

102102
deployTransactionHash?: string;
103103

@@ -544,7 +544,7 @@ export class Contract implements ContractInterface {
544544
method: string,
545545
args: Array<any> = [],
546546
options: Overrides = {}
547-
): Promise<AddTransactionResponse> {
547+
): Promise<InvokeFunctionResponse> {
548548
// ensure contract is connected
549549
assert(this.address !== null, 'contract isnt connected to an address');
550550
// validate method and args
@@ -578,6 +578,7 @@ export class Contract implements ContractInterface {
578578
});
579579
}
580580

581+
// TODO: throw warning
581582
return this.providerOrAccount.invokeFunction({
582583
...invocation,
583584
signature: options.signature || [],
@@ -609,13 +610,12 @@ export class Contract implements ContractInterface {
609610
calldata,
610611
entrypoint: method,
611612
},
612-
{ blockIdentifier }
613+
blockIdentifier
613614
)
614615
.then((x) => this.parseResponse(method, x.result));
615616
}
616617

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

src/provider/default.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { StarknetChainId } from '../constants';
22
import {
3+
Call,
34
CallContractResponse,
45
DeclareContractPayload,
56
DeclareContractResponse,
67
DeployContractPayload,
78
DeployContractResponse,
89
EstimateFeeResponse,
9-
FunctionCall,
1010
GetBlockResponse,
1111
GetTransactionReceiptResponse,
1212
GetTransactionResponse,
1313
Invocation,
1414
InvocationsDetails,
1515
InvokeFunctionResponse,
16+
Signature,
1617
} from '../types';
1718
import { BigNumberish } from '../utils/number';
1819
import { GatewayProvider, GatewayProviderOptions } from './gateway';
@@ -52,10 +53,11 @@ export class Provider implements ProviderInterface {
5253
}
5354

5455
public async getEstimateFee(
55-
request: FunctionCall,
56-
blockIdentifier: BlockIdentifier
56+
request: Call,
57+
blockIdentifier: BlockIdentifier,
58+
signature?: Signature
5759
): Promise<EstimateFeeResponse> {
58-
return this.provider.getEstimateFee(request, blockIdentifier);
60+
return this.provider.getEstimateFee(request, blockIdentifier, signature);
5961
}
6062

6163
public async getStorageAt(
@@ -75,7 +77,7 @@ export class Provider implements ProviderInterface {
7577
}
7678

7779
public async callContract(
78-
request: FunctionCall,
80+
request: Call,
7981
blockIdentifier: BlockIdentifier = 'pending'
8082
): Promise<CallContractResponse> {
8183
return this.provider.callContract(request, blockIdentifier);

src/provider/gateway.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import urljoin from 'url-join';
22

33
import { ONE, StarknetChainId, ZERO } from '../constants';
44
import {
5+
Call,
56
CallContractResponse,
67
CompiledContract,
78
DeclareContractPayload,
89
DeclareContractResponse,
910
DeployContractPayload,
1011
DeployContractResponse,
1112
EstimateFeeResponse,
12-
FunctionCall,
1313
Gateway,
1414
GetBlockResponse,
1515
GetContractAddressesResponse,
@@ -20,6 +20,7 @@ import {
2020
Invocation,
2121
InvocationsDetails,
2222
InvokeFunctionResponse,
23+
Signature,
2324
} from '../types';
2425
import { getSelectorFromName } from '../utils/hash';
2526
import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
@@ -205,7 +206,7 @@ export class GatewayProvider implements ProviderInterface {
205206
}
206207

207208
public async callContract(
208-
{ contractAddress, entryPointSelector, calldata = [] }: FunctionCall,
209+
{ contractAddress, entrypoint: entryPointSelector, calldata = [] }: Call,
209210
blockIdentifier: BlockIdentifier = 'pending'
210211
): Promise<CallContractResponse> {
211212
return this.fetchEndpoint(
@@ -322,17 +323,17 @@ export class GatewayProvider implements ProviderInterface {
322323
}
323324

324325
public async getEstimateFee(
325-
request: FunctionCall,
326+
call: Call,
326327
blockIdentifier: BlockIdentifier = 'pending',
327-
signature?: Array<string>
328+
signature?: Signature
328329
): Promise<EstimateFeeResponse> {
329330
return this.fetchEndpoint(
330331
'estimate_fee',
331332
{ blockIdentifier },
332333
{
333-
contract_address: request.contractAddress,
334-
entry_point_selector: getSelectorFromName(request.entryPointSelector),
335-
calldata: bigNumberishArrayToDecimalStringArray(request.calldata ?? []),
334+
contract_address: call.contractAddress,
335+
entry_point_selector: getSelectorFromName(call.entrypoint),
336+
calldata: bigNumberishArrayToDecimalStringArray(call.calldata ?? []),
336337
signature: bigNumberishArrayToDecimalStringArray(signature || []),
337338
}
338339
).then(this.responseParser.parseFeeEstimateResponse);

src/provider/interface.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { StarknetChainId } from '../constants';
22
import type {
3+
Call,
34
CallContractResponse,
45
DeclareContractPayload,
56
DeclareContractResponse,
67
DeployContractPayload,
78
DeployContractResponse,
89
EstimateFeeResponse,
9-
FunctionCall,
1010
GetBlockResponse,
1111
GetTransactionReceiptResponse,
1212
GetTransactionResponse,
1313
Invocation,
1414
InvocationsDetails,
1515
InvokeFunctionResponse,
16+
Signature,
1617
} from '../types';
1718
import type { BigNumberish } from '../utils/number';
1819
import { BlockIdentifier } from './utils';
@@ -28,7 +29,7 @@ export abstract class ProviderInterface {
2829
* @returns the result of the function on the smart contract.
2930
*/
3031
public abstract callContract(
31-
request: FunctionCall,
32+
request: Call,
3233
blockIdentifier?: BlockIdentifier
3334
): Promise<CallContractResponse>;
3435

@@ -122,12 +123,13 @@ export abstract class ProviderInterface {
122123
*/
123124
public abstract invokeFunction(
124125
invocation: Invocation,
125-
details: InvocationsDetails
126+
details?: InvocationsDetails
126127
): Promise<InvokeFunctionResponse>;
127128

128129
public abstract getEstimateFee(
129-
request: FunctionCall,
130-
blockIdentifier: BlockIdentifier
130+
request: Call,
131+
blockIdentifier: BlockIdentifier,
132+
signature?: Signature
131133
): Promise<EstimateFeeResponse>;
132134

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

src/provider/rpc.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ import fetch from 'cross-fetch';
22

33
import { StarknetChainId } from '../constants';
44
import {
5+
Call,
56
CallContractResponse,
67
CompiledContract,
78
DeclareContractPayload,
89
DeclareContractResponse,
910
DeployContractPayload,
1011
DeployContractResponse,
1112
EstimateFeeResponse,
12-
FunctionCall,
1313
GetBlockResponse,
1414
GetTransactionReceiptResponse,
1515
GetTransactionResponse,
1616
Invocation,
1717
InvocationsDetails,
1818
InvokeFunctionResponse,
1919
RPC,
20+
Signature,
2021
} from '../types';
2122
import { getSelectorFromName } from '../utils/hash';
2223
import { parse, stringify } from '../utils/json';
@@ -135,10 +136,11 @@ export class RPCProvider implements ProviderInterface {
135136
}
136137

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

148150
return this.fetchEndpoint('starknet_estimateFee', [
149151
{
150-
contract_address: request.contractAddress,
151-
entry_point_selector: getSelectorFromName(request.entryPointSelector),
152+
contract_address: call.contractAddress,
153+
entry_point_selector: getSelectorFromName(call.entrypoint),
152154
calldata: parsedCalldata,
153155
},
154156
blockIdentifier,
@@ -223,10 +225,10 @@ export class RPCProvider implements ProviderInterface {
223225
}
224226

225227
public async callContract(
226-
request: FunctionCall,
228+
call: Call,
227229
blockIdentifier: BlockIdentifier = 'pending'
228230
): Promise<CallContractResponse> {
229-
const parsedCalldata = request.calldata.map((data) => {
231+
const parsedCalldata = call.calldata?.map((data) => {
230232
if (typeof data === 'string' && isHex(data as string)) {
231233
return data;
232234
}
@@ -235,8 +237,8 @@ export class RPCProvider implements ProviderInterface {
235237

236238
const result = await this.fetchEndpoint('starknet_call', [
237239
{
238-
contract_address: request.contractAddress,
239-
entry_point_selector: getSelectorFromName(request.entryPointSelector),
240+
contract_address: call.contractAddress,
241+
entry_point_selector: getSelectorFromName(call.entrypoint),
240242
calldata: parsedCalldata,
241243
},
242244
blockIdentifier,

0 commit comments

Comments
 (0)