starknet 4.17.0 → 4.18.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 +20 -0
- package/README.md +2 -10
- package/dist/index.d.ts +27 -9
- package/dist/index.global.js +30 -13
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +30 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -1
- package/.babelrc +0 -6
- package/.commitlintrc +0 -22
- package/.eslintignore +0 -2
- package/.eslintrc +0 -28
- package/.prettierrc +0 -6
- package/.releaserc +0 -28
- package/CODE_OF_CONDUCT.md +0 -128
- package/CONTRIBUTING.md +0 -52
- package/__mocks__/Account.json +0 -25486
- package/__mocks__/ERC20.json +0 -40027
- package/__mocks__/TestDapp.json +0 -12962
- package/__mocks__/contract.json +0 -33191
- package/__mocks__/l1l2_compiled.json +0 -10107
- package/__mocks__/multicall.json +0 -8139
- package/__mocks__/naming_compiled.json +0 -53283
- package/__mocks__/starknetId_compiled.json +0 -44703
- package/__mocks__/typedDataExample.json +0 -35
- package/__mocks__/typedDataSessionExample.json +0 -42
- package/__mocks__/typedDataStructArrayExample.json +0 -44
- package/__tests__/account.test.ts +0 -345
- package/__tests__/contract.test.ts +0 -249
- package/__tests__/defaultProvider.test.ts +0 -177
- package/__tests__/fixtures.ts +0 -84
- package/__tests__/jest.setup.ts +0 -30
- package/__tests__/rpcProvider.test.ts +0 -132
- package/__tests__/sequencerProvider.test.ts +0 -123
- package/__tests__/utils/__snapshots__/ellipticalCurve.test.ts.snap +0 -5
- package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +0 -5
- package/__tests__/utils/__snapshots__/utils.test.ts.snap +0 -5
- package/__tests__/utils/address.test.ts +0 -45
- package/__tests__/utils/ellipticalCurve.test.ts +0 -91
- package/__tests__/utils/merkle.test.ts +0 -256
- package/__tests__/utils/shortString.test.ts +0 -22
- package/__tests__/utils/starknetId.test.ts +0 -53
- package/__tests__/utils/transactionHash.test.ts +0 -17
- package/__tests__/utils/typedData.test.ts +0 -223
- package/__tests__/utils/uint256.test.ts +0 -32
- package/__tests__/utils/utils.browser.test.ts +0 -29
- package/__tests__/utils/utils.test.ts +0 -141
- package/img/logo.svg +0 -17
- package/index.d.ts +0 -2683
- package/index.global.js +0 -23192
- package/index.global.js.map +0 -1
- package/index.js +0 -4910
- package/index.js.map +0 -1
- package/index.mjs +0 -4852
- package/index.mjs.map +0 -1
- package/src/account/default.ts +0 -501
- package/src/account/index.ts +0 -2
- package/src/account/interface.ts +0 -309
- package/src/constants.ts +0 -2065
- package/src/contract/contractFactory.ts +0 -88
- package/src/contract/default.ts +0 -425
- package/src/contract/index.ts +0 -3
- package/src/contract/interface.ts +0 -106
- package/src/index.ts +0 -26
- package/src/provider/default.ts +0 -187
- package/src/provider/errors.ts +0 -14
- package/src/provider/index.ts +0 -9
- package/src/provider/interface.ts +0 -281
- package/src/provider/rpc.ts +0 -482
- package/src/provider/sequencer.ts +0 -523
- package/src/provider/utils.ts +0 -100
- package/src/signer/default.ts +0 -99
- package/src/signer/index.ts +0 -2
- package/src/signer/interface.ts +0 -77
- package/src/types/account.ts +0 -43
- package/src/types/api/index.ts +0 -10
- package/src/types/api/openrpc.ts +0 -607
- package/src/types/api/rpc.ts +0 -45
- package/src/types/api/sequencer.ts +0 -369
- package/src/types/contract.ts +0 -5
- package/src/types/index.ts +0 -8
- package/src/types/lib.ts +0 -143
- package/src/types/provider.ts +0 -134
- package/src/types/signer.ts +0 -24
- package/src/utils/address.ts +0 -44
- package/src/utils/calldata.ts +0 -250
- package/src/utils/ellipticCurve.ts +0 -120
- package/src/utils/encode.ts +0 -66
- package/src/utils/events.ts +0 -32
- package/src/utils/fetchPonyfill.ts +0 -5
- package/src/utils/hash.ts +0 -235
- package/src/utils/json.ts +0 -15
- package/src/utils/merkle.ts +0 -69
- package/src/utils/number.ts +0 -98
- package/src/utils/provider.ts +0 -28
- package/src/utils/responseParser/index.ts +0 -28
- package/src/utils/responseParser/rpc.ts +0 -70
- package/src/utils/responseParser/sequencer.ts +0 -135
- package/src/utils/shortString.ts +0 -21
- package/src/utils/stark.ts +0 -57
- package/src/utils/starknetId.ts +0 -116
- package/src/utils/transaction.ts +0 -56
- package/src/utils/typedData/index.ts +0 -247
- package/src/utils/typedData/types.ts +0 -37
- package/src/utils/typedData/utils.ts +0 -16
- package/src/utils/uint256.ts +0 -32
- package/src/utils/url.ts +0 -53
- package/tsconfig.eslint.json +0 -4
- package/tsup.config.ts +0 -9
- package/www/README.md +0 -41
- package/www/babel.config.js +0 -3
- package/www/code-examples/account.js +0 -65
- package/www/code-examples/amm.js +0 -44
- package/www/code-examples/erc20.js +0 -13
- package/www/code-examples/package-lock.json +0 -336
- package/www/code-examples/package.json +0 -15
- package/www/docs/API/_category_.json +0 -5
- package/www/docs/API/account.md +0 -463
- package/www/docs/API/changelog.md +0 -15
- package/www/docs/API/contract.md +0 -135
- package/www/docs/API/contractFactory.md +0 -45
- package/www/docs/API/index.md +0 -3
- package/www/docs/API/provider.md +0 -725
- package/www/docs/API/signer.md +0 -105
- package/www/docs/API/utils.md +0 -374
- package/www/docusaurus.config.js +0 -130
- package/www/guides/account.md +0 -140
- package/www/guides/cra.md +0 -3
- package/www/guides/erc20.md +0 -163
- package/www/guides/intro.md +0 -63
- package/www/package-lock.json +0 -20405
- package/www/package.json +0 -43
- package/www/sidebars.js +0 -31
- package/www/src/components/HomepageFeatures/index.tsx +0 -67
- package/www/src/components/HomepageFeatures/styles.module.css +0 -10
- package/www/src/css/custom.css +0 -39
- package/www/src/pages/index.module.css +0 -23
- package/www/src/pages/index.tsx +0 -40
- package/www/src/pages/markdown-page.md +0 -7
- package/www/static/.nojekyll +0 -0
- package/www/static/img/docusaurus.png +0 -0
- package/www/static/img/favicon.ico +0 -0
- package/www/static/img/logo.svg +0 -17
- package/www/static/img/starknet-1.png +0 -0
- package/www/static/img/starknet-2.png +0 -0
- package/www/static/img/starknet-3.png +0 -0
- package/www/static/img/tutorial/docsVersionDropdown.png +0 -0
- package/www/static/img/tutorial/localeDropdown.png +0 -0
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import assert from 'minimalistic-assert';
|
|
2
|
-
|
|
3
|
-
import { AccountInterface } from '../account';
|
|
4
|
-
import { Abi, CompiledContract, FunctionAbi } from '../types';
|
|
5
|
-
import { CheckCallData } from '../utils/calldata';
|
|
6
|
-
import { Contract } from './default';
|
|
7
|
-
|
|
8
|
-
export class ContractFactory {
|
|
9
|
-
abi: Abi;
|
|
10
|
-
|
|
11
|
-
compiledContract: CompiledContract;
|
|
12
|
-
|
|
13
|
-
classHash: string;
|
|
14
|
-
|
|
15
|
-
account: AccountInterface;
|
|
16
|
-
|
|
17
|
-
private checkCalldata: CheckCallData;
|
|
18
|
-
|
|
19
|
-
constructor(
|
|
20
|
-
compiledContract: CompiledContract,
|
|
21
|
-
classHash: string,
|
|
22
|
-
account: AccountInterface,
|
|
23
|
-
abi: Abi = compiledContract.abi // abi can be different from the deployed contract ie for proxy contracts
|
|
24
|
-
) {
|
|
25
|
-
this.abi = abi;
|
|
26
|
-
this.compiledContract = compiledContract;
|
|
27
|
-
this.account = account;
|
|
28
|
-
this.classHash = classHash;
|
|
29
|
-
this.checkCalldata = new CheckCallData(abi);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Deploys contract and returns new instance of the Contract
|
|
34
|
-
*
|
|
35
|
-
* @param args - Array of the constructor arguments for deployment
|
|
36
|
-
* @param addressSalt (optional) - Address Salt for deployment
|
|
37
|
-
* @returns deployed Contract
|
|
38
|
-
*/
|
|
39
|
-
public async deploy(args: Array<any> = [], addressSalt?: string | undefined): Promise<Contract> {
|
|
40
|
-
this.checkCalldata.validateMethodAndArgs('DEPLOY', 'constructor', args);
|
|
41
|
-
const { inputs } = this.abi.find((abi) => abi.type === 'constructor') as FunctionAbi;
|
|
42
|
-
|
|
43
|
-
// compile calldata
|
|
44
|
-
const constructorCalldata = this.checkCalldata.compileCalldata(args, inputs);
|
|
45
|
-
|
|
46
|
-
const {
|
|
47
|
-
deploy: { contract_address, transaction_hash },
|
|
48
|
-
} = await this.account.declareDeploy({
|
|
49
|
-
contract: this.compiledContract,
|
|
50
|
-
classHash: this.classHash,
|
|
51
|
-
constructorCalldata,
|
|
52
|
-
salt: addressSalt,
|
|
53
|
-
});
|
|
54
|
-
assert(Boolean(contract_address), 'Deployment of the contract failed');
|
|
55
|
-
|
|
56
|
-
const contractInstance = new Contract(
|
|
57
|
-
this.compiledContract.abi,
|
|
58
|
-
contract_address!,
|
|
59
|
-
this.account
|
|
60
|
-
);
|
|
61
|
-
contractInstance.deployTransactionHash = transaction_hash;
|
|
62
|
-
|
|
63
|
-
return contractInstance;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Attaches to new Provider or Account
|
|
68
|
-
*
|
|
69
|
-
* @param account - new Provider or Account to attach to
|
|
70
|
-
* @returns ContractFactory
|
|
71
|
-
*/
|
|
72
|
-
connect(account: AccountInterface): ContractFactory {
|
|
73
|
-
this.account = account;
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Attaches current abi and provider or account to the new address
|
|
79
|
-
*
|
|
80
|
-
* @param address - Contract address
|
|
81
|
-
* @returns Contract
|
|
82
|
-
*/
|
|
83
|
-
attach(address: string): Contract {
|
|
84
|
-
return new Contract(this.abi, address, this.account);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// ethers.js' getDeployTransaction cant be supported as it requires the account or signer to return a signed transaction which is not possible with the current implementation
|
|
88
|
-
}
|
package/src/contract/default.ts
DELETED
|
@@ -1,425 +0,0 @@
|
|
|
1
|
-
import BN from 'bn.js';
|
|
2
|
-
import assert from 'minimalistic-assert';
|
|
3
|
-
|
|
4
|
-
import { AccountInterface } from '../account';
|
|
5
|
-
import { ProviderInterface, defaultProvider } from '../provider';
|
|
6
|
-
import {
|
|
7
|
-
Abi,
|
|
8
|
-
AbiEntry,
|
|
9
|
-
Args,
|
|
10
|
-
AsyncContractFunction,
|
|
11
|
-
BlockTag,
|
|
12
|
-
Call,
|
|
13
|
-
ContractFunction,
|
|
14
|
-
FunctionAbi,
|
|
15
|
-
InvokeFunctionResponse,
|
|
16
|
-
Overrides,
|
|
17
|
-
ParsedStruct,
|
|
18
|
-
Result,
|
|
19
|
-
StructAbi,
|
|
20
|
-
} from '../types';
|
|
21
|
-
import { CheckCallData } from '../utils/calldata';
|
|
22
|
-
import { BigNumberish, toBN } from '../utils/number';
|
|
23
|
-
import { CallOptions, ContractInterface } from './interface';
|
|
24
|
-
|
|
25
|
-
function parseFelt(candidate: string): BN {
|
|
26
|
-
try {
|
|
27
|
-
return toBN(candidate);
|
|
28
|
-
} catch (e) {
|
|
29
|
-
throw Error('Could not parse felt');
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Adds call methods to the contract
|
|
35
|
-
*
|
|
36
|
-
*/
|
|
37
|
-
function buildCall(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
|
|
38
|
-
return async function (...args: Array<any>): Promise<any> {
|
|
39
|
-
let blockIdentifier: BlockTag | null = null;
|
|
40
|
-
|
|
41
|
-
args.forEach((arg) => {
|
|
42
|
-
if (arg.blockIdentifier) {
|
|
43
|
-
blockIdentifier = arg.blockIdentifier;
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
return contract.call(functionAbi.name, args, { blockIdentifier });
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Adds invoke methods to the contract
|
|
53
|
-
*
|
|
54
|
-
*/
|
|
55
|
-
function buildInvoke(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
|
|
56
|
-
return async function (...args: Array<any>): Promise<any> {
|
|
57
|
-
const { inputs } = functionAbi;
|
|
58
|
-
const inputsLength = inputs.reduce((acc, input) => {
|
|
59
|
-
if (!/_len$/.test(input.name)) {
|
|
60
|
-
return acc + 1;
|
|
61
|
-
}
|
|
62
|
-
return acc;
|
|
63
|
-
}, 0);
|
|
64
|
-
const options = {};
|
|
65
|
-
if (inputsLength + 1 === args.length && typeof args[args.length - 1] === 'object') {
|
|
66
|
-
Object.assign(options, args.pop());
|
|
67
|
-
}
|
|
68
|
-
return contract.invoke(functionAbi.name, args, options);
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Adds call/invoke methods to the contract
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
function buildDefault(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
|
|
77
|
-
if (functionAbi.stateMutability === 'view') {
|
|
78
|
-
return buildCall(contract, functionAbi);
|
|
79
|
-
}
|
|
80
|
-
return buildInvoke(contract, functionAbi);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Adds populate for methods to the contract
|
|
85
|
-
*
|
|
86
|
-
*/
|
|
87
|
-
function buildPopulate(contract: Contract, functionAbi: FunctionAbi): ContractFunction {
|
|
88
|
-
return function (...args: Array<any>): any {
|
|
89
|
-
return contract.populate(functionAbi.name, args);
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Adds estimateFee for methods to the contract
|
|
95
|
-
*
|
|
96
|
-
*/
|
|
97
|
-
function buildEstimate(contract: Contract, functionAbi: FunctionAbi): ContractFunction {
|
|
98
|
-
return function (...args: Array<any>): any {
|
|
99
|
-
return contract.estimate(functionAbi.name, args);
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export class Contract implements ContractInterface {
|
|
104
|
-
abi: Abi;
|
|
105
|
-
|
|
106
|
-
address: string;
|
|
107
|
-
|
|
108
|
-
providerOrAccount: ProviderInterface | AccountInterface;
|
|
109
|
-
|
|
110
|
-
deployTransactionHash?: string;
|
|
111
|
-
|
|
112
|
-
protected readonly structs: { [name: string]: StructAbi };
|
|
113
|
-
|
|
114
|
-
readonly functions!: { [name: string]: AsyncContractFunction };
|
|
115
|
-
|
|
116
|
-
readonly callStatic!: { [name: string]: AsyncContractFunction };
|
|
117
|
-
|
|
118
|
-
readonly populateTransaction!: { [name: string]: ContractFunction };
|
|
119
|
-
|
|
120
|
-
readonly estimateFee!: { [name: string]: ContractFunction };
|
|
121
|
-
|
|
122
|
-
readonly [key: string]: AsyncContractFunction | any;
|
|
123
|
-
|
|
124
|
-
private checkCalldata: CheckCallData;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Contract class to handle contract methods
|
|
128
|
-
*
|
|
129
|
-
* @param abi - Abi of the contract object
|
|
130
|
-
* @param address (optional) - address to connect to
|
|
131
|
-
* @param providerOrAccount (optional) - Provider or Account to attach to
|
|
132
|
-
*/
|
|
133
|
-
constructor(
|
|
134
|
-
abi: Abi,
|
|
135
|
-
address: string,
|
|
136
|
-
providerOrAccount: ProviderInterface | AccountInterface = defaultProvider
|
|
137
|
-
) {
|
|
138
|
-
this.address = address && address.toLowerCase();
|
|
139
|
-
this.providerOrAccount = providerOrAccount;
|
|
140
|
-
this.abi = abi;
|
|
141
|
-
this.structs = abi
|
|
142
|
-
.filter((abiEntry) => abiEntry.type === 'struct')
|
|
143
|
-
.reduce(
|
|
144
|
-
(acc, abiEntry) => ({
|
|
145
|
-
...acc,
|
|
146
|
-
[abiEntry.name]: abiEntry,
|
|
147
|
-
}),
|
|
148
|
-
{}
|
|
149
|
-
);
|
|
150
|
-
this.checkCalldata = new CheckCallData(abi);
|
|
151
|
-
|
|
152
|
-
Object.defineProperty(this, 'functions', {
|
|
153
|
-
enumerable: true,
|
|
154
|
-
value: {},
|
|
155
|
-
writable: false,
|
|
156
|
-
});
|
|
157
|
-
Object.defineProperty(this, 'callStatic', {
|
|
158
|
-
enumerable: true,
|
|
159
|
-
value: {},
|
|
160
|
-
writable: false,
|
|
161
|
-
});
|
|
162
|
-
Object.defineProperty(this, 'populateTransaction', {
|
|
163
|
-
enumerable: true,
|
|
164
|
-
value: {},
|
|
165
|
-
writable: false,
|
|
166
|
-
});
|
|
167
|
-
Object.defineProperty(this, 'estimateFee', {
|
|
168
|
-
enumerable: true,
|
|
169
|
-
value: {},
|
|
170
|
-
writable: false,
|
|
171
|
-
});
|
|
172
|
-
this.abi.forEach((abiElement) => {
|
|
173
|
-
if (abiElement.type !== 'function') {
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
const signature = abiElement.name;
|
|
177
|
-
if (!this[signature]) {
|
|
178
|
-
Object.defineProperty(this, signature, {
|
|
179
|
-
enumerable: true,
|
|
180
|
-
value: buildDefault(this, abiElement),
|
|
181
|
-
writable: false,
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
if (!this.functions[signature]) {
|
|
185
|
-
Object.defineProperty(this.functions, signature, {
|
|
186
|
-
enumerable: true,
|
|
187
|
-
value: buildDefault(this, abiElement),
|
|
188
|
-
writable: false,
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (!this.callStatic[signature]) {
|
|
193
|
-
Object.defineProperty(this.callStatic, signature, {
|
|
194
|
-
enumerable: true,
|
|
195
|
-
value: buildCall(this, abiElement),
|
|
196
|
-
writable: false,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (!this.populateTransaction[signature]) {
|
|
201
|
-
Object.defineProperty(this.populateTransaction, signature, {
|
|
202
|
-
enumerable: true,
|
|
203
|
-
value: buildPopulate(this, abiElement),
|
|
204
|
-
writable: false,
|
|
205
|
-
});
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (!this.estimateFee[signature]) {
|
|
209
|
-
Object.defineProperty(this.estimateFee, signature, {
|
|
210
|
-
enumerable: true,
|
|
211
|
-
value: buildEstimate(this, abiElement),
|
|
212
|
-
writable: false,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
public attach(address: string): void {
|
|
219
|
-
this.address = address;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
public connect(providerOrAccount: ProviderInterface | AccountInterface) {
|
|
223
|
-
this.providerOrAccount = providerOrAccount;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
public async deployed(): Promise<Contract> {
|
|
227
|
-
if (this.deployTransactionHash) {
|
|
228
|
-
await this.providerOrAccount.waitForTransaction(this.deployTransactionHash);
|
|
229
|
-
this.deployTransactionHash = undefined;
|
|
230
|
-
}
|
|
231
|
-
return this;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
public async call(
|
|
235
|
-
method: string,
|
|
236
|
-
args: Array<any> = [],
|
|
237
|
-
options: CallOptions = {}
|
|
238
|
-
): Promise<Result> {
|
|
239
|
-
// default value also for null
|
|
240
|
-
const blockIdentifier = options?.blockIdentifier || undefined;
|
|
241
|
-
|
|
242
|
-
// ensure contract is connected
|
|
243
|
-
assert(this.address !== null, 'contract is not connected to an address');
|
|
244
|
-
|
|
245
|
-
// validate method and args
|
|
246
|
-
this.checkCalldata.validateMethodAndArgs('CALL', method, args);
|
|
247
|
-
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
248
|
-
|
|
249
|
-
// compile calldata
|
|
250
|
-
const calldata = this.checkCalldata.compileCalldata(args, inputs);
|
|
251
|
-
return this.providerOrAccount
|
|
252
|
-
.callContract(
|
|
253
|
-
{
|
|
254
|
-
contractAddress: this.address,
|
|
255
|
-
calldata,
|
|
256
|
-
entrypoint: method,
|
|
257
|
-
},
|
|
258
|
-
blockIdentifier
|
|
259
|
-
)
|
|
260
|
-
.then((x) => this.parseResponse(method, x.result));
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
public invoke(
|
|
264
|
-
method: string,
|
|
265
|
-
args: Array<any> = [],
|
|
266
|
-
options: Overrides = {}
|
|
267
|
-
): Promise<InvokeFunctionResponse> {
|
|
268
|
-
// ensure contract is connected
|
|
269
|
-
assert(this.address !== null, 'contract is not connected to an address');
|
|
270
|
-
// validate method and args
|
|
271
|
-
this.checkCalldata.validateMethodAndArgs('INVOKE', method, args);
|
|
272
|
-
|
|
273
|
-
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
274
|
-
const inputsLength = inputs.reduce((acc, input) => {
|
|
275
|
-
if (!/_len$/.test(input.name)) {
|
|
276
|
-
return acc + 1;
|
|
277
|
-
}
|
|
278
|
-
return acc;
|
|
279
|
-
}, 0);
|
|
280
|
-
|
|
281
|
-
if (args.length !== inputsLength) {
|
|
282
|
-
throw Error(
|
|
283
|
-
`Invalid number of arguments, expected ${inputsLength} arguments, but got ${args.length}`
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
// compile calldata
|
|
287
|
-
const calldata = this.checkCalldata.compileCalldata(args, inputs);
|
|
288
|
-
|
|
289
|
-
const invocation = {
|
|
290
|
-
contractAddress: this.address,
|
|
291
|
-
calldata,
|
|
292
|
-
entrypoint: method,
|
|
293
|
-
};
|
|
294
|
-
if ('execute' in this.providerOrAccount) {
|
|
295
|
-
return this.providerOrAccount.execute(invocation, undefined, {
|
|
296
|
-
maxFee: options.maxFee,
|
|
297
|
-
nonce: options.nonce,
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (!options.nonce) {
|
|
302
|
-
throw new Error(`Nonce is required when invoking a function without an account`);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// eslint-disable-next-line no-console
|
|
306
|
-
console.warn(`Invoking ${method} without an account. This will not work on a public node.`);
|
|
307
|
-
|
|
308
|
-
return this.providerOrAccount.invokeFunction(
|
|
309
|
-
{
|
|
310
|
-
...invocation,
|
|
311
|
-
signature: options.signature || [],
|
|
312
|
-
},
|
|
313
|
-
{
|
|
314
|
-
nonce: options.nonce,
|
|
315
|
-
}
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
public async estimate(method: string, args: Array<any> = []) {
|
|
320
|
-
// ensure contract is connected
|
|
321
|
-
assert(this.address !== null, 'contract is not connected to an address');
|
|
322
|
-
|
|
323
|
-
// validate method and args
|
|
324
|
-
this.checkCalldata.validateMethodAndArgs('INVOKE', method, args);
|
|
325
|
-
const invocation = this.populateTransaction[method](...args);
|
|
326
|
-
if ('estimateInvokeFee' in this.providerOrAccount) {
|
|
327
|
-
return this.providerOrAccount.estimateInvokeFee(invocation);
|
|
328
|
-
}
|
|
329
|
-
throw Error('Contract must be connected to the account contract to estimate');
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
public populate(method: string, args: Array<any> = []): Call {
|
|
333
|
-
const { inputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
334
|
-
return {
|
|
335
|
-
contractAddress: this.address,
|
|
336
|
-
entrypoint: method,
|
|
337
|
-
calldata: this.checkCalldata.compileCalldata(args, inputs),
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Parse of the response elements that are converted to Object (Struct) by using the abi
|
|
343
|
-
*
|
|
344
|
-
* @param responseIterator - iterator of the response
|
|
345
|
-
* @param type - type of the struct
|
|
346
|
-
* @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
|
|
347
|
-
*/
|
|
348
|
-
protected parseResponseStruct(
|
|
349
|
-
responseIterator: Iterator<string>,
|
|
350
|
-
type: string
|
|
351
|
-
): BigNumberish | ParsedStruct {
|
|
352
|
-
// check the type of current element
|
|
353
|
-
if (type in this.structs && this.structs[type]) {
|
|
354
|
-
return this.structs[type].members.reduce((acc, el) => {
|
|
355
|
-
// parse each member of the struct (member can felt or nested struct)
|
|
356
|
-
acc[el.name] = this.parseResponseStruct(responseIterator, el.type);
|
|
357
|
-
return acc;
|
|
358
|
-
}, {} as any);
|
|
359
|
-
}
|
|
360
|
-
return parseFelt(responseIterator.next().value);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* Parse elements of the response and structuring them into one field by using output property from the abi for that method
|
|
365
|
-
*
|
|
366
|
-
* @param responseIterator - iterator of the response
|
|
367
|
-
* @param output - output(field) information from the abi that will be used to parse the data
|
|
368
|
-
* @return - parsed response corresponding to the abi structure of the field
|
|
369
|
-
*/
|
|
370
|
-
protected parseResponseField(
|
|
371
|
-
responseIterator: Iterator<string>,
|
|
372
|
-
output: AbiEntry,
|
|
373
|
-
parsedResult?: Args
|
|
374
|
-
): any {
|
|
375
|
-
const { name, type } = output;
|
|
376
|
-
const parsedDataArr: (BigNumberish | ParsedStruct)[] = [];
|
|
377
|
-
switch (true) {
|
|
378
|
-
case /_len$/.test(name):
|
|
379
|
-
return parseFelt(responseIterator.next().value).toNumber();
|
|
380
|
-
case /\(felt/.test(type):
|
|
381
|
-
return type.split(',').reduce((acc) => {
|
|
382
|
-
acc.push(parseFelt(responseIterator.next().value));
|
|
383
|
-
return acc;
|
|
384
|
-
}, [] as BigNumberish[]);
|
|
385
|
-
case /\*/.test(type):
|
|
386
|
-
if (parsedResult && parsedResult[`${name}_len`]) {
|
|
387
|
-
const arrLen = parsedResult[`${name}_len`] as number;
|
|
388
|
-
while (parsedDataArr.length < arrLen) {
|
|
389
|
-
parsedDataArr.push(
|
|
390
|
-
this.parseResponseStruct(responseIterator, output.type.replace('*', ''))
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
return parsedDataArr;
|
|
395
|
-
case type in this.structs:
|
|
396
|
-
return this.parseResponseStruct(responseIterator, type);
|
|
397
|
-
default:
|
|
398
|
-
return parseFelt(responseIterator.next().value);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Parse elements of the response array and structuring them into response object
|
|
404
|
-
*
|
|
405
|
-
* @param method - method name
|
|
406
|
-
* @param response - response from the method
|
|
407
|
-
* @return - parsed response corresponding to the abi
|
|
408
|
-
*/
|
|
409
|
-
protected parseResponse(method: string, response: string[]): Result {
|
|
410
|
-
const { outputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
|
|
411
|
-
const responseIterator = response.flat()[Symbol.iterator]();
|
|
412
|
-
const resultObject = outputs.flat().reduce((acc, output) => {
|
|
413
|
-
acc[output.name] = this.parseResponseField(responseIterator, output, acc);
|
|
414
|
-
if (acc[output.name] && acc[`${output.name}_len`]) {
|
|
415
|
-
delete acc[`${output.name}_len`];
|
|
416
|
-
}
|
|
417
|
-
return acc;
|
|
418
|
-
}, {} as Args);
|
|
419
|
-
return Object.entries(resultObject).reduce((acc, [key, value]) => {
|
|
420
|
-
acc.push(value);
|
|
421
|
-
acc[key] = value;
|
|
422
|
-
return acc;
|
|
423
|
-
}, [] as Result);
|
|
424
|
-
}
|
|
425
|
-
}
|
package/src/contract/index.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { AccountInterface } from '../account';
|
|
2
|
-
import { ProviderInterface } from '../provider';
|
|
3
|
-
import { BlockIdentifier } from '../provider/utils';
|
|
4
|
-
import {
|
|
5
|
-
Abi,
|
|
6
|
-
AsyncContractFunction,
|
|
7
|
-
ContractFunction,
|
|
8
|
-
Invocation,
|
|
9
|
-
InvokeFunctionResponse,
|
|
10
|
-
Overrides,
|
|
11
|
-
Result,
|
|
12
|
-
} from '../types';
|
|
13
|
-
|
|
14
|
-
export type CallOptions = {
|
|
15
|
-
blockIdentifier?: BlockIdentifier;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export abstract class ContractInterface {
|
|
19
|
-
public abstract abi: Abi;
|
|
20
|
-
|
|
21
|
-
public abstract address: string;
|
|
22
|
-
|
|
23
|
-
public abstract providerOrAccount: ProviderInterface | AccountInterface;
|
|
24
|
-
|
|
25
|
-
public abstract deployTransactionHash?: string;
|
|
26
|
-
|
|
27
|
-
readonly functions!: { [name: string]: AsyncContractFunction };
|
|
28
|
-
|
|
29
|
-
readonly callStatic!: { [name: string]: AsyncContractFunction };
|
|
30
|
-
|
|
31
|
-
readonly populateTransaction!: { [name: string]: ContractFunction };
|
|
32
|
-
|
|
33
|
-
readonly estimateFee!: { [name: string]: ContractFunction };
|
|
34
|
-
|
|
35
|
-
readonly [key: string]: AsyncContractFunction | any;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Saves the address of the contract deployed on network that will be used for interaction
|
|
39
|
-
*
|
|
40
|
-
* @param address - address of the contract
|
|
41
|
-
*/
|
|
42
|
-
public abstract attach(address: string): void;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Attaches to new Provider or Account
|
|
46
|
-
*
|
|
47
|
-
* @param providerOrAccount - new Provider or Account to attach to
|
|
48
|
-
*/
|
|
49
|
-
public abstract connect(providerOrAccount: ProviderInterface | AccountInterface): void;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Resolves when contract is deployed on the network or when no deployment transaction is found
|
|
53
|
-
*
|
|
54
|
-
* @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
|
|
55
|
-
* @throws When deployment fails
|
|
56
|
-
*/
|
|
57
|
-
public abstract deployed(): Promise<ContractInterface>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Calls a method on a contract
|
|
61
|
-
*
|
|
62
|
-
* @param method name of the method
|
|
63
|
-
* @param args Array of the arguments for the call
|
|
64
|
-
* @param options optional blockIdentifier
|
|
65
|
-
* @returns Result of the call as an array with key value pars
|
|
66
|
-
*/
|
|
67
|
-
public abstract call(method: string, args?: Array<any>, options?: CallOptions): Promise<Result>;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Invokes a method on a contract
|
|
71
|
-
*
|
|
72
|
-
* @param method name of the method
|
|
73
|
-
* @param args Array of the arguments for the invoke
|
|
74
|
-
* @param options
|
|
75
|
-
* @returns Add Transaction Response
|
|
76
|
-
*/
|
|
77
|
-
public abstract invoke(
|
|
78
|
-
method: string,
|
|
79
|
-
args?: Array<any>,
|
|
80
|
-
options?: Overrides
|
|
81
|
-
): Promise<InvokeFunctionResponse>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Estimates a method on a contract
|
|
85
|
-
*
|
|
86
|
-
* @param method name of the method
|
|
87
|
-
* @param args Array of the arguments for the call
|
|
88
|
-
* @param options optional blockIdentifier
|
|
89
|
-
*/
|
|
90
|
-
public abstract estimate(
|
|
91
|
-
method: string,
|
|
92
|
-
args?: Array<any>,
|
|
93
|
-
options?: {
|
|
94
|
-
blockIdentifier?: BlockIdentifier;
|
|
95
|
-
}
|
|
96
|
-
): Promise<any>;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Calls a method on a contract
|
|
100
|
-
*
|
|
101
|
-
* @param method name of the method
|
|
102
|
-
* @param args Array of the arguments for the call
|
|
103
|
-
* @returns Invocation object
|
|
104
|
-
*/
|
|
105
|
-
public abstract populate(method: string, args?: Array<any>): Invocation;
|
|
106
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Main
|
|
3
|
-
*/
|
|
4
|
-
export * from './contract';
|
|
5
|
-
export * from './types';
|
|
6
|
-
export * from './provider';
|
|
7
|
-
export * from './account';
|
|
8
|
-
export * from './signer';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Utils
|
|
12
|
-
*/
|
|
13
|
-
export * as constants from './constants';
|
|
14
|
-
export * as encode from './utils/encode';
|
|
15
|
-
export * as hash from './utils/hash';
|
|
16
|
-
export * as json from './utils/json';
|
|
17
|
-
export * as number from './utils/number';
|
|
18
|
-
export * as transaction from './utils/transaction';
|
|
19
|
-
export * as stark from './utils/stark';
|
|
20
|
-
export * as merkle from './utils/merkle';
|
|
21
|
-
export * as ec from './utils/ellipticCurve';
|
|
22
|
-
export * as uint256 from './utils/uint256';
|
|
23
|
-
export * as shortString from './utils/shortString';
|
|
24
|
-
export * as typedData from './utils/typedData';
|
|
25
|
-
export * from './utils/address';
|
|
26
|
-
export * from './utils/url';
|