starknet 3.18.2 → 4.0.1
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 +70 -0
- package/README.md +1 -2
- package/__tests__/account.test.ts +11 -56
- package/__tests__/contract.test.ts +11 -49
- package/__tests__/defaultProvider.test.ts +321 -0
- package/__tests__/fixtures.ts +32 -11
- package/__tests__/jest.setup.ts +2 -3
- package/__tests__/rpcProvider.test.ts +17 -0
- package/__tests__/sequencerProvider.test.ts +45 -0
- package/account/default.d.ts +54 -77
- package/account/default.js +271 -596
- package/account/index.js +18 -31
- package/account/interface.d.ts +66 -95
- package/account/interface.js +20 -30
- package/constants.d.ts +17 -19
- package/constants.js +2038 -2059
- package/contract/contractFactory.d.ts +25 -29
- package/contract/contractFactory.js +94 -210
- package/contract/default.d.ts +117 -146
- package/contract/default.js +582 -776
- package/contract/index.js +19 -32
- package/contract/interface.d.ts +72 -92
- package/contract/interface.js +6 -5
- package/dist/account/default.d.ts +5 -9
- package/dist/account/default.js +35 -169
- package/dist/account/interface.d.ts +3 -15
- package/dist/contract/contractFactory.js +4 -4
- package/dist/contract/default.d.ts +3 -3
- package/dist/contract/default.js +3 -2
- package/dist/contract/interface.d.ts +2 -2
- package/dist/provider/default.d.ts +18 -134
- package/dist/provider/default.js +47 -411
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +2 -0
- package/dist/provider/interface.d.ts +45 -50
- package/dist/provider/rpc.d.ts +57 -0
- package/dist/provider/rpc.js +364 -0
- package/dist/provider/sequencer.d.ts +66 -0
- package/dist/provider/sequencer.js +444 -0
- package/dist/types/account.d.ts +2 -3
- package/dist/types/api/index.d.ts +16 -0
- package/dist/types/api/index.js +18 -0
- package/dist/types/api/rpc.d.ts +221 -0
- package/dist/types/{api.js → api/rpc.js} +0 -0
- package/dist/types/api/sequencer.d.ts +289 -0
- package/dist/types/api/sequencer.js +2 -0
- package/dist/types/index.d.ts +3 -1
- package/dist/types/index.js +15 -1
- package/dist/types/lib.d.ts +3 -1
- package/dist/types/provider.d.ts +86 -0
- package/dist/types/provider.js +2 -0
- package/dist/utils/fetchPonyfill.d.ts +2 -0
- package/dist/utils/fetchPonyfill.js +6 -0
- package/dist/utils/provider.d.ts +4 -0
- package/dist/utils/provider.js +38 -0
- package/dist/utils/responseParser/index.d.ts +11 -0
- package/dist/utils/responseParser/index.js +9 -0
- package/dist/utils/responseParser/rpc.d.ts +13 -0
- package/dist/utils/responseParser/rpc.js +96 -0
- package/dist/utils/responseParser/sequencer.d.ts +13 -0
- package/dist/utils/responseParser/sequencer.js +124 -0
- package/index.js +42 -75
- package/package.json +2 -3
- package/provider/default.d.ts +21 -175
- package/provider/default.js +139 -704
- package/provider/errors.d.ts +4 -4
- package/provider/errors.js +30 -40
- package/provider/index.d.ts +2 -0
- package/provider/index.js +22 -33
- package/provider/interface.d.ts +104 -131
- package/provider/interface.js +6 -5
- package/provider/rpc.d.ts +57 -0
- package/provider/rpc.js +364 -0
- package/provider/sequencer.d.ts +66 -0
- package/provider/sequencer.js +444 -0
- package/provider/utils.d.ts +7 -9
- package/provider/utils.js +39 -44
- package/signer/default.d.ts +5 -9
- package/signer/default.js +72 -177
- package/signer/index.js +18 -31
- package/signer/interface.d.ts +29 -33
- package/signer/interface.js +6 -5
- package/src/account/default.ts +26 -146
- package/src/account/interface.ts +5 -20
- package/src/contract/contractFactory.ts +3 -6
- package/src/contract/default.ts +6 -4
- package/src/contract/interface.ts +2 -2
- package/src/provider/default.ts +63 -395
- package/src/provider/index.ts +2 -0
- package/src/provider/interface.ts +68 -63
- package/src/provider/rpc.ts +299 -0
- package/src/provider/sequencer.ts +385 -0
- package/src/types/account.ts +2 -3
- package/src/types/api/index.ts +17 -0
- package/src/types/api/rpc.ts +247 -0
- package/src/types/api/sequencer.ts +331 -0
- package/src/types/index.ts +3 -1
- package/src/types/lib.ts +3 -1
- package/src/types/provider.ts +108 -0
- package/src/utils/fetchPonyfill.ts +4 -0
- package/src/utils/provider.ts +28 -0
- package/src/utils/responseParser/index.ts +28 -0
- package/src/utils/responseParser/rpc.ts +93 -0
- package/src/utils/responseParser/sequencer.ts +127 -0
- package/types/account.d.ts +5 -7
- package/types/account.js +2 -2
- package/types/api/index.d.ts +16 -0
- package/types/api/index.js +18 -0
- package/types/api/rpc.d.ts +221 -0
- package/types/api/rpc.js +2 -0
- package/types/api/sequencer.d.ts +289 -0
- package/types/api/sequencer.js +2 -0
- package/types/contract.d.ts +1 -1
- package/types/contract.js +2 -2
- package/types/index.d.ts +3 -1
- package/types/index.js +35 -34
- package/types/lib.d.ts +36 -41
- package/types/lib.js +2 -2
- package/types/provider.d.ts +86 -0
- package/types/provider.js +2 -0
- package/types/signer.d.ts +2 -2
- package/types/signer.js +2 -2
- package/utils/address.js +26 -37
- package/utils/ellipticCurve.d.ts +1 -6
- package/utils/ellipticCurve.js +73 -137
- package/utils/encode.js +49 -85
- package/utils/fetchPonyfill.d.ts +2 -0
- package/utils/fetchPonyfill.js +6 -0
- package/utils/hash.d.ts +4 -31
- package/utils/hash.js +76 -141
- package/utils/json.d.ts +13 -45
- package/utils/json.js +15 -22
- package/utils/number.d.ts +2 -9
- package/utils/number.js +47 -81
- package/utils/provider.d.ts +4 -0
- package/utils/provider.js +38 -0
- package/utils/responseParser/index.d.ts +11 -0
- package/utils/responseParser/index.js +9 -0
- package/utils/responseParser/rpc.d.ts +13 -0
- package/utils/responseParser/rpc.js +96 -0
- package/utils/responseParser/sequencer.d.ts +13 -0
- package/utils/responseParser/sequencer.js +124 -0
- package/utils/shortString.js +13 -21
- package/utils/stark.d.ts +0 -1
- package/utils/stark.js +59 -93
- package/utils/transaction.d.ts +3 -6
- package/utils/transaction.js +50 -81
- package/utils/typedData/index.d.ts +3 -15
- package/utils/typedData/index.js +109 -175
- package/utils/typedData/types.d.ts +9 -9
- package/utils/typedData/types.js +2 -2
- package/utils/typedData/utils.js +6 -6
- package/utils/uint256.d.ts +5 -5
- package/utils/uint256.js +16 -26
- package/www/docs/API/account.md +3 -4
- package/www/docs/API/contract.md +2 -2
- package/www/docs/API/contractFactory.md +2 -2
- package/www/docs/API/provider.md +185 -74
- package/www/guides/account.md +1 -8
- package/www/guides/erc20.md +3 -0
- package/__tests__/provider.test.ts +0 -168
- package/dist/types/api.d.ts +0 -261
- package/src/types/api.ts +0 -303
- package/types/api.d.ts +0 -287
- package/types/api.js +0 -2
package/src/account/interface.ts
CHANGED
|
@@ -2,13 +2,13 @@ import { ProviderInterface } from '../provider';
|
|
|
2
2
|
import { SignerInterface } from '../signer';
|
|
3
3
|
import {
|
|
4
4
|
Abi,
|
|
5
|
-
AddTransactionResponse,
|
|
6
5
|
Call,
|
|
7
|
-
|
|
6
|
+
EstimateFeeDetails,
|
|
7
|
+
EstimateFeeResponse,
|
|
8
8
|
InvocationsDetails,
|
|
9
|
+
InvokeFunctionResponse,
|
|
9
10
|
Signature,
|
|
10
11
|
} from '../types';
|
|
11
|
-
import { EstimateFee, EstimateFeeDetails } from '../types/account';
|
|
12
12
|
import { BigNumberish } from '../utils/number';
|
|
13
13
|
import { TypedData } from '../utils/typedData/types';
|
|
14
14
|
|
|
@@ -17,21 +17,6 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
17
17
|
|
|
18
18
|
public abstract signer: SignerInterface;
|
|
19
19
|
|
|
20
|
-
/**
|
|
21
|
-
* Deploys a given compiled contract (json) to starknet
|
|
22
|
-
*
|
|
23
|
-
* @param payload payload to be deployed containing:
|
|
24
|
-
* - compiled contract code
|
|
25
|
-
* - constructor calldata
|
|
26
|
-
* - address salt
|
|
27
|
-
* @param abi the abi of the contract
|
|
28
|
-
* @returns a confirmation of sending a transaction on the starknet contract
|
|
29
|
-
*/
|
|
30
|
-
public abstract override deployContract(
|
|
31
|
-
payload: DeployContractPayload,
|
|
32
|
-
abi?: Abi
|
|
33
|
-
): Promise<AddTransactionResponse>;
|
|
34
|
-
|
|
35
20
|
/**
|
|
36
21
|
* Estimate Fee for a method on starknet
|
|
37
22
|
*
|
|
@@ -46,7 +31,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
46
31
|
public abstract estimateFee(
|
|
47
32
|
calls: Call | Call[],
|
|
48
33
|
estimateFeeDetails?: EstimateFeeDetails
|
|
49
|
-
): Promise<
|
|
34
|
+
): Promise<EstimateFeeResponse>;
|
|
50
35
|
|
|
51
36
|
/**
|
|
52
37
|
* Invoke execute function in account contract
|
|
@@ -64,7 +49,7 @@ export abstract class AccountInterface extends ProviderInterface {
|
|
|
64
49
|
transactions: Call | Call[],
|
|
65
50
|
abis?: Abi[],
|
|
66
51
|
transactionsDetail?: InvocationsDetails
|
|
67
|
-
): Promise<
|
|
52
|
+
): Promise<InvokeFunctionResponse>;
|
|
68
53
|
|
|
69
54
|
/**
|
|
70
55
|
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
|
|
@@ -34,19 +34,16 @@ export class ContractFactory {
|
|
|
34
34
|
constructorCalldata?: RawCalldata,
|
|
35
35
|
addressSalt?: BigNumberish
|
|
36
36
|
): Promise<Contract> {
|
|
37
|
-
const {
|
|
37
|
+
const { contract_address, transaction_hash } = await this.providerOrAccount.deployContract({
|
|
38
38
|
contract: this.compiledContract,
|
|
39
39
|
constructorCalldata,
|
|
40
40
|
addressSalt,
|
|
41
41
|
});
|
|
42
|
-
assert(
|
|
43
|
-
code === 'TRANSACTION_RECEIVED' && Boolean(address),
|
|
44
|
-
'Deployment of the contract failed'
|
|
45
|
-
);
|
|
42
|
+
assert(Boolean(contract_address), 'Deployment of the contract failed');
|
|
46
43
|
|
|
47
44
|
const contractInstance = new Contract(
|
|
48
45
|
this.compiledContract.abi,
|
|
49
|
-
|
|
46
|
+
contract_address!,
|
|
50
47
|
this.providerOrAccount
|
|
51
48
|
);
|
|
52
49
|
contractInstance.deployTransactionHash = transaction_hash;
|
package/src/contract/default.ts
CHANGED
|
@@ -7,13 +7,13 @@ import { BlockIdentifier } from '../provider/utils';
|
|
|
7
7
|
import {
|
|
8
8
|
Abi,
|
|
9
9
|
AbiEntry,
|
|
10
|
-
AddTransactionResponse,
|
|
11
10
|
Args,
|
|
12
11
|
AsyncContractFunction,
|
|
13
12
|
Calldata,
|
|
14
13
|
ContractFunction,
|
|
15
14
|
FunctionAbi,
|
|
16
15
|
Invocation,
|
|
16
|
+
InvokeFunctionResponse,
|
|
17
17
|
Overrides,
|
|
18
18
|
ParsedStruct,
|
|
19
19
|
Result,
|
|
@@ -544,7 +544,7 @@ export class Contract implements ContractInterface {
|
|
|
544
544
|
method: string,
|
|
545
545
|
args: Array<any> = [],
|
|
546
546
|
options: Overrides = {}
|
|
547
|
-
): Promise<
|
|
547
|
+
): Promise<InvokeFunctionResponse> {
|
|
548
548
|
// ensure contract is connected
|
|
549
549
|
assert(this.address !== null, 'contract isnt connected to an address');
|
|
550
550
|
// validate method and args
|
|
@@ -578,6 +578,9 @@ export class Contract implements ContractInterface {
|
|
|
578
578
|
});
|
|
579
579
|
}
|
|
580
580
|
|
|
581
|
+
// eslint-disable-next-line no-console
|
|
582
|
+
console.warn(`Invoking ${method} without an account. This will not work on a public node.`);
|
|
583
|
+
|
|
581
584
|
return this.providerOrAccount.invokeFunction({
|
|
582
585
|
...invocation,
|
|
583
586
|
signature: options.signature || [],
|
|
@@ -609,13 +612,12 @@ export class Contract implements ContractInterface {
|
|
|
609
612
|
calldata,
|
|
610
613
|
entrypoint: method,
|
|
611
614
|
},
|
|
612
|
-
|
|
615
|
+
blockIdentifier
|
|
613
616
|
)
|
|
614
617
|
.then((x) => this.parseResponse(method, x.result));
|
|
615
618
|
}
|
|
616
619
|
|
|
617
620
|
public async estimate(method: string, args: Array<any> = []) {
|
|
618
|
-
// TODO; remove error as soon as estimate fees are supported
|
|
619
621
|
// ensure contract is connected
|
|
620
622
|
assert(this.address !== null, 'contract isnt connected to an address');
|
|
621
623
|
|
|
@@ -3,10 +3,10 @@ import { ProviderInterface } from '../provider';
|
|
|
3
3
|
import { BlockIdentifier } from '../provider/utils';
|
|
4
4
|
import {
|
|
5
5
|
Abi,
|
|
6
|
-
AddTransactionResponse,
|
|
7
6
|
AsyncContractFunction,
|
|
8
7
|
ContractFunction,
|
|
9
8
|
Invocation,
|
|
9
|
+
InvokeFunctionResponse,
|
|
10
10
|
Overrides,
|
|
11
11
|
Result,
|
|
12
12
|
} from '../types';
|
|
@@ -78,7 +78,7 @@ export abstract class ContractInterface {
|
|
|
78
78
|
method: string,
|
|
79
79
|
args?: Array<any>,
|
|
80
80
|
options?: Overrides
|
|
81
|
-
): Promise<
|
|
81
|
+
): Promise<InvokeFunctionResponse>;
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Calls a method on a contract
|
package/src/provider/default.ts
CHANGED
|
@@ -1,441 +1,109 @@
|
|
|
1
|
-
import
|
|
2
|
-
import fetch from 'cross-fetch';
|
|
3
|
-
|
|
4
|
-
import { ONE, StarknetChainId, ZERO } from '../constants';
|
|
1
|
+
import { StarknetChainId } from '../constants';
|
|
5
2
|
import {
|
|
6
|
-
|
|
7
|
-
AddTransactionResponse,
|
|
3
|
+
BlockTag,
|
|
8
4
|
Call,
|
|
9
5
|
CallContractResponse,
|
|
10
|
-
|
|
6
|
+
ContractClass,
|
|
11
7
|
DeclareContractPayload,
|
|
8
|
+
DeclareContractResponse,
|
|
12
9
|
DeployContractPayload,
|
|
13
|
-
|
|
10
|
+
DeployContractResponse,
|
|
11
|
+
EstimateFeeResponse,
|
|
14
12
|
GetBlockResponse,
|
|
15
|
-
|
|
16
|
-
GetContractAddressesResponse,
|
|
13
|
+
GetTransactionReceiptResponse,
|
|
17
14
|
GetTransactionResponse,
|
|
18
|
-
GetTransactionStatusResponse,
|
|
19
|
-
GetTransactionTraceResponse,
|
|
20
15
|
Invocation,
|
|
21
|
-
|
|
16
|
+
InvocationsDetails,
|
|
17
|
+
InvokeFunctionResponse,
|
|
22
18
|
} from '../types';
|
|
23
|
-
import {
|
|
24
|
-
import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
|
|
25
|
-
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
|
|
26
|
-
import { compressProgram, randomAddress } from '../utils/stark';
|
|
27
|
-
import { GatewayError, HttpError } from './errors';
|
|
19
|
+
import { BigNumberish } from '../utils/number';
|
|
28
20
|
import { ProviderInterface } from './interface';
|
|
29
|
-
import {
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
import { RpcProvider, RpcProviderOptions } from './rpc';
|
|
22
|
+
import { SequencerProvider, SequencerProviderOptions } from './sequencer';
|
|
23
|
+
import { BlockIdentifier } from './utils';
|
|
32
24
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return new Promise((res) => {
|
|
37
|
-
setTimeout(res, delay);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function isEmptyQueryObject(obj?: Record<any, any>): obj is undefined {
|
|
42
|
-
return (
|
|
43
|
-
obj === undefined ||
|
|
44
|
-
Object.keys(obj).length === 0 ||
|
|
45
|
-
(Object.keys(obj).length === 1 &&
|
|
46
|
-
Object.entries(obj).every(([k, v]) => k === 'blockIdentifier' && v === null))
|
|
47
|
-
);
|
|
25
|
+
export interface ProviderOptions {
|
|
26
|
+
sequencer?: SequencerProviderOptions;
|
|
27
|
+
rpc?: RpcProviderOptions;
|
|
48
28
|
}
|
|
49
29
|
|
|
50
30
|
export class Provider implements ProviderInterface {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
optionsOrProvider: ProviderOptions | ProviderInterface = { network: 'goerli-alpha' }
|
|
61
|
-
) {
|
|
62
|
-
if (optionsOrProvider instanceof ProviderInterface) {
|
|
63
|
-
this.baseUrl = optionsOrProvider.baseUrl;
|
|
64
|
-
this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
|
|
65
|
-
this.gatewayUrl = optionsOrProvider.gatewayUrl;
|
|
66
|
-
this.chainId =
|
|
67
|
-
optionsOrProvider.chainId ?? Provider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
|
|
31
|
+
private provider!: ProviderInterface;
|
|
32
|
+
|
|
33
|
+
constructor(providerOrOptions?: ProviderOptions | ProviderInterface) {
|
|
34
|
+
if (providerOrOptions && 'chainId' in providerOrOptions) {
|
|
35
|
+
this.provider = providerOrOptions;
|
|
36
|
+
} else if (providerOrOptions?.rpc) {
|
|
37
|
+
this.provider = new RpcProvider(providerOrOptions.rpc);
|
|
38
|
+
} else if (providerOrOptions?.sequencer) {
|
|
39
|
+
this.provider = new SequencerProvider(providerOrOptions.sequencer);
|
|
68
40
|
} else {
|
|
69
|
-
|
|
70
|
-
'baseUrl' in optionsOrProvider
|
|
71
|
-
? optionsOrProvider.baseUrl
|
|
72
|
-
: Provider.getNetworkFromName(optionsOrProvider.network);
|
|
73
|
-
this.baseUrl = baseUrl;
|
|
74
|
-
this.chainId = Provider.getChainIdFromBaseUrl(baseUrl);
|
|
75
|
-
this.feederGatewayUrl = urljoin(baseUrl, 'feeder_gateway');
|
|
76
|
-
this.gatewayUrl = urljoin(baseUrl, 'gateway');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
protected static getNetworkFromName(name: NetworkName) {
|
|
81
|
-
switch (name) {
|
|
82
|
-
case 'mainnet-alpha':
|
|
83
|
-
return 'https://alpha-mainnet.starknet.io';
|
|
84
|
-
case 'goerli-alpha':
|
|
85
|
-
default:
|
|
86
|
-
return 'https://alpha4.starknet.io';
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
protected static getChainIdFromBaseUrl(baseUrl: string): StarknetChainId {
|
|
91
|
-
try {
|
|
92
|
-
const url = new URL(baseUrl);
|
|
93
|
-
if (url.host.includes('mainnet.starknet.io')) {
|
|
94
|
-
return StarknetChainId.MAINNET;
|
|
95
|
-
}
|
|
96
|
-
} catch {
|
|
97
|
-
// eslint-disable-next-line no-console
|
|
98
|
-
console.error(`Could not parse baseUrl: ${baseUrl}`);
|
|
99
|
-
}
|
|
100
|
-
return StarknetChainId.TESTNET;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
private getFetchUrl(endpoint: keyof Endpoints) {
|
|
104
|
-
const gatewayUrlEndpoints = ['add_transaction'];
|
|
105
|
-
|
|
106
|
-
return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
private getFetchMethod(endpoint: keyof Endpoints) {
|
|
110
|
-
const postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];
|
|
111
|
-
|
|
112
|
-
return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
private getQueryString(query?: Record<string, any>): string {
|
|
116
|
-
if (isEmptyQueryObject(query)) {
|
|
117
|
-
return '';
|
|
118
|
-
}
|
|
119
|
-
const queryString = Object.entries(query)
|
|
120
|
-
.map(([key, value]) => {
|
|
121
|
-
if (key === 'blockIdentifier') {
|
|
122
|
-
return `${getFormattedBlockIdentifier(value)}`;
|
|
123
|
-
}
|
|
124
|
-
return `${key}=${value}`;
|
|
125
|
-
})
|
|
126
|
-
.join('&');
|
|
127
|
-
|
|
128
|
-
return `?${queryString}`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
private getHeaders(method: 'POST' | 'GET'): Record<string, string> | undefined {
|
|
132
|
-
if (method === 'POST') {
|
|
133
|
-
return {
|
|
134
|
-
'Content-Type': 'application/json',
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// typesafe fetch
|
|
141
|
-
protected async fetchEndpoint<T extends keyof Endpoints>(
|
|
142
|
-
endpoint: T,
|
|
143
|
-
// typescript type magiuc to create a nice fitting function interface
|
|
144
|
-
...[query, request]: Endpoints[T]['QUERY'] extends never
|
|
145
|
-
? Endpoints[T]['REQUEST'] extends never
|
|
146
|
-
? [] // when no query and no request is needed, we can omit the query and request parameters
|
|
147
|
-
: [undefined, Endpoints[T]['REQUEST']]
|
|
148
|
-
: Endpoints[T]['REQUEST'] extends never
|
|
149
|
-
? [Endpoints[T]['QUERY']] // when no request is needed, we can omit the request parameter
|
|
150
|
-
: [Endpoints[T]['QUERY'], Endpoints[T]['REQUEST']] // when both query and request are needed, we cant omit anything
|
|
151
|
-
): Promise<Endpoints[T]['RESPONSE']> {
|
|
152
|
-
const baseUrl = this.getFetchUrl(endpoint);
|
|
153
|
-
const method = this.getFetchMethod(endpoint);
|
|
154
|
-
const queryString = this.getQueryString(query);
|
|
155
|
-
const headers = this.getHeaders(method);
|
|
156
|
-
const url = urljoin(baseUrl, endpoint, queryString);
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
const res = await fetch(url, {
|
|
160
|
-
method,
|
|
161
|
-
body: stringify(request),
|
|
162
|
-
headers,
|
|
163
|
-
});
|
|
164
|
-
const textResponse = await res.text();
|
|
165
|
-
if (!res.ok) {
|
|
166
|
-
// This will allow user to handle contract errors
|
|
167
|
-
let responseBody: any;
|
|
168
|
-
try {
|
|
169
|
-
responseBody = parse(textResponse);
|
|
170
|
-
} catch {
|
|
171
|
-
// if error parsing fails, return an http error
|
|
172
|
-
throw new HttpError(res.statusText, res.status);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const errorCode = responseBody.code || ((responseBody as any)?.status_code as string); // starknet-devnet uses status_code instead of code; They need to fix that
|
|
176
|
-
throw new GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (endpoint === 'estimate_fee') {
|
|
180
|
-
return parseAlwaysAsBig(textResponse, (_, v) => {
|
|
181
|
-
if (v && typeof v === 'bigint') {
|
|
182
|
-
return toBN(v.toString());
|
|
183
|
-
}
|
|
184
|
-
return v;
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
return parse(textResponse) as Endpoints[T]['RESPONSE'];
|
|
188
|
-
} catch (err) {
|
|
189
|
-
// rethrow custom errors
|
|
190
|
-
if (err instanceof GatewayError || err instanceof HttpError) {
|
|
191
|
-
throw err;
|
|
192
|
-
}
|
|
193
|
-
if (err instanceof Error) {
|
|
194
|
-
throw Error(`Could not ${method} from endpoint \`${url}\`: ${err.message}`);
|
|
195
|
-
}
|
|
196
|
-
throw err;
|
|
41
|
+
this.provider = new SequencerProvider();
|
|
197
42
|
}
|
|
198
43
|
}
|
|
199
44
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
*
|
|
203
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
|
|
204
|
-
* @returns starknet smart contract addresses
|
|
205
|
-
*/
|
|
206
|
-
public async getContractAddresses(): Promise<GetContractAddressesResponse> {
|
|
207
|
-
return this.fetchEndpoint('get_contract_addresses');
|
|
45
|
+
public get chainId(): StarknetChainId {
|
|
46
|
+
return this.provider.chainId;
|
|
208
47
|
}
|
|
209
48
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
*
|
|
213
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
|
|
214
|
-
*
|
|
215
|
-
* @param invokeTransaction - transaction to be invoked
|
|
216
|
-
* @param blockHash
|
|
217
|
-
* @param blockNumber
|
|
218
|
-
* @returns the result of the function on the smart contract.
|
|
219
|
-
*/
|
|
220
|
-
public async callContract(
|
|
221
|
-
{ contractAddress, entrypoint, calldata = [] }: Call,
|
|
222
|
-
{ blockIdentifier = 'pending' }: { blockIdentifier?: BlockIdentifier } = {}
|
|
223
|
-
): Promise<CallContractResponse> {
|
|
224
|
-
return this.fetchEndpoint(
|
|
225
|
-
'call_contract',
|
|
226
|
-
{ blockIdentifier },
|
|
227
|
-
{
|
|
228
|
-
signature: [],
|
|
229
|
-
contract_address: contractAddress,
|
|
230
|
-
entry_point_selector: getSelectorFromName(entrypoint),
|
|
231
|
-
calldata,
|
|
232
|
-
}
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Gets the block information
|
|
238
|
-
*
|
|
239
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
|
|
240
|
-
*
|
|
241
|
-
* @param blockHash
|
|
242
|
-
* @param blockNumber
|
|
243
|
-
* @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
|
|
244
|
-
*/
|
|
245
|
-
public async getBlock(blockIdentifier: BlockIdentifier = null): Promise<GetBlockResponse> {
|
|
246
|
-
return this.fetchEndpoint('get_block', { blockIdentifier });
|
|
49
|
+
public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
|
|
50
|
+
return this.provider.getBlock(blockIdentifier);
|
|
247
51
|
}
|
|
248
52
|
|
|
249
|
-
|
|
250
|
-
* Gets the code of the deployed contract.
|
|
251
|
-
*
|
|
252
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
|
|
253
|
-
*
|
|
254
|
-
* @param contractAddress
|
|
255
|
-
* @param blockHash
|
|
256
|
-
* @param blockNumber
|
|
257
|
-
* @returns Bytecode and ABI of compiled contract
|
|
258
|
-
*/
|
|
259
|
-
public async getCode(
|
|
53
|
+
public async getClassAt(
|
|
260
54
|
contractAddress: string,
|
|
261
55
|
blockIdentifier: BlockIdentifier = 'pending'
|
|
262
|
-
): Promise<
|
|
263
|
-
return this.
|
|
56
|
+
): Promise<ContractClass> {
|
|
57
|
+
return this.provider.getClassAt(contractAddress, blockIdentifier);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public async getEstimateFee(
|
|
61
|
+
invocation: Invocation,
|
|
62
|
+
blockIdentifier: BlockIdentifier = 'pending',
|
|
63
|
+
invocationDetails: InvocationsDetails = {}
|
|
64
|
+
): Promise<EstimateFeeResponse> {
|
|
65
|
+
return this.provider.getEstimateFee(invocation, blockIdentifier, invocationDetails);
|
|
264
66
|
}
|
|
265
67
|
|
|
266
|
-
// TODO: add proper type
|
|
267
|
-
/**
|
|
268
|
-
* Gets the contract's storage variable at a specific key.
|
|
269
|
-
*
|
|
270
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L70-L85)
|
|
271
|
-
*
|
|
272
|
-
* @param contractAddress
|
|
273
|
-
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
|
|
274
|
-
* @param blockHash
|
|
275
|
-
* @param blockNumber
|
|
276
|
-
* @returns the value of the storage variable
|
|
277
|
-
*/
|
|
278
68
|
public async getStorageAt(
|
|
279
69
|
contractAddress: string,
|
|
280
70
|
key: BigNumberish,
|
|
281
|
-
|
|
282
|
-
): Promise<
|
|
283
|
-
return this.
|
|
71
|
+
blockTagOrHash: BlockTag | BigNumberish = 'pending'
|
|
72
|
+
): Promise<BigNumberish> {
|
|
73
|
+
return this.provider.getStorageAt(contractAddress, key, blockTagOrHash);
|
|
284
74
|
}
|
|
285
75
|
|
|
286
|
-
/**
|
|
287
|
-
* Gets the status of a transaction.
|
|
288
|
-
*
|
|
289
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
|
|
290
|
-
*
|
|
291
|
-
* @param txHash
|
|
292
|
-
* @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
|
|
293
|
-
*/
|
|
294
|
-
public async getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse> {
|
|
295
|
-
const txHashHex = toHex(toBN(txHash));
|
|
296
|
-
return this.fetchEndpoint('get_transaction_status', { transactionHash: txHashHex });
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Gets the transaction receipt from a tx hash.
|
|
301
|
-
*
|
|
302
|
-
* [Reference] (https://github.com/starkware-libs/cairo-lang/blob/167b28bcd940fd25ea3816204fa882a0b0a49603/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L183)
|
|
303
|
-
*
|
|
304
|
-
* @param txHash
|
|
305
|
-
* @returns the transaction receipt object
|
|
306
|
-
*/
|
|
307
|
-
public async getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse> {
|
|
308
|
-
const txHashHex = toHex(toBN(txHash));
|
|
309
|
-
return this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex });
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Gets the transaction information from a tx id.
|
|
314
|
-
*
|
|
315
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
|
|
316
|
-
*
|
|
317
|
-
* @param txHash
|
|
318
|
-
* @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
|
|
319
|
-
*/
|
|
320
76
|
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
|
|
321
|
-
|
|
322
|
-
return this.fetchEndpoint('get_transaction', { transactionHash: txHashHex });
|
|
77
|
+
return this.provider.getTransaction(txHash);
|
|
323
78
|
}
|
|
324
79
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* @param txHash
|
|
330
|
-
* @returns the transaction trace
|
|
331
|
-
*/
|
|
332
|
-
public async getTransactionTrace(txHash: BigNumberish): Promise<GetTransactionTraceResponse> {
|
|
333
|
-
const txHashHex = toHex(toBN(txHash));
|
|
334
|
-
return this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex });
|
|
80
|
+
public async getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse> {
|
|
81
|
+
return this.provider.getTransactionReceipt(txHash);
|
|
335
82
|
}
|
|
336
83
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
*/
|
|
343
|
-
public declareContract(payload: DeclareContractPayload): Promise<AddTransactionResponse> {
|
|
344
|
-
const parsedContract =
|
|
345
|
-
typeof payload.contract === 'string'
|
|
346
|
-
? (parse(payload.contract) as CompiledContract)
|
|
347
|
-
: payload.contract;
|
|
348
|
-
const contractDefinition = {
|
|
349
|
-
...parsedContract,
|
|
350
|
-
program: compressProgram(parsedContract.program),
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
return this.fetchEndpoint('add_transaction', undefined, {
|
|
354
|
-
type: 'DECLARE',
|
|
355
|
-
contract_class: contractDefinition,
|
|
356
|
-
nonce: toHex(ZERO),
|
|
357
|
-
signature: [],
|
|
358
|
-
sender_address: toHex(ONE),
|
|
359
|
-
});
|
|
84
|
+
public async callContract(
|
|
85
|
+
request: Call,
|
|
86
|
+
blockIdentifier: BlockIdentifier = 'pending'
|
|
87
|
+
): Promise<CallContractResponse> {
|
|
88
|
+
return this.provider.callContract(request, blockIdentifier);
|
|
360
89
|
}
|
|
361
90
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
* @returns a confirmation of sending a transaction on the starknet contract
|
|
368
|
-
*/
|
|
369
|
-
public deployContract(
|
|
370
|
-
payload: DeployContractPayload,
|
|
371
|
-
_abi?: Abi
|
|
372
|
-
): Promise<AddTransactionResponse> {
|
|
373
|
-
const parsedContract =
|
|
374
|
-
typeof payload.contract === 'string'
|
|
375
|
-
? (parse(payload.contract) as CompiledContract)
|
|
376
|
-
: payload.contract;
|
|
377
|
-
const contractDefinition = {
|
|
378
|
-
...parsedContract,
|
|
379
|
-
program: compressProgram(parsedContract.program),
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
return this.fetchEndpoint('add_transaction', undefined, {
|
|
383
|
-
type: 'DEPLOY',
|
|
384
|
-
contract_address_salt: payload.addressSalt ?? randomAddress(),
|
|
385
|
-
constructor_calldata: bigNumberishArrayToDecimalStringArray(
|
|
386
|
-
payload.constructorCalldata ?? []
|
|
387
|
-
),
|
|
388
|
-
contract_definition: contractDefinition,
|
|
389
|
-
});
|
|
91
|
+
public async invokeFunction(
|
|
92
|
+
functionInvocation: Invocation,
|
|
93
|
+
details: InvocationsDetails
|
|
94
|
+
): Promise<InvokeFunctionResponse> {
|
|
95
|
+
return this.provider.invokeFunction(functionInvocation, details);
|
|
390
96
|
}
|
|
391
97
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
* @deprecated This method wont be supported as soon as fees are mandatory
|
|
395
|
-
*
|
|
396
|
-
* @param invocation
|
|
397
|
-
* @param _abi - (optional) signature to send along
|
|
398
|
-
* @returns response from addTransaction
|
|
399
|
-
*/
|
|
400
|
-
public invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse> {
|
|
401
|
-
return this.fetchEndpoint('add_transaction', undefined, {
|
|
402
|
-
type: 'INVOKE_FUNCTION',
|
|
403
|
-
contract_address: invocation.contractAddress,
|
|
404
|
-
entry_point_selector: getSelectorFromName(invocation.entrypoint),
|
|
405
|
-
calldata: bigNumberishArrayToDecimalStringArray(invocation.calldata ?? []),
|
|
406
|
-
signature: bigNumberishArrayToDecimalStringArray(invocation.signature ?? []),
|
|
407
|
-
});
|
|
98
|
+
public async deployContract(payload: DeployContractPayload): Promise<DeployContractResponse> {
|
|
99
|
+
return this.provider.deployContract(payload);
|
|
408
100
|
}
|
|
409
101
|
|
|
410
|
-
public async
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
while (!onchain) {
|
|
414
|
-
// eslint-disable-next-line no-await-in-loop
|
|
415
|
-
await wait(retryInterval);
|
|
416
|
-
// eslint-disable-next-line no-await-in-loop
|
|
417
|
-
const res = await this.getTransactionStatus(txHash);
|
|
418
|
-
|
|
419
|
-
const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
|
|
420
|
-
const errorStates = ['REJECTED', 'NOT_RECEIVED'];
|
|
421
|
-
|
|
422
|
-
if (successStates.includes(res.tx_status)) {
|
|
423
|
-
onchain = true;
|
|
424
|
-
} else if (errorStates.includes(res.tx_status)) {
|
|
425
|
-
const message = res.tx_failure_reason
|
|
426
|
-
? `${res.tx_status}: ${res.tx_failure_reason.code}\n${res.tx_failure_reason.error_message}`
|
|
427
|
-
: res.tx_status;
|
|
428
|
-
const error = new Error(message) as Error & { response: GetTransactionStatusResponse };
|
|
429
|
-
error.response = res;
|
|
430
|
-
throw error;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
102
|
+
public async declareContract(payload: DeclareContractPayload): Promise<DeclareContractResponse> {
|
|
103
|
+
return this.provider.declareContract(payload);
|
|
433
104
|
}
|
|
434
105
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
*/
|
|
438
|
-
public async waitForTx(txHash: BigNumberish, retryInterval: number = 8000) {
|
|
439
|
-
return this.waitForTransaction(txHash, retryInterval);
|
|
106
|
+
public async waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void> {
|
|
107
|
+
return this.provider.waitForTransaction(txHash, retryInterval);
|
|
440
108
|
}
|
|
441
109
|
}
|
package/src/provider/index.ts
CHANGED