starknet 3.9.0 → 3.10.2
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 +27 -0
- package/__mocks__/ArgentAccount.json +32022 -38726
- package/__tests__/accountContract.test.ts +42 -32
- package/__tests__/contract.test.ts +20 -6
- package/__tests__/provider.test.ts +4 -4
- package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
- package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
- package/__tests__/utils/ellipticalCurve.test.ts +26 -8
- package/__tests__/utils/transactionHash.test.ts +17 -0
- package/account/default.d.ts +10 -6
- package/account/default.js +13 -24
- package/account/interface.d.ts +2 -0
- package/constants.d.ts +9 -0
- package/constants.js +13 -0
- package/contract/contractFactory.d.ts +5 -5
- package/contract/default.js +1 -1
- package/dist/account/default.d.ts +6 -6
- package/dist/account/default.js +12 -8
- package/dist/account/interface.d.ts +2 -0
- package/dist/constants.d.ts +9 -0
- package/dist/constants.js +12 -1
- package/dist/contract/contractFactory.d.ts +5 -5
- package/dist/contract/default.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/provider/default.d.ts +4 -1
- package/dist/provider/default.js +20 -1
- package/dist/provider/interface.d.ts +2 -0
- package/dist/signer/default.js +4 -2
- package/dist/signer/ledger.js +4 -2
- package/dist/types/signer.d.ts +2 -0
- package/dist/utils/hash.d.ts +4 -3
- package/dist/utils/hash.js +24 -24
- package/dist/utils/transaction.d.ts +2 -0
- package/dist/utils/transaction.js +5 -1
- package/dist/utils/typedData/index.d.ts +2 -2
- package/dist/utils/typedData/types.d.ts +3 -3
- package/dist/utils/typedData/utils.d.ts +1 -1
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/provider/default.d.ts +4 -1
- package/provider/default.js +21 -1
- package/provider/interface.d.ts +2 -0
- package/signer/default.js +12 -6
- package/signer/ledger.js +12 -5
- package/src/account/default.ts +26 -15
- package/src/account/interface.ts +3 -0
- package/src/constants.ts +10 -0
- package/src/contract/contractFactory.ts +5 -5
- package/src/contract/default.ts +1 -1
- package/src/index.ts +1 -0
- package/src/provider/default.ts +23 -2
- package/src/provider/interface.ts +3 -0
- package/src/signer/default.ts +10 -6
- package/src/signer/ledger.ts +10 -5
- package/src/types/signer.ts +2 -0
- package/src/utils/hash.ts +68 -26
- package/src/utils/transaction.ts +7 -0
- package/types/signer.d.ts +2 -0
- package/utils/hash.d.ts +24 -8
- package/utils/hash.js +55 -28
- package/utils/transaction.d.ts +5 -0
- package/utils/transaction.js +12 -1
- package/utils/typedData/index.d.ts +2 -2
- package/utils/typedData/types.d.ts +3 -3
- package/utils/typedData/utils.d.ts +1 -1
- package/www/code-examples/account.js +8 -5
- package/www/code-examples/amm.js +13 -18
- package/www/code-examples/erc20.js +6 -3
- package/www/docs/API/account.md +94 -0
- package/www/docs/API/changelog.md +15 -0
- package/www/docs/API/contract.md +73 -2
- package/www/docs/API/contractFacotry.md +42 -0
- package/www/docs/API/index.md +0 -1
- package/www/docs/API/provider.md +204 -1
- package/www/docs/API/signer.md +35 -0
- package/www/docusaurus.config.js +2 -3
- package/www/guides/account.md +1 -1
- package/www/guides/erc20.md +7 -0
- package/www/guides/intro.md +1 -0
- package/www/sidebars.js +1 -1
- package/__tests__/constancts.ts +0 -2
package/src/provider/default.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios, { AxiosRequestHeaders } from 'axios';
|
|
2
2
|
import urljoin from 'url-join';
|
|
3
3
|
|
|
4
|
+
import { StarknetChainId } from '../constants';
|
|
4
5
|
import {
|
|
5
6
|
Abi,
|
|
6
7
|
AddTransactionResponse,
|
|
@@ -49,17 +50,24 @@ export class Provider implements ProviderInterface {
|
|
|
49
50
|
|
|
50
51
|
public gatewayUrl: string;
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
public chainId: StarknetChainId;
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
optionsOrProvider: ProviderOptions | ProviderInterface = { network: 'goerli-alpha' }
|
|
57
|
+
) {
|
|
58
|
+
if (optionsOrProvider instanceof ProviderInterface) {
|
|
54
59
|
this.baseUrl = optionsOrProvider.baseUrl;
|
|
55
60
|
this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
|
|
56
61
|
this.gatewayUrl = optionsOrProvider.gatewayUrl;
|
|
62
|
+
this.chainId =
|
|
63
|
+
optionsOrProvider.chainId ?? Provider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
|
|
57
64
|
} else {
|
|
58
65
|
const baseUrl =
|
|
59
66
|
'baseUrl' in optionsOrProvider
|
|
60
67
|
? optionsOrProvider.baseUrl
|
|
61
68
|
: Provider.getNetworkFromName(optionsOrProvider.network);
|
|
62
69
|
this.baseUrl = baseUrl;
|
|
70
|
+
this.chainId = Provider.getChainIdFromBaseUrl(baseUrl);
|
|
63
71
|
this.feederGatewayUrl = urljoin(baseUrl, 'feeder_gateway');
|
|
64
72
|
this.gatewayUrl = urljoin(baseUrl, 'gateway');
|
|
65
73
|
}
|
|
@@ -75,6 +83,19 @@ export class Provider implements ProviderInterface {
|
|
|
75
83
|
}
|
|
76
84
|
}
|
|
77
85
|
|
|
86
|
+
protected static getChainIdFromBaseUrl(baseUrl: string): StarknetChainId {
|
|
87
|
+
try {
|
|
88
|
+
const url = new URL(baseUrl);
|
|
89
|
+
if (url.host.includes('mainnet.starknet.io')) {
|
|
90
|
+
return StarknetChainId.MAINNET;
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
// eslint-disable-next-line no-console
|
|
94
|
+
console.error(`Could not parse baseUrl: ${baseUrl}`);
|
|
95
|
+
}
|
|
96
|
+
return StarknetChainId.TESTNET;
|
|
97
|
+
}
|
|
98
|
+
|
|
78
99
|
private getFetchUrl(endpoint: keyof Endpoints) {
|
|
79
100
|
const gatewayUrlEndpoints = ['add_transaction'];
|
|
80
101
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StarknetChainId } from '../constants';
|
|
1
2
|
import type {
|
|
2
3
|
AddTransactionResponse,
|
|
3
4
|
Call,
|
|
@@ -21,6 +22,8 @@ export abstract class ProviderInterface {
|
|
|
21
22
|
|
|
22
23
|
public abstract gatewayUrl: string;
|
|
23
24
|
|
|
25
|
+
public abstract chainId: StarknetChainId;
|
|
26
|
+
|
|
24
27
|
/**
|
|
25
28
|
* Gets the smart contract address on the goerli testnet.
|
|
26
29
|
*
|
package/src/signer/default.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Abi, Invocation, InvocationsSignerDetails, KeyPair, Signature } from '../types';
|
|
2
2
|
import { getStarkKey, sign } from '../utils/ellipticCurve';
|
|
3
|
-
import {
|
|
3
|
+
import { calculcateTransactionHash, getSelectorFromName } from '../utils/hash';
|
|
4
|
+
import { fromCallsToExecuteCalldataWithNonce } from '../utils/transaction';
|
|
4
5
|
import { TypedData, getMessageHash } from '../utils/typedData';
|
|
5
6
|
import { SignerInterface } from './interface';
|
|
6
7
|
|
|
@@ -25,12 +26,15 @@ export class Signer implements SignerInterface {
|
|
|
25
26
|
}
|
|
26
27
|
// now use abi to display decoded data somewhere, but as this signer is headless, we can't do that
|
|
27
28
|
|
|
28
|
-
const
|
|
29
|
+
const calldata = fromCallsToExecuteCalldataWithNonce(transactions, transactionsDetail.nonce);
|
|
30
|
+
|
|
31
|
+
const msgHash = calculcateTransactionHash(
|
|
29
32
|
transactionsDetail.walletAddress,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
transactionsDetail.
|
|
33
|
+
transactionsDetail.version,
|
|
34
|
+
getSelectorFromName('__execute__'),
|
|
35
|
+
calldata,
|
|
36
|
+
transactionsDetail.maxFee,
|
|
37
|
+
transactionsDetail.chainId
|
|
34
38
|
);
|
|
35
39
|
|
|
36
40
|
return sign(this.keyPair, msgHash);
|
package/src/signer/ledger.ts
CHANGED
|
@@ -4,7 +4,8 @@ import TransportWebHID from '@ledgerhq/hw-transport-webhid';
|
|
|
4
4
|
|
|
5
5
|
import { Invocation, InvocationsSignerDetails, Signature } from '../types';
|
|
6
6
|
import { addHexPrefix } from '../utils/encode';
|
|
7
|
-
import {
|
|
7
|
+
import { calculcateTransactionHash, getSelectorFromName } from '../utils/hash';
|
|
8
|
+
import { fromCallsToExecuteCalldataWithNonce } from '../utils/transaction';
|
|
8
9
|
import { TypedData, getMessageHash } from '../utils/typedData';
|
|
9
10
|
import { SignerInterface } from './interface';
|
|
10
11
|
|
|
@@ -46,11 +47,15 @@ export class LedgerBlindSigner implements SignerInterface {
|
|
|
46
47
|
transactions: Invocation[],
|
|
47
48
|
transactionsDetail: InvocationsSignerDetails
|
|
48
49
|
): Promise<Signature> {
|
|
49
|
-
const
|
|
50
|
+
const calldata = fromCallsToExecuteCalldataWithNonce(transactions, transactionsDetail.nonce);
|
|
51
|
+
|
|
52
|
+
const msgHash = calculcateTransactionHash(
|
|
50
53
|
transactionsDetail.walletAddress,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
transactionsDetail.version,
|
|
55
|
+
getSelectorFromName('__execute__'),
|
|
56
|
+
calldata,
|
|
57
|
+
transactionsDetail.maxFee,
|
|
58
|
+
transactionsDetail.chainId
|
|
54
59
|
);
|
|
55
60
|
|
|
56
61
|
return this.sign(msgHash);
|
package/src/types/signer.ts
CHANGED
package/src/utils/hash.ts
CHANGED
|
@@ -2,14 +2,19 @@ import BN from 'bn.js';
|
|
|
2
2
|
import { keccak256 } from 'ethereum-cryptography/keccak';
|
|
3
3
|
import assert from 'minimalistic-assert';
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
CONSTANT_POINTS,
|
|
7
|
+
FIELD_PRIME,
|
|
8
|
+
MASK_250,
|
|
9
|
+
ONE,
|
|
10
|
+
StarknetChainId,
|
|
11
|
+
TransactionHashPrefix,
|
|
12
|
+
ZERO,
|
|
13
|
+
} from '../constants';
|
|
7
14
|
import { ec } from './ellipticCurve';
|
|
8
15
|
import { addHexPrefix, buf2hex, utf8ToArray } from './encode';
|
|
9
|
-
import { BigNumberish,
|
|
10
|
-
import { encodeShortString } from './shortString';
|
|
16
|
+
import { BigNumberish, toBN, toHex } from './number';
|
|
11
17
|
|
|
12
|
-
export const transactionPrefix = encodeShortString('StarkNet Transaction');
|
|
13
18
|
export const transactionVersion = 0;
|
|
14
19
|
export const feeTransactionVersion = toBN(2).pow(toBN(128)).add(toBN(transactionVersion));
|
|
15
20
|
|
|
@@ -65,28 +70,65 @@ export function computeHashOnElements(data: BigNumberish[]) {
|
|
|
65
70
|
return [...data, data.length].reduce((x, y) => pedersen([x, y]), 0).toString();
|
|
66
71
|
}
|
|
67
72
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
transactions: Call[],
|
|
71
|
-
nonce: string,
|
|
72
|
-
maxFee: string,
|
|
73
|
-
version: string | number = transactionVersion
|
|
74
|
-
) {
|
|
75
|
-
const hashArray = transactions
|
|
76
|
-
.map(({ contractAddress, entrypoint, calldata }) => [
|
|
77
|
-
contractAddress,
|
|
78
|
-
getSelectorFromName(entrypoint),
|
|
79
|
-
computeHashOnElements(calldata || []),
|
|
80
|
-
])
|
|
81
|
-
.map(bigNumberishArrayToDecimalStringArray)
|
|
82
|
-
.map(computeHashOnElements);
|
|
73
|
+
// following implementation is based on this python implementation:
|
|
74
|
+
// https://github.com/starkware-libs/cairo-lang/blob/b614d1867c64f3fb2cf4a4879348cfcf87c3a5a7/src/starkware/starknet/core/os/transaction_hash/transaction_hash.py
|
|
83
75
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
76
|
+
export function calculateTransactionHashCommon(
|
|
77
|
+
txHashPrefix: TransactionHashPrefix,
|
|
78
|
+
version: BigNumberish,
|
|
79
|
+
contractAddress: BigNumberish,
|
|
80
|
+
entryPointSelector: BigNumberish,
|
|
81
|
+
calldata: BigNumberish[],
|
|
82
|
+
maxFee: BigNumberish,
|
|
83
|
+
chainId: StarknetChainId,
|
|
84
|
+
additionalData: BigNumberish[] = []
|
|
85
|
+
): string {
|
|
86
|
+
const calldataHash = computeHashOnElements(calldata);
|
|
87
|
+
const dataToHash = [
|
|
88
|
+
txHashPrefix,
|
|
89
|
+
version,
|
|
90
|
+
contractAddress,
|
|
91
|
+
entryPointSelector,
|
|
92
|
+
calldataHash,
|
|
89
93
|
maxFee,
|
|
94
|
+
chainId,
|
|
95
|
+
...additionalData,
|
|
96
|
+
];
|
|
97
|
+
return computeHashOnElements(dataToHash);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function calculateDeployTransactionHash(
|
|
101
|
+
contractAddress: BigNumberish,
|
|
102
|
+
constructorCalldata: BigNumberish[],
|
|
103
|
+
version: BigNumberish,
|
|
104
|
+
chainId: StarknetChainId
|
|
105
|
+
): string {
|
|
106
|
+
return calculateTransactionHashCommon(
|
|
107
|
+
TransactionHashPrefix.DEPLOY,
|
|
90
108
|
version,
|
|
91
|
-
|
|
109
|
+
contractAddress,
|
|
110
|
+
getSelectorFromName('constructor'),
|
|
111
|
+
constructorCalldata,
|
|
112
|
+
ZERO,
|
|
113
|
+
chainId
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function calculcateTransactionHash(
|
|
118
|
+
contractAddress: BigNumberish,
|
|
119
|
+
version: BigNumberish,
|
|
120
|
+
entryPointSelector: BigNumberish,
|
|
121
|
+
calldata: BigNumberish[],
|
|
122
|
+
maxFee: BigNumberish,
|
|
123
|
+
chainId: StarknetChainId
|
|
124
|
+
): string {
|
|
125
|
+
return calculateTransactionHashCommon(
|
|
126
|
+
TransactionHashPrefix.INVOKE,
|
|
127
|
+
version,
|
|
128
|
+
contractAddress,
|
|
129
|
+
entryPointSelector,
|
|
130
|
+
calldata,
|
|
131
|
+
maxFee,
|
|
132
|
+
chainId
|
|
133
|
+
);
|
|
92
134
|
}
|
package/src/utils/transaction.ts
CHANGED
|
@@ -47,3 +47,10 @@ export const fromCallsToExecuteCalldata = (calls: Call[]): string[] => {
|
|
|
47
47
|
...calldata,
|
|
48
48
|
];
|
|
49
49
|
};
|
|
50
|
+
|
|
51
|
+
export const fromCallsToExecuteCalldataWithNonce = (
|
|
52
|
+
calls: Call[],
|
|
53
|
+
nonce: BigNumberish
|
|
54
|
+
): string[] => {
|
|
55
|
+
return [...fromCallsToExecuteCalldata(calls), toBN(nonce).toString()];
|
|
56
|
+
};
|
package/types/signer.d.ts
CHANGED
package/utils/hash.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { StarknetChainId, TransactionHashPrefix } from '../constants';
|
|
4
4
|
import { BigNumberish } from './number';
|
|
5
|
-
export declare const transactionPrefix: string;
|
|
6
5
|
export declare const transactionVersion = 0;
|
|
7
6
|
export declare const feeTransactionVersion: BN;
|
|
8
7
|
/**
|
|
@@ -23,10 +22,27 @@ export declare function starknetKeccak(value: string): BN;
|
|
|
23
22
|
export declare function getSelectorFromName(funcName: string): string;
|
|
24
23
|
export declare function pedersen(input: [BigNumberish, BigNumberish]): string;
|
|
25
24
|
export declare function computeHashOnElements(data: BigNumberish[]): string;
|
|
26
|
-
export declare function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
export declare function calculateTransactionHashCommon(
|
|
26
|
+
txHashPrefix: TransactionHashPrefix,
|
|
27
|
+
version: BigNumberish,
|
|
28
|
+
contractAddress: BigNumberish,
|
|
29
|
+
entryPointSelector: BigNumberish,
|
|
30
|
+
calldata: BigNumberish[],
|
|
31
|
+
maxFee: BigNumberish,
|
|
32
|
+
chainId: StarknetChainId,
|
|
33
|
+
additionalData?: BigNumberish[]
|
|
34
|
+
): string;
|
|
35
|
+
export declare function calculateDeployTransactionHash(
|
|
36
|
+
contractAddress: BigNumberish,
|
|
37
|
+
constructorCalldata: BigNumberish[],
|
|
38
|
+
version: BigNumberish,
|
|
39
|
+
chainId: StarknetChainId
|
|
40
|
+
): string;
|
|
41
|
+
export declare function calculcateTransactionHash(
|
|
42
|
+
contractAddress: BigNumberish,
|
|
43
|
+
version: BigNumberish,
|
|
44
|
+
entryPointSelector: BigNumberish,
|
|
45
|
+
calldata: BigNumberish[],
|
|
46
|
+
maxFee: BigNumberish,
|
|
47
|
+
chainId: StarknetChainId
|
|
32
48
|
): string;
|
package/utils/hash.js
CHANGED
|
@@ -39,14 +39,15 @@ var __importDefault =
|
|
|
39
39
|
return mod && mod.__esModule ? mod : { default: mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
42
|
-
exports.
|
|
42
|
+
exports.calculcateTransactionHash =
|
|
43
|
+
exports.calculateDeployTransactionHash =
|
|
44
|
+
exports.calculateTransactionHashCommon =
|
|
43
45
|
exports.computeHashOnElements =
|
|
44
46
|
exports.pedersen =
|
|
45
47
|
exports.getSelectorFromName =
|
|
46
48
|
exports.starknetKeccak =
|
|
47
49
|
exports.feeTransactionVersion =
|
|
48
50
|
exports.transactionVersion =
|
|
49
|
-
exports.transactionPrefix =
|
|
50
51
|
void 0;
|
|
51
52
|
var keccak_1 = require('ethereum-cryptography/keccak');
|
|
52
53
|
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
|
|
@@ -54,8 +55,6 @@ var constants_1 = require('../constants');
|
|
|
54
55
|
var ellipticCurve_1 = require('./ellipticCurve');
|
|
55
56
|
var encode_1 = require('./encode');
|
|
56
57
|
var number_1 = require('./number');
|
|
57
|
-
var shortString_1 = require('./shortString');
|
|
58
|
-
exports.transactionPrefix = (0, shortString_1.encodeShortString)('StarkNet Transaction');
|
|
59
58
|
exports.transactionVersion = 0;
|
|
60
59
|
exports.feeTransactionVersion = (0, number_1.toBN)(2)
|
|
61
60
|
.pow((0, number_1.toBN)(128))
|
|
@@ -120,30 +119,58 @@ function computeHashOnElements(data) {
|
|
|
120
119
|
.toString();
|
|
121
120
|
}
|
|
122
121
|
exports.computeHashOnElements = computeHashOnElements;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
122
|
+
// following implementation is based on this python implementation:
|
|
123
|
+
// https://github.com/starkware-libs/cairo-lang/blob/b614d1867c64f3fb2cf4a4879348cfcf87c3a5a7/src/starkware/starknet/core/os/transaction_hash/transaction_hash.py
|
|
124
|
+
function calculateTransactionHashCommon(
|
|
125
|
+
txHashPrefix,
|
|
126
|
+
version,
|
|
127
|
+
contractAddress,
|
|
128
|
+
entryPointSelector,
|
|
129
|
+
calldata,
|
|
130
|
+
maxFee,
|
|
131
|
+
chainId,
|
|
132
|
+
additionalData
|
|
133
|
+
) {
|
|
134
|
+
if (additionalData === void 0) {
|
|
135
|
+
additionalData = [];
|
|
126
136
|
}
|
|
127
|
-
var
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
var calldataHash = computeHashOnElements(calldata);
|
|
138
|
+
var dataToHash = __spreadArray(
|
|
139
|
+
[txHashPrefix, version, contractAddress, entryPointSelector, calldataHash, maxFee, chainId],
|
|
140
|
+
__read(additionalData),
|
|
141
|
+
false
|
|
142
|
+
);
|
|
143
|
+
return computeHashOnElements(dataToHash);
|
|
144
|
+
}
|
|
145
|
+
exports.calculateTransactionHashCommon = calculateTransactionHashCommon;
|
|
146
|
+
function calculateDeployTransactionHash(contractAddress, constructorCalldata, version, chainId) {
|
|
147
|
+
return calculateTransactionHashCommon(
|
|
148
|
+
constants_1.TransactionHashPrefix.DEPLOY,
|
|
149
|
+
version,
|
|
150
|
+
contractAddress,
|
|
151
|
+
getSelectorFromName('constructor'),
|
|
152
|
+
constructorCalldata,
|
|
153
|
+
constants_1.ZERO,
|
|
154
|
+
chainId
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
exports.calculateDeployTransactionHash = calculateDeployTransactionHash;
|
|
158
|
+
function calculcateTransactionHash(
|
|
159
|
+
contractAddress,
|
|
160
|
+
version,
|
|
161
|
+
entryPointSelector,
|
|
162
|
+
calldata,
|
|
163
|
+
maxFee,
|
|
164
|
+
chainId
|
|
165
|
+
) {
|
|
166
|
+
return calculateTransactionHashCommon(
|
|
167
|
+
constants_1.TransactionHashPrefix.INVOKE,
|
|
146
168
|
version,
|
|
147
|
-
|
|
169
|
+
contractAddress,
|
|
170
|
+
entryPointSelector,
|
|
171
|
+
calldata,
|
|
172
|
+
maxFee,
|
|
173
|
+
chainId
|
|
174
|
+
);
|
|
148
175
|
}
|
|
149
|
-
exports.
|
|
176
|
+
exports.calculcateTransactionHash = calculcateTransactionHash;
|
package/utils/transaction.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Call, ParsedStruct } from '../types';
|
|
2
|
+
import { BigNumberish } from './number';
|
|
2
3
|
/**
|
|
3
4
|
* Transforms a list of Calls, each with their own calldata, into
|
|
4
5
|
* two arrays: one with the entrypoints, and one with the concatenated calldata.
|
|
@@ -16,3 +17,7 @@ export declare const transformCallsToMulticallArrays: (calls: Call[]) => {
|
|
|
16
17
|
* @returns
|
|
17
18
|
*/
|
|
18
19
|
export declare const fromCallsToExecuteCalldata: (calls: Call[]) => string[];
|
|
20
|
+
export declare const fromCallsToExecuteCalldataWithNonce: (
|
|
21
|
+
calls: Call[],
|
|
22
|
+
nonce: BigNumberish
|
|
23
|
+
) => string[];
|
package/utils/transaction.js
CHANGED
|
@@ -34,7 +34,10 @@ var __spreadArray =
|
|
|
34
34
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
35
35
|
};
|
|
36
36
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
37
|
-
exports.
|
|
37
|
+
exports.fromCallsToExecuteCalldataWithNonce =
|
|
38
|
+
exports.fromCallsToExecuteCalldata =
|
|
39
|
+
exports.transformCallsToMulticallArrays =
|
|
40
|
+
void 0;
|
|
38
41
|
var hash_1 = require('./hash');
|
|
39
42
|
var number_1 = require('./number');
|
|
40
43
|
/**
|
|
@@ -97,3 +100,11 @@ var fromCallsToExecuteCalldata = function (calls) {
|
|
|
97
100
|
);
|
|
98
101
|
};
|
|
99
102
|
exports.fromCallsToExecuteCalldata = fromCallsToExecuteCalldata;
|
|
103
|
+
var fromCallsToExecuteCalldataWithNonce = function (calls, nonce) {
|
|
104
|
+
return __spreadArray(
|
|
105
|
+
__spreadArray([], __read((0, exports.fromCallsToExecuteCalldata)(calls)), false),
|
|
106
|
+
[(0, number_1.toBN)(nonce).toString()],
|
|
107
|
+
false
|
|
108
|
+
);
|
|
109
|
+
};
|
|
110
|
+
exports.fromCallsToExecuteCalldataWithNonce = fromCallsToExecuteCalldataWithNonce;
|
|
@@ -56,8 +56,8 @@ export declare const encodeData: <
|
|
|
56
56
|
primaryType: string;
|
|
57
57
|
domain: {
|
|
58
58
|
version?: string | undefined;
|
|
59
|
-
name?: string | undefined;
|
|
60
59
|
chainId?: string | number | undefined;
|
|
60
|
+
name?: string | undefined;
|
|
61
61
|
};
|
|
62
62
|
message: Record<string, unknown>;
|
|
63
63
|
}
|
|
@@ -92,8 +92,8 @@ export declare const getStructHash: <
|
|
|
92
92
|
primaryType: string;
|
|
93
93
|
domain: {
|
|
94
94
|
version?: string | undefined;
|
|
95
|
-
name?: string | undefined;
|
|
96
95
|
chainId?: string | number | undefined;
|
|
96
|
+
name?: string | undefined;
|
|
97
97
|
};
|
|
98
98
|
message: Record<string, unknown>;
|
|
99
99
|
}
|
|
@@ -30,8 +30,8 @@ export declare type StarkNetType = Infer<typeof STARKNET_TYPE>;
|
|
|
30
30
|
export declare const STARKNET_DOMAIN_TYPE: import('superstruct').Struct<
|
|
31
31
|
{
|
|
32
32
|
version?: string | undefined;
|
|
33
|
-
name?: string | undefined;
|
|
34
33
|
chainId?: string | number | undefined;
|
|
34
|
+
name?: string | undefined;
|
|
35
35
|
},
|
|
36
36
|
{
|
|
37
37
|
name: import('superstruct').Struct<string | undefined, null>;
|
|
@@ -60,8 +60,8 @@ export declare const STARKNET_TYPED_DATA_TYPE: import('superstruct').Struct<
|
|
|
60
60
|
primaryType: string;
|
|
61
61
|
domain: {
|
|
62
62
|
version?: string | undefined;
|
|
63
|
-
name?: string | undefined;
|
|
64
63
|
chainId?: string | number | undefined;
|
|
64
|
+
name?: string | undefined;
|
|
65
65
|
};
|
|
66
66
|
message: Record<string, unknown>;
|
|
67
67
|
},
|
|
@@ -85,8 +85,8 @@ export declare const STARKNET_TYPED_DATA_TYPE: import('superstruct').Struct<
|
|
|
85
85
|
domain: import('superstruct').Struct<
|
|
86
86
|
{
|
|
87
87
|
version?: string | undefined;
|
|
88
|
-
name?: string | undefined;
|
|
89
88
|
chainId?: string | number | undefined;
|
|
89
|
+
name?: string | undefined;
|
|
90
90
|
},
|
|
91
91
|
{
|
|
92
92
|
name: import('superstruct').Struct<string | undefined, null>;
|
|
@@ -20,8 +20,8 @@ export declare const validateTypedData: (data: unknown) => data is {
|
|
|
20
20
|
primaryType: string;
|
|
21
21
|
domain: {
|
|
22
22
|
version?: string | undefined;
|
|
23
|
-
name?: string | undefined;
|
|
24
23
|
chainId?: string | number | undefined;
|
|
24
|
+
name?: string | undefined;
|
|
25
25
|
};
|
|
26
26
|
message: Record<string, unknown>;
|
|
27
27
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
1
|
// Install the latest version of starknet with npm install starknet@next and import starknet
|
|
3
|
-
import * as starknet from
|
|
2
|
+
import * as starknet from 'starknet';
|
|
4
3
|
|
|
5
4
|
// Generate public and private key pair.
|
|
6
5
|
|
|
@@ -8,7 +7,11 @@ const keyPair = starknet.ec.genKeyPair();
|
|
|
8
7
|
const starkKey = starknet.ec.getStarkKey(keyPair);
|
|
9
8
|
const starkKeyInt = starknet.number.toBN(starknet.encode.removeHexPrefix(starkKey), 16);
|
|
10
9
|
|
|
11
|
-
const { address: walletAddressLocal } = await provider.deployContract({
|
|
10
|
+
const { address: walletAddressLocal } = await provider.deployContract({
|
|
11
|
+
contract: COMPILED_WALLET_CONTRACT_JSON,
|
|
12
|
+
constructorCallData: [starkKeyInt],
|
|
13
|
+
addressSalt: 0,
|
|
14
|
+
});
|
|
12
15
|
|
|
13
16
|
walletAddress = walletAddressLocal;
|
|
14
17
|
|
|
@@ -28,7 +31,7 @@ const balanceBeforeTransfer = await erc20.call('balance_of', {
|
|
|
28
31
|
user: walletAddress,
|
|
29
32
|
}).res;
|
|
30
33
|
|
|
31
|
-
console.log(number.toBN(res).toString())
|
|
34
|
+
console.log(number.toBN(res).toString());
|
|
32
35
|
|
|
33
36
|
const { nonce } = await wallet.call('get_nonce');
|
|
34
37
|
const msgHash = encode.addHexPrefix(
|
|
@@ -59,4 +62,4 @@ const balanceAfterTransfer = await erc20.call('balance_of', {
|
|
|
59
62
|
user: walletAddress,
|
|
60
63
|
}).res;
|
|
61
64
|
|
|
62
|
-
console.log('Balance after transfer', balanceAfterTransfer)
|
|
65
|
+
console.log('Balance after transfer', balanceAfterTransfer);
|
package/www/code-examples/amm.js
CHANGED
|
@@ -1,37 +1,33 @@
|
|
|
1
1
|
import { defaultProvider, stark } from 'starknet';
|
|
2
2
|
const { getSelectorFromName } = stark;
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
/**
|
|
6
5
|
* !! IMPORTANT NOTE !! When fees are introduced all function invocations will go through the account account contract and this example will be deprecated.
|
|
7
|
-
**/
|
|
6
|
+
**/
|
|
8
7
|
|
|
9
|
-
const CONTRACT_ADDRESS =
|
|
10
|
-
"0x03e19baa6cb2078631bcdb34844f3f7879449a544c9ce722681a54af08cff4b9";
|
|
8
|
+
const CONTRACT_ADDRESS = '0x03e19baa6cb2078631bcdb34844f3f7879449a544c9ce722681a54af08cff4b9';
|
|
11
9
|
|
|
12
10
|
/**
|
|
13
11
|
* invokeFunction() example
|
|
14
|
-
**/
|
|
12
|
+
**/
|
|
15
13
|
|
|
16
14
|
/** Reset the liquidity pool **/
|
|
17
|
-
const addLiquidityResponse = await defaultProvider.LEGACYinvokeFunction(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
);
|
|
15
|
+
const addLiquidityResponse = await defaultProvider.LEGACYinvokeFunction({
|
|
16
|
+
contractAddress: CONTRACT_ADDRESS,
|
|
17
|
+
entrypoint: 'init_pool',
|
|
18
|
+
calldata: ['1000000', '1000000'],
|
|
19
|
+
});
|
|
24
20
|
console.log(addLiquidityResponse);
|
|
25
21
|
|
|
26
22
|
/**
|
|
27
23
|
* callContract() example
|
|
28
|
-
**/
|
|
24
|
+
**/
|
|
29
25
|
|
|
30
26
|
/** Get the balance of the liquidity pool of token A **/
|
|
31
27
|
const poolBalanceTokenA = await defaultProvider.callContract({
|
|
32
28
|
contractAddress: CONTRACT_ADDRESS,
|
|
33
|
-
entrypoint:
|
|
34
|
-
calldata: [
|
|
29
|
+
entrypoint: 'get_pool_token_balance',
|
|
30
|
+
calldata: ['1'], // Account 1 (no account implemented)
|
|
35
31
|
});
|
|
36
32
|
const balanceA = poolBalanceTokenA.result[0];
|
|
37
33
|
console.log('token a liquidity pool balance: ', parseInt(balanceA, 16));
|
|
@@ -39,11 +35,10 @@ console.log('token a liquidity pool balance: ', parseInt(balanceA, 16));
|
|
|
39
35
|
/** Get the balance of the liquidity pool of token B **/
|
|
40
36
|
const poolBalanceTokenB = await defaultProvider.callContract({
|
|
41
37
|
contractAddress: CONTRACT_ADDRESS,
|
|
42
|
-
entrypoint:
|
|
43
|
-
calldata: [
|
|
38
|
+
entrypoint: 'get_pool_token_balance',
|
|
39
|
+
calldata: ['2'],
|
|
44
40
|
});
|
|
45
41
|
const balanceB = poolBalanceTokenB.result[0];
|
|
46
42
|
console.log('token b liquidity pool balance: ', parseInt(balanceB, 16));
|
|
47
43
|
|
|
48
|
-
|
|
49
44
|
/** Make a swap */
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import * as starknet from
|
|
1
|
+
import * as starknet from 'starknet';
|
|
2
2
|
|
|
3
3
|
const keyPair = starknet.ec.genKeyPair();
|
|
4
4
|
const starkKey = starknet.ec.getStarkKey(keyPair);
|
|
5
5
|
const starkKeyInt = starknet.number.toBN(starknet.encode.removeHexPrefix(starkKey), 16);
|
|
6
6
|
|
|
7
|
-
const deployWalletTx = await provider.deployContract({
|
|
7
|
+
const deployWalletTx = await provider.deployContract({
|
|
8
|
+
contract: COMPILED_WALLET_CONTRACT_JSON,
|
|
9
|
+
constructorCallData: [starkKeyInt],
|
|
10
|
+
addressSalt: 0,
|
|
11
|
+
});
|
|
8
12
|
|
|
9
13
|
await defaultProvider.waitForTx(deployWalletTx.transaction_hash);
|
|
10
|
-
|