starknet 4.9.0 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/README.md +1 -3
- package/__tests__/account.test.ts +5 -1
- package/__tests__/defaultProvider.test.ts +97 -210
- package/__tests__/fixtures.ts +2 -2
- package/__tests__/rpcProvider.test.ts +7 -4
- package/__tests__/udc.test.ts +41 -0
- package/account/default.d.ts +3 -1
- package/account/default.js +53 -3
- package/account/interface.d.ts +24 -7
- package/constants.d.ts +7 -1
- package/constants.js +7 -1
- package/contract/default.d.ts +11 -27
- package/contract/default.js +104 -120
- package/contract/interface.d.ts +5 -2
- package/dist/account/default.d.ts +3 -1
- package/dist/account/default.js +53 -3
- package/dist/account/interface.d.ts +24 -7
- package/dist/constants.d.ts +7 -1
- package/dist/constants.js +7 -1
- package/dist/contract/default.d.ts +11 -27
- package/dist/contract/default.js +104 -120
- package/dist/contract/interface.d.ts +5 -2
- package/dist/provider/default.d.ts +4 -2
- package/dist/provider/default.js +13 -2
- package/dist/provider/interface.d.ts +18 -3
- package/dist/provider/rpc.d.ts +15 -10
- package/dist/provider/rpc.js +71 -31
- package/dist/provider/sequencer.d.ts +6 -2
- package/dist/provider/sequencer.js +33 -5
- package/dist/signer/default.d.ts +2 -2
- package/dist/signer/default.js +15 -15
- package/dist/signer/interface.d.ts +2 -0
- package/dist/types/api/index.d.ts +0 -6
- package/dist/types/api/openrpc.d.ts +147 -98
- package/dist/types/api/openrpc.js +22 -4
- package/dist/types/index.d.ts +1 -1
- package/dist/types/lib.d.ts +22 -2
- package/dist/types/lib.js +6 -0
- package/dist/utils/number.d.ts +1 -0
- package/dist/utils/number.js +3 -1
- package/package.json +1 -1
- package/provider/default.d.ts +4 -2
- package/provider/default.js +13 -2
- package/provider/interface.d.ts +18 -3
- package/provider/rpc.d.ts +15 -10
- package/provider/rpc.js +71 -31
- package/provider/sequencer.d.ts +6 -2
- package/provider/sequencer.js +33 -5
- package/signer/default.d.ts +2 -2
- package/signer/default.js +15 -15
- package/signer/interface.d.ts +2 -0
- package/src/account/default.ts +51 -7
- package/src/account/interface.ts +34 -7
- package/src/constants.ts +7 -0
- package/src/contract/default.ts +123 -140
- package/src/contract/interface.ts +5 -2
- package/src/provider/default.ts +21 -3
- package/src/provider/interface.ts +26 -2
- package/src/provider/rpc.ts +73 -45
- package/src/provider/sequencer.ts +24 -4
- package/src/signer/default.ts +18 -18
- package/src/signer/interface.ts +2 -0
- package/src/types/api/index.ts +0 -4
- package/src/types/api/openrpc.ts +193 -105
- package/src/types/api/rpc.ts +0 -1
- package/src/types/index.ts +1 -1
- package/src/types/lib.ts +23 -2
- package/src/utils/number.ts +2 -0
- package/types/api/index.d.ts +0 -6
- package/types/api/openrpc.d.ts +147 -98
- package/types/api/openrpc.js +22 -4
- package/types/index.d.ts +1 -1
- package/types/lib.d.ts +22 -2
- package/types/lib.js +6 -0
- package/utils/number.d.ts +1 -0
- package/utils/number.js +3 -1
- package/www/docs/API/account.md +122 -22
- package/www/docs/API/contract.md +39 -3
- package/www/docs/API/provider.md +4 -0
- package/www/docs/API/signer.md +56 -2
package/src/account/default.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ZERO } from '../constants';
|
|
1
|
+
import { UDC, ZERO } from '../constants';
|
|
2
2
|
import { ProviderInterface, ProviderOptions } from '../provider';
|
|
3
3
|
import { Provider } from '../provider/default';
|
|
4
4
|
import { BlockIdentifier } from '../provider/utils';
|
|
@@ -16,9 +16,18 @@ import {
|
|
|
16
16
|
Signature,
|
|
17
17
|
} from '../types';
|
|
18
18
|
import { EstimateFee, EstimateFeeDetails } from '../types/account';
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
import {
|
|
20
|
+
AllowArray,
|
|
21
|
+
DeclareContractPayload,
|
|
22
|
+
DeployAccountContractPayload,
|
|
23
|
+
UniversalDeployerContractPayload,
|
|
24
|
+
} from '../types/lib';
|
|
25
|
+
import {
|
|
26
|
+
calculateContractAddressFromHash,
|
|
27
|
+
feeTransactionVersion,
|
|
28
|
+
transactionVersion,
|
|
29
|
+
} from '../utils/hash';
|
|
30
|
+
import { BigNumberish, toBN, toCairoBool } from '../utils/number';
|
|
22
31
|
import { parseContract } from '../utils/provider';
|
|
23
32
|
import { compileCalldata, estimatedFeeToMaxFee } from '../utils/stark';
|
|
24
33
|
import { fromCallsToExecuteCalldata } from '../utils/transaction';
|
|
@@ -58,7 +67,7 @@ export class Account extends Provider implements AccountInterface {
|
|
|
58
67
|
): Promise<EstimateFee> {
|
|
59
68
|
const transactions = Array.isArray(calls) ? calls : [calls];
|
|
60
69
|
const nonce = toBN(providedNonce ?? (await this.getNonce()));
|
|
61
|
-
const version = toBN(
|
|
70
|
+
const version = toBN(feeTransactionVersion);
|
|
62
71
|
const chainId = await this.getChainId();
|
|
63
72
|
|
|
64
73
|
const signerDetails: InvocationsSignerDetails = {
|
|
@@ -91,7 +100,7 @@ export class Account extends Provider implements AccountInterface {
|
|
|
91
100
|
{ blockIdentifier, nonce: providedNonce }: EstimateFeeDetails = {}
|
|
92
101
|
): Promise<EstimateFee> {
|
|
93
102
|
const nonce = toBN(providedNonce ?? (await this.getNonce()));
|
|
94
|
-
const version = toBN(
|
|
103
|
+
const version = toBN(feeTransactionVersion);
|
|
95
104
|
const chainId = await this.getChainId();
|
|
96
105
|
const contractDefinition = parseContract(contract);
|
|
97
106
|
|
|
@@ -127,7 +136,7 @@ export class Account extends Provider implements AccountInterface {
|
|
|
127
136
|
{ blockIdentifier, nonce: providedNonce }: EstimateFeeDetails = {}
|
|
128
137
|
): Promise<EstimateFee> {
|
|
129
138
|
const nonce = toBN(providedNonce ?? (await this.getNonce()));
|
|
130
|
-
const version = toBN(
|
|
139
|
+
const version = toBN(feeTransactionVersion);
|
|
131
140
|
const chainId = await this.getChainId();
|
|
132
141
|
const contractAddress =
|
|
133
142
|
providedContractAddress ??
|
|
@@ -228,6 +237,41 @@ export class Account extends Provider implements AccountInterface {
|
|
|
228
237
|
);
|
|
229
238
|
}
|
|
230
239
|
|
|
240
|
+
public async deploy(
|
|
241
|
+
{
|
|
242
|
+
classHash,
|
|
243
|
+
salt,
|
|
244
|
+
unique = true,
|
|
245
|
+
constructorCalldata = [],
|
|
246
|
+
isDevnet = false,
|
|
247
|
+
}: UniversalDeployerContractPayload,
|
|
248
|
+
additionalCalls: AllowArray<Call> = [], // support multicall
|
|
249
|
+
transactionsDetail: InvocationsDetails = {}
|
|
250
|
+
): Promise<InvokeFunctionResponse> {
|
|
251
|
+
const compiledConstructorCallData = compileCalldata(constructorCalldata);
|
|
252
|
+
|
|
253
|
+
const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
|
|
254
|
+
|
|
255
|
+
return this.execute(
|
|
256
|
+
[
|
|
257
|
+
{
|
|
258
|
+
contractAddress: isDevnet ? UDC.ADDRESS_DEVNET : UDC.ADDRESS,
|
|
259
|
+
entrypoint: UDC.ENTRYPOINT,
|
|
260
|
+
calldata: [
|
|
261
|
+
classHash,
|
|
262
|
+
salt,
|
|
263
|
+
toCairoBool(unique),
|
|
264
|
+
compiledConstructorCallData.length,
|
|
265
|
+
...compiledConstructorCallData,
|
|
266
|
+
],
|
|
267
|
+
},
|
|
268
|
+
...callsArray,
|
|
269
|
+
],
|
|
270
|
+
undefined,
|
|
271
|
+
transactionsDetail
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
231
275
|
public async deployAccount(
|
|
232
276
|
{
|
|
233
277
|
classHash,
|
package/src/account/interface.ts
CHANGED
|
@@ -13,7 +13,12 @@ import {
|
|
|
13
13
|
InvokeFunctionResponse,
|
|
14
14
|
Signature,
|
|
15
15
|
} from '../types';
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
AllowArray,
|
|
18
|
+
DeclareContractPayload,
|
|
19
|
+
DeployAccountContractPayload,
|
|
20
|
+
UniversalDeployerContractPayload,
|
|
21
|
+
} from '../types/lib';
|
|
17
22
|
import { BigNumberish } from '../utils/number';
|
|
18
23
|
import { TypedData } from '../utils/typedData/types';
|
|
19
24
|
|
|
@@ -71,7 +76,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
71
76
|
* Estimate Fee for executing a DEPLOY_ACCOUNT transaction on starknet
|
|
72
77
|
*
|
|
73
78
|
* @param contractPayload the payload object containing:
|
|
74
|
-
* - contract - the compiled contract to be
|
|
79
|
+
* - contract - the compiled contract to be deployed
|
|
75
80
|
* - classHash - the class hash of the compiled contract. This can be obtained by using starknet-cli.
|
|
76
81
|
*
|
|
77
82
|
* @returns response from estimate_fee
|
|
@@ -101,6 +106,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
101
106
|
|
|
102
107
|
/**
|
|
103
108
|
* Declares a given compiled contract (json) to starknet
|
|
109
|
+
*
|
|
104
110
|
* @param contractPayload transaction payload to be deployed containing:
|
|
105
111
|
- contract: compiled contract code
|
|
106
112
|
- classHash: computed class hash of compiled contract
|
|
@@ -116,13 +122,32 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
116
122
|
transactionsDetail?: InvocationsDetails
|
|
117
123
|
): Promise<DeclareContractResponse>;
|
|
118
124
|
|
|
125
|
+
/**
|
|
126
|
+
* @param deployContractPayload containing
|
|
127
|
+
* - classHash: computed class hash of compiled contract
|
|
128
|
+
* - salt: address salt
|
|
129
|
+
* - unique: bool if true ensure unique salt
|
|
130
|
+
* - calldata: constructor calldata
|
|
131
|
+
* @param additionalCalls - optional additional calls array to support multicall
|
|
132
|
+
* @param transactionsDetail Invocation Details containing:
|
|
133
|
+
* - optional nonce
|
|
134
|
+
* - optional version
|
|
135
|
+
* - optional maxFee
|
|
136
|
+
*/
|
|
137
|
+
public abstract deploy(
|
|
138
|
+
deployContractPayload: UniversalDeployerContractPayload,
|
|
139
|
+
additionalCalls?: AllowArray<Call>,
|
|
140
|
+
transactionsDetail?: InvocationsDetails
|
|
141
|
+
): Promise<InvokeFunctionResponse>;
|
|
142
|
+
|
|
119
143
|
/**
|
|
120
144
|
* Deploy the account on Starknet
|
|
145
|
+
*
|
|
121
146
|
* @param contractPayload transaction payload to be deployed containing:
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
-
|
|
147
|
+
- classHash: computed class hash of compiled contract
|
|
148
|
+
- optional constructor calldata
|
|
149
|
+
- optional address salt
|
|
150
|
+
- optional contractAddress
|
|
126
151
|
* @param transactionsDetail Invocation Details containing:
|
|
127
152
|
- optional nonce
|
|
128
153
|
- optional version
|
|
@@ -157,7 +182,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
157
182
|
/**
|
|
158
183
|
* Verify a signature of a JSON object
|
|
159
184
|
*
|
|
160
|
-
* @param
|
|
185
|
+
* @param typedData - JSON object to be verified
|
|
161
186
|
* @param signature - signature of the JSON object
|
|
162
187
|
* @returns true if the signature is valid, false otherwise
|
|
163
188
|
* @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
|
|
@@ -177,6 +202,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
177
202
|
|
|
178
203
|
/**
|
|
179
204
|
* Gets the nonce of the account with respect to a specific block
|
|
205
|
+
*
|
|
180
206
|
* @param {BlockIdentifier} blockIdentifier - optional blockIdentifier. Defaults to 'pending'
|
|
181
207
|
* @returns nonce of the account
|
|
182
208
|
*/
|
|
@@ -184,6 +210,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
184
210
|
|
|
185
211
|
/**
|
|
186
212
|
* Gets Suggested Max Fee based on the transaction type
|
|
213
|
+
*
|
|
187
214
|
* @param {EstimateFeeAction} estimateFeeAction
|
|
188
215
|
* @param {EstimateFeeDetails} details
|
|
189
216
|
* @returns suggestedMaxFee
|
package/src/constants.ts
CHANGED
|
@@ -11,6 +11,7 @@ export const MASK_251 = TWO.pow(toBN(251));
|
|
|
11
11
|
export enum StarknetChainId {
|
|
12
12
|
MAINNET = '0x534e5f4d41494e', // encodeShortString('SN_MAIN'),
|
|
13
13
|
TESTNET = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI'),
|
|
14
|
+
TESTNET2 = '0x534e5f474f45524c4932',
|
|
14
15
|
}
|
|
15
16
|
export enum TransactionHashPrefix {
|
|
16
17
|
DECLARE = '0x6465636c617265', // encodeShortString('declare'),
|
|
@@ -20,6 +21,12 @@ export enum TransactionHashPrefix {
|
|
|
20
21
|
L1_HANDLER = '0x6c315f68616e646c6572', // encodeShortString('l1_handler'),
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
export const UDC = {
|
|
25
|
+
ADDRESS: '0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf',
|
|
26
|
+
ENTRYPOINT: 'deployContract',
|
|
27
|
+
ADDRESS_DEVNET: '0x25fcb74260022bd8ed7e8d542408941826b53345e478b8303d6f31744838a36',
|
|
28
|
+
};
|
|
29
|
+
|
|
23
30
|
/**
|
|
24
31
|
* The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
|
|
25
32
|
* Please do not edit until the JSON changes.
|
package/src/contract/default.ts
CHANGED
|
@@ -204,30 +204,14 @@ export class Contract implements ContractInterface {
|
|
|
204
204
|
});
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
/**
|
|
208
|
-
* Saves the address of the contract deployed on network that will be used for interaction
|
|
209
|
-
*
|
|
210
|
-
* @param address - address of the contract
|
|
211
|
-
*/
|
|
212
207
|
public attach(address: string): void {
|
|
213
208
|
this.address = address;
|
|
214
209
|
}
|
|
215
210
|
|
|
216
|
-
/**
|
|
217
|
-
* Attaches to new Provider or Account
|
|
218
|
-
*
|
|
219
|
-
* @param providerOrAccount - new Provider or Account to attach to
|
|
220
|
-
*/
|
|
221
211
|
public connect(providerOrAccount: ProviderInterface | AccountInterface) {
|
|
222
212
|
this.providerOrAccount = providerOrAccount;
|
|
223
213
|
}
|
|
224
214
|
|
|
225
|
-
/**
|
|
226
|
-
* Resolves when contract is deployed on the network or when no deployment transaction is found
|
|
227
|
-
*
|
|
228
|
-
* @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
|
|
229
|
-
* @throws When deployment fails
|
|
230
|
-
*/
|
|
231
215
|
public async deployed(): Promise<Contract> {
|
|
232
216
|
if (this.deployTransactionHash) {
|
|
233
217
|
await this.providerOrAccount.waitForTransaction(this.deployTransactionHash);
|
|
@@ -236,6 +220,129 @@ export class Contract implements ContractInterface {
|
|
|
236
220
|
return this;
|
|
237
221
|
}
|
|
238
222
|
|
|
223
|
+
public async call(
|
|
224
|
+
method: string,
|
|
225
|
+
args: Array<any> = [],
|
|
226
|
+
{
|
|
227
|
+
blockIdentifier = 'pending',
|
|
228
|
+
}: {
|
|
229
|
+
blockIdentifier?: BlockIdentifier;
|
|
230
|
+
} = {}
|
|
231
|
+
): Promise<Result> {
|
|
232
|
+
// ensure contract is connected
|
|
233
|
+
assert(this.address !== null, 'contract is not connected to an address');
|
|
234
|
+
|
|
235
|
+
// validate method and args
|
|
236
|
+
this.validateMethodAndArgs('CALL', method, args);
|
|
237
|
+
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
238
|
+
|
|
239
|
+
// compile calldata
|
|
240
|
+
const calldata = this.compileCalldata(args, inputs);
|
|
241
|
+
return this.providerOrAccount
|
|
242
|
+
.callContract(
|
|
243
|
+
{
|
|
244
|
+
contractAddress: this.address,
|
|
245
|
+
calldata,
|
|
246
|
+
entrypoint: method,
|
|
247
|
+
},
|
|
248
|
+
blockIdentifier
|
|
249
|
+
)
|
|
250
|
+
.then((x) => this.parseResponse(method, x.result));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
public invoke(
|
|
254
|
+
method: string,
|
|
255
|
+
args: Array<any> = [],
|
|
256
|
+
options: Overrides = {}
|
|
257
|
+
): Promise<InvokeFunctionResponse> {
|
|
258
|
+
// ensure contract is connected
|
|
259
|
+
assert(this.address !== null, 'contract is not connected to an address');
|
|
260
|
+
// validate method and args
|
|
261
|
+
this.validateMethodAndArgs('INVOKE', method, args);
|
|
262
|
+
|
|
263
|
+
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
264
|
+
const inputsLength = inputs.reduce((acc, input) => {
|
|
265
|
+
if (!/_len$/.test(input.name)) {
|
|
266
|
+
return acc + 1;
|
|
267
|
+
}
|
|
268
|
+
return acc;
|
|
269
|
+
}, 0);
|
|
270
|
+
|
|
271
|
+
if (args.length !== inputsLength) {
|
|
272
|
+
throw Error(
|
|
273
|
+
`Invalid number of arguments, expected ${inputsLength} arguments, but got ${args.length}`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
// compile calldata
|
|
277
|
+
const calldata = this.compileCalldata(args, inputs);
|
|
278
|
+
|
|
279
|
+
const invocation = {
|
|
280
|
+
contractAddress: this.address,
|
|
281
|
+
calldata,
|
|
282
|
+
entrypoint: method,
|
|
283
|
+
};
|
|
284
|
+
if ('execute' in this.providerOrAccount) {
|
|
285
|
+
return this.providerOrAccount.execute(invocation, undefined, {
|
|
286
|
+
maxFee: options.maxFee,
|
|
287
|
+
nonce: options.nonce,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (!options.nonce) {
|
|
292
|
+
throw new Error(`Nonce is required when invoking a function without an account`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// eslint-disable-next-line no-console
|
|
296
|
+
console.warn(`Invoking ${method} without an account. This will not work on a public node.`);
|
|
297
|
+
|
|
298
|
+
return this.providerOrAccount.invokeFunction(
|
|
299
|
+
{
|
|
300
|
+
...invocation,
|
|
301
|
+
signature: options.signature || [],
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
nonce: options.nonce,
|
|
305
|
+
}
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
public async estimate(method: string, args: Array<any> = []) {
|
|
310
|
+
// ensure contract is connected
|
|
311
|
+
assert(this.address !== null, 'contract is not connected to an address');
|
|
312
|
+
|
|
313
|
+
// validate method and args
|
|
314
|
+
this.validateMethodAndArgs('INVOKE', method, args);
|
|
315
|
+
const invocation = this.populateTransaction[method](...args);
|
|
316
|
+
if ('estimateInvokeFee' in this.providerOrAccount) {
|
|
317
|
+
return this.providerOrAccount.estimateInvokeFee(invocation);
|
|
318
|
+
}
|
|
319
|
+
throw Error('Contract must be connected to the account contract to estimate');
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
public populate(method: string, args: Array<any> = []): Call {
|
|
323
|
+
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
324
|
+
return {
|
|
325
|
+
contractAddress: this.address,
|
|
326
|
+
entrypoint: method,
|
|
327
|
+
calldata: this.compileCalldata(args, inputs),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Deep parse of the object that has been passed to the method
|
|
333
|
+
*
|
|
334
|
+
* @param struct - struct that needs to be calculated
|
|
335
|
+
* @return {number} - number of members for the given struct
|
|
336
|
+
*/
|
|
337
|
+
private calculateStructMembers(struct: string): number {
|
|
338
|
+
return this.structs[struct].members.reduce((acc, member) => {
|
|
339
|
+
if (member.type === 'felt') {
|
|
340
|
+
return acc + 1;
|
|
341
|
+
}
|
|
342
|
+
return acc + this.calculateStructMembers(member.type);
|
|
343
|
+
}, 0);
|
|
344
|
+
}
|
|
345
|
+
|
|
239
346
|
/**
|
|
240
347
|
* Validates if all arguments that are passed to the method are corresponding to the ones in the abi
|
|
241
348
|
*
|
|
@@ -337,22 +444,6 @@ export class Contract implements ContractInterface {
|
|
|
337
444
|
});
|
|
338
445
|
}
|
|
339
446
|
|
|
340
|
-
/**
|
|
341
|
-
* Deep parse of the object that has been passed to the method
|
|
342
|
-
*
|
|
343
|
-
* @param struct - struct that needs to be calculated
|
|
344
|
-
* @return {number} - number of members for the given struct
|
|
345
|
-
*/
|
|
346
|
-
|
|
347
|
-
private calculateStructMembers(struct: string): number {
|
|
348
|
-
return this.structs[struct].members.reduce((acc, member) => {
|
|
349
|
-
if (member.type === 'felt') {
|
|
350
|
-
return acc + 1;
|
|
351
|
-
}
|
|
352
|
-
return acc + this.calculateStructMembers(member.type);
|
|
353
|
-
}, 0);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
447
|
/**
|
|
357
448
|
* Deep parse of the object that has been passed to the method
|
|
358
449
|
*
|
|
@@ -539,112 +630,4 @@ export class Contract implements ContractInterface {
|
|
|
539
630
|
return acc;
|
|
540
631
|
}, [] as Result);
|
|
541
632
|
}
|
|
542
|
-
|
|
543
|
-
public invoke(
|
|
544
|
-
method: string,
|
|
545
|
-
args: Array<any> = [],
|
|
546
|
-
options: Overrides = {}
|
|
547
|
-
): Promise<InvokeFunctionResponse> {
|
|
548
|
-
// ensure contract is connected
|
|
549
|
-
assert(this.address !== null, 'contract is not connected to an address');
|
|
550
|
-
// validate method and args
|
|
551
|
-
this.validateMethodAndArgs('INVOKE', method, args);
|
|
552
|
-
|
|
553
|
-
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
554
|
-
const inputsLength = inputs.reduce((acc, input) => {
|
|
555
|
-
if (!/_len$/.test(input.name)) {
|
|
556
|
-
return acc + 1;
|
|
557
|
-
}
|
|
558
|
-
return acc;
|
|
559
|
-
}, 0);
|
|
560
|
-
|
|
561
|
-
if (args.length !== inputsLength) {
|
|
562
|
-
throw Error(
|
|
563
|
-
`Invalid number of arguments, expected ${inputsLength} arguments, but got ${args.length}`
|
|
564
|
-
);
|
|
565
|
-
}
|
|
566
|
-
// compile calldata
|
|
567
|
-
const calldata = this.compileCalldata(args, inputs);
|
|
568
|
-
|
|
569
|
-
const invocation = {
|
|
570
|
-
contractAddress: this.address,
|
|
571
|
-
calldata,
|
|
572
|
-
entrypoint: method,
|
|
573
|
-
};
|
|
574
|
-
if ('execute' in this.providerOrAccount) {
|
|
575
|
-
return this.providerOrAccount.execute(invocation, undefined, {
|
|
576
|
-
maxFee: options.maxFee,
|
|
577
|
-
nonce: options.nonce,
|
|
578
|
-
});
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
if (!options.nonce) {
|
|
582
|
-
throw new Error(`Nonce is required when invoking a function without an account`);
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// eslint-disable-next-line no-console
|
|
586
|
-
console.warn(`Invoking ${method} without an account. This will not work on a public node.`);
|
|
587
|
-
|
|
588
|
-
return this.providerOrAccount.invokeFunction(
|
|
589
|
-
{
|
|
590
|
-
...invocation,
|
|
591
|
-
signature: options.signature || [],
|
|
592
|
-
},
|
|
593
|
-
{
|
|
594
|
-
nonce: options.nonce,
|
|
595
|
-
}
|
|
596
|
-
);
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
public async call(
|
|
600
|
-
method: string,
|
|
601
|
-
args: Array<any> = [],
|
|
602
|
-
{
|
|
603
|
-
blockIdentifier = 'pending',
|
|
604
|
-
}: {
|
|
605
|
-
blockIdentifier?: BlockIdentifier;
|
|
606
|
-
} = {}
|
|
607
|
-
): Promise<Result> {
|
|
608
|
-
// ensure contract is connected
|
|
609
|
-
assert(this.address !== null, 'contract is not connected to an address');
|
|
610
|
-
|
|
611
|
-
// validate method and args
|
|
612
|
-
this.validateMethodAndArgs('CALL', method, args);
|
|
613
|
-
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
614
|
-
|
|
615
|
-
// compile calldata
|
|
616
|
-
const calldata = this.compileCalldata(args, inputs);
|
|
617
|
-
return this.providerOrAccount
|
|
618
|
-
.callContract(
|
|
619
|
-
{
|
|
620
|
-
contractAddress: this.address,
|
|
621
|
-
calldata,
|
|
622
|
-
entrypoint: method,
|
|
623
|
-
},
|
|
624
|
-
blockIdentifier
|
|
625
|
-
)
|
|
626
|
-
.then((x) => this.parseResponse(method, x.result));
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
public async estimate(method: string, args: Array<any> = []) {
|
|
630
|
-
// ensure contract is connected
|
|
631
|
-
assert(this.address !== null, 'contract is not connected to an address');
|
|
632
|
-
|
|
633
|
-
// validate method and args
|
|
634
|
-
this.validateMethodAndArgs('INVOKE', method, args);
|
|
635
|
-
const invocation = this.populateTransaction[method](...args);
|
|
636
|
-
if ('estimateInvokeFee' in this.providerOrAccount) {
|
|
637
|
-
return this.providerOrAccount.estimateInvokeFee(invocation);
|
|
638
|
-
}
|
|
639
|
-
throw Error('Contract must be connected to the account contract to estimate');
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
public populate(method: string, args: Array<any> = []): Call {
|
|
643
|
-
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
644
|
-
return {
|
|
645
|
-
contractAddress: this.address,
|
|
646
|
-
entrypoint: method,
|
|
647
|
-
calldata: this.compileCalldata(args, inputs),
|
|
648
|
-
};
|
|
649
|
-
}
|
|
650
633
|
}
|
|
@@ -57,6 +57,7 @@ export abstract class ContractInterface {
|
|
|
57
57
|
*
|
|
58
58
|
* @param method name of the method
|
|
59
59
|
* @param args Array of the arguments for the call
|
|
60
|
+
* @param options optional blockIdentifier
|
|
60
61
|
* @returns Result of the call as an array with key value pars
|
|
61
62
|
*/
|
|
62
63
|
public abstract call(
|
|
@@ -72,6 +73,7 @@ export abstract class ContractInterface {
|
|
|
72
73
|
*
|
|
73
74
|
* @param method name of the method
|
|
74
75
|
* @param args Array of the arguments for the invoke
|
|
76
|
+
* @param options
|
|
75
77
|
* @returns Add Transaction Response
|
|
76
78
|
*/
|
|
77
79
|
public abstract invoke(
|
|
@@ -81,10 +83,11 @@ export abstract class ContractInterface {
|
|
|
81
83
|
): Promise<InvokeFunctionResponse>;
|
|
82
84
|
|
|
83
85
|
/**
|
|
84
|
-
*
|
|
86
|
+
* Estimates a method on a contract
|
|
85
87
|
*
|
|
86
88
|
* @param method name of the method
|
|
87
89
|
* @param args Array of the arguments for the call
|
|
90
|
+
* @param options optional blockIdentifier
|
|
88
91
|
*/
|
|
89
92
|
public abstract estimate(
|
|
90
93
|
method: string,
|
|
@@ -99,7 +102,7 @@ export abstract class ContractInterface {
|
|
|
99
102
|
*
|
|
100
103
|
* @param method name of the method
|
|
101
104
|
* @param args Array of the arguments for the call
|
|
102
|
-
* @returns Invocation
|
|
105
|
+
* @returns Invocation object
|
|
103
106
|
*/
|
|
104
107
|
public abstract populate(method: string, args?: Array<any>): Invocation;
|
|
105
108
|
}
|
package/src/provider/default.ts
CHANGED
|
@@ -15,7 +15,11 @@ import {
|
|
|
15
15
|
InvocationsDetailsWithNonce,
|
|
16
16
|
InvokeFunctionResponse,
|
|
17
17
|
} from '../types';
|
|
18
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
DeclareContractTransaction,
|
|
20
|
+
DeployAccountContractTransaction,
|
|
21
|
+
InvocationsDetails,
|
|
22
|
+
} from '../types/lib';
|
|
19
23
|
import { BigNumberish } from '../utils/number';
|
|
20
24
|
import { ProviderInterface } from './interface';
|
|
21
25
|
import { RpcProvider, RpcProviderOptions } from './rpc';
|
|
@@ -61,6 +65,17 @@ export class Provider implements ProviderInterface {
|
|
|
61
65
|
return this.provider.getClassAt(contractAddress, blockIdentifier);
|
|
62
66
|
}
|
|
63
67
|
|
|
68
|
+
public async getClassHashAt(
|
|
69
|
+
contractAddress: string,
|
|
70
|
+
blockIdentifier: BlockIdentifier = 'pending'
|
|
71
|
+
): Promise<string> {
|
|
72
|
+
return this.provider.getClassHashAt(contractAddress, blockIdentifier);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public getClass(classHash: string): Promise<ContractClass> {
|
|
76
|
+
return this.provider.getClass(classHash);
|
|
77
|
+
}
|
|
78
|
+
|
|
64
79
|
public async getEstimateFee(
|
|
65
80
|
invocationWithTxType: Invocation,
|
|
66
81
|
invocationDetails: InvocationsDetailsWithNonce,
|
|
@@ -118,8 +133,11 @@ export class Provider implements ProviderInterface {
|
|
|
118
133
|
return this.provider.invokeFunction(functionInvocation, details);
|
|
119
134
|
}
|
|
120
135
|
|
|
121
|
-
public async deployContract(
|
|
122
|
-
|
|
136
|
+
public async deployContract(
|
|
137
|
+
payload: DeployContractPayload,
|
|
138
|
+
details: InvocationsDetails
|
|
139
|
+
): Promise<DeployContractResponse> {
|
|
140
|
+
return this.provider.deployContract(payload, details);
|
|
123
141
|
}
|
|
124
142
|
|
|
125
143
|
public async deployAccountContract(
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
DeclareContractTransaction,
|
|
20
20
|
DeployAccountContractPayload,
|
|
21
21
|
DeployAccountContractTransaction,
|
|
22
|
+
InvocationsDetails,
|
|
22
23
|
} from '../types/lib';
|
|
23
24
|
import type { BigNumberish } from '../utils/number';
|
|
24
25
|
import { BlockIdentifier } from './utils';
|
|
@@ -73,6 +74,26 @@ export abstract class ProviderInterface {
|
|
|
73
74
|
blockIdentifier?: BlockIdentifier
|
|
74
75
|
): Promise<ContractClass>;
|
|
75
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Returns the class hash deployed under the given address.
|
|
79
|
+
*
|
|
80
|
+
* @param contractAddress - contract address
|
|
81
|
+
* @param blockIdentifier - block identifier
|
|
82
|
+
* @returns Class hash
|
|
83
|
+
*/
|
|
84
|
+
public abstract getClassHashAt(
|
|
85
|
+
contractAddress: string,
|
|
86
|
+
blockIdentifier?: BlockIdentifier
|
|
87
|
+
): Promise<string>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the contract class deployed under the given class hash.
|
|
91
|
+
*
|
|
92
|
+
* @param classHash - class hash
|
|
93
|
+
* @returns Contract class of compiled contract
|
|
94
|
+
*/
|
|
95
|
+
public abstract getClass(classHash: string): Promise<ContractClass>;
|
|
96
|
+
|
|
76
97
|
/**
|
|
77
98
|
* Gets the nonce of a contract with respect to a specific block
|
|
78
99
|
*
|
|
@@ -95,7 +116,7 @@ export abstract class ProviderInterface {
|
|
|
95
116
|
public abstract getStorageAt(
|
|
96
117
|
contractAddress: string,
|
|
97
118
|
key: BigNumberish,
|
|
98
|
-
blockIdentifier
|
|
119
|
+
blockIdentifier?: BlockIdentifier
|
|
99
120
|
): Promise<BigNumberish>;
|
|
100
121
|
|
|
101
122
|
/**
|
|
@@ -125,7 +146,10 @@ export abstract class ProviderInterface {
|
|
|
125
146
|
* - address salt
|
|
126
147
|
* @returns a confirmation of sending a transaction on the starknet contract
|
|
127
148
|
*/
|
|
128
|
-
public abstract deployContract(
|
|
149
|
+
public abstract deployContract(
|
|
150
|
+
payload: DeployContractPayload,
|
|
151
|
+
details?: InvocationsDetails
|
|
152
|
+
): Promise<DeployContractResponse>;
|
|
129
153
|
|
|
130
154
|
/**
|
|
131
155
|
* Deploys a given compiled Account contract (json) to starknet
|