starknet 3.13.1 → 3.15.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 CHANGED
@@ -1,3 +1,29 @@
1
+ # [3.15.0](https://github.com/0xs34n/starknet.js/compare/v3.14.1...v3.15.0) (2022-06-16)
2
+
3
+ ### Bug Fixes
4
+
5
+ - **tests:** update test ([c71f482](https://github.com/0xs34n/starknet.js/commit/c71f482facdaac914c45f4bf91f48e05a930abff))
6
+
7
+ ### Features
8
+
9
+ - add calculateContractAddressFromHash ([e22c346](https://github.com/0xs34n/starknet.js/commit/e22c3464036f97eee3c617d3790aac35b3d95379))
10
+
11
+ ## [3.14.1](https://github.com/0xs34n/starknet.js/compare/v3.14.0...v3.14.1) (2022-06-15)
12
+
13
+ ### Bug Fixes
14
+
15
+ - update api typings ([44796af](https://github.com/0xs34n/starknet.js/commit/44796af4849b6bab3d99065bb1e1948e4ea0b55e))
16
+
17
+ # [3.14.0](https://github.com/0xs34n/starknet.js/compare/v3.13.1...v3.14.0) (2022-06-15)
18
+
19
+ ### Bug Fixes
20
+
21
+ - remove redundant \_abi from declareContract() ([53d6578](https://github.com/0xs34n/starknet.js/commit/53d6578b932ed6046b5e0df83d748673d7efc3d5))
22
+
23
+ ### Features
24
+
25
+ - support contract declaration API introduced at SN 0.9.0 ([ca6203f](https://github.com/0xs34n/starknet.js/commit/ca6203f93471d7fb421d580e07d6de7c183e40f3))
26
+
1
27
  ## [3.13.1](https://github.com/0xs34n/starknet.js/compare/v3.13.0...v3.13.1) (2022-06-14)
2
28
 
3
29
  ### Bug Fixes
@@ -1,6 +1,6 @@
1
1
  import { defaultProvider, stark } from '../src';
2
2
  import { toBN } from '../src/utils/number';
3
- import { compiledArgentAccount } from './fixtures';
3
+ import { compiledArgentAccount, compiledErc20 } from './fixtures';
4
4
 
5
5
  const { compileCalldata } = stark;
6
6
 
@@ -85,20 +85,43 @@ describe('defaultProvider', () => {
85
85
  )
86
86
  ).resolves.not.toThrow();
87
87
  });
88
- test('getTransaction()', async () => {
89
- return expect(
90
- defaultProvider.getTransaction(
91
- '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
92
- )
93
- ).resolves.not.toThrow();
88
+
89
+ test('getTransaction() - successful transaction', async () => {
90
+ const transaction = await defaultProvider.getTransaction(
91
+ '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
92
+ );
93
+
94
+ expect(transaction).not.toHaveProperty('transaction_failure_reason');
95
+
96
+ expect(transaction.transaction).toHaveProperty('transaction_hash');
97
+
98
+ return expect(transaction.status).not.toEqual('REJECTED');
94
99
  });
95
100
 
96
- test('getTransactionReceipt', async () => {
97
- return expect(
98
- defaultProvider.getTransactionReceipt(
99
- '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
100
- )
101
- ).resolves.not.toThrow();
101
+ test('getTransaction() - failed transaction', async () => {
102
+ const transaction = await defaultProvider.getTransaction(
103
+ '0x698e60db2bae3ef8d5fafda10ff83b6ba634351aa1f4fcf8455ec0cffa738d9'
104
+ );
105
+
106
+ expect(transaction).toHaveProperty('transaction_failure_reason');
107
+
108
+ return expect(transaction.status).toEqual('REJECTED');
109
+ });
110
+
111
+ test('getTransactionReceipt() - successful transaction', async () => {
112
+ const transactionReceipt = await defaultProvider.getTransactionReceipt(
113
+ '0x18c49389193b40e178dfc9f2f595a7c79a7a55639e9951d956329f2ce6cfd4f'
114
+ );
115
+
116
+ return expect(transactionReceipt).toHaveProperty('actual_fee');
117
+ });
118
+
119
+ test('getTransactionReceipt() - failed transaction', async () => {
120
+ const transactionReceipt = await defaultProvider.getTransactionReceipt(
121
+ '0x698e60db2bae3ef8d5fafda10ff83b6ba634351aa1f4fcf8455ec0cffa738d9'
122
+ );
123
+
124
+ return expect(transactionReceipt).not.toHaveProperty('actual_fee');
102
125
  });
103
126
 
104
127
  test('callContract()', () => {
@@ -123,6 +146,16 @@ describe('defaultProvider', () => {
123
146
  });
124
147
 
125
148
  describe('addTransaction()', () => {
149
+ test('declareContract()', async () => {
150
+ const response = await defaultProvider.declareContract({
151
+ contract: compiledErc20,
152
+ });
153
+
154
+ expect(response.code).toBe('TRANSACTION_RECEIVED');
155
+ expect(response.transaction_hash).toBeDefined();
156
+ expect(response.class_hash).toBeDefined();
157
+ });
158
+
126
159
  test('deployContract()', async () => {
127
160
  const response = await defaultProvider.deployContract({
128
161
  contract: compiledArgentAccount,
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs';
2
2
 
3
3
  import { constants, hash, json, number, stark } from '../../src';
4
+ import { pedersen } from '../../src/utils/hash';
4
5
 
5
6
  const { IS_BROWSER } = constants;
6
7
 
@@ -87,3 +88,29 @@ describe('estimatedFeeToMaxFee()', () => {
87
88
  expect(res).toBe(11_500);
88
89
  });
89
90
  });
91
+
92
+ describe('calculateContractAddressFromHash()', () => {
93
+ // This test just show how to use calculateContractAddressFromHash for new devs
94
+
95
+ test('calculated contract address should match the snapshot', () => {
96
+ const ethAddress = '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7';
97
+
98
+ const daiAddress = '0x03e85bfbb8e2a42b7bead9e88e9a1b19dbccf661471061807292120462396ec9';
99
+ const factoryAddress = '0x249827618A01858A72B7D04339C47195A324D20D6037033DFE2829F98AFF4FC';
100
+ const classHash = '0x55187E68C60664A947048E0C9E5322F9BF55F7D435ECDCF17ED75724E77368F';
101
+
102
+ // Any type of salt can be used. It depends on the dApp what kind of salt it wants to use.
103
+ const salt = pedersen([ethAddress, daiAddress]);
104
+
105
+ const res = hash.calculateContractAddressFromHash(
106
+ salt,
107
+ classHash,
108
+ [ethAddress, daiAddress, factoryAddress],
109
+ factoryAddress
110
+ );
111
+
112
+ expect(res).toMatchInlineSnapshot(
113
+ `"0x36dc8dcb3440596472ddde11facacc45d0cd250df764ae7c3d1a360c853c324"`
114
+ );
115
+ });
116
+ });
@@ -424,6 +424,7 @@ var Account = /** @class */ (function (_super) {
424
424
  switch (_a.label) {
425
425
  case 0:
426
426
  if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
427
+ if (transaction.type === 'DECLARE') throw new Error('No DECLARES');
427
428
  (0,
428
429
  minimalistic_assert_1.default)(!transaction.signature, "Adding signatures to a signer transaction currently isn't supported");
429
430
  if (!transaction.nonce) return [3 /*break*/, 1];
package/constants.d.ts CHANGED
@@ -10,6 +10,7 @@ export declare enum StarknetChainId {
10
10
  TESTNET = '0x534e5f474f45524c49',
11
11
  }
12
12
  export declare enum TransactionHashPrefix {
13
+ DECLARE = '0x6465636c617265',
13
14
  DEPLOY = '0x6465706c6f79',
14
15
  INVOKE = '0x696e766f6b65',
15
16
  L1_HANDLER = '0x6c315f68616e646c6572',
package/constants.js CHANGED
@@ -37,6 +37,7 @@ var StarknetChainId;
37
37
  })((StarknetChainId = exports.StarknetChainId || (exports.StarknetChainId = {})));
38
38
  var TransactionHashPrefix;
39
39
  (function (TransactionHashPrefix) {
40
+ TransactionHashPrefix['DECLARE'] = '0x6465636c617265';
40
41
  TransactionHashPrefix['DEPLOY'] = '0x6465706c6f79';
41
42
  TransactionHashPrefix['INVOKE'] = '0x696e766f6b65';
42
43
  TransactionHashPrefix['L1_HANDLER'] = '0x6c315f68616e646c6572';
@@ -267,6 +267,8 @@ var Account = /** @class */ (function (_super) {
267
267
  case 0:
268
268
  if (transaction.type === 'DEPLOY')
269
269
  throw new Error('No DEPLOYS');
270
+ if (transaction.type === 'DECLARE')
271
+ throw new Error('No DECLARES');
270
272
  (0, minimalistic_assert_1.default)(!transaction.signature, "Adding signatures to a signer transaction currently isn't supported");
271
273
  if (!transaction.nonce) return [3 /*break*/, 1];
272
274
  nonceBn = (0, number_1.toBN)(transaction.nonce);
@@ -10,6 +10,7 @@ export declare enum StarknetChainId {
10
10
  TESTNET = "0x534e5f474f45524c49"
11
11
  }
12
12
  export declare enum TransactionHashPrefix {
13
+ DECLARE = "0x6465636c617265",
13
14
  DEPLOY = "0x6465706c6f79",
14
15
  INVOKE = "0x696e766f6b65",
15
16
  L1_HANDLER = "0x6c315f68616e646c6572"
package/dist/constants.js CHANGED
@@ -16,6 +16,7 @@ var StarknetChainId;
16
16
  })(StarknetChainId = exports.StarknetChainId || (exports.StarknetChainId = {}));
17
17
  var TransactionHashPrefix;
18
18
  (function (TransactionHashPrefix) {
19
+ TransactionHashPrefix["DECLARE"] = "0x6465636c617265";
19
20
  TransactionHashPrefix["DEPLOY"] = "0x6465706c6f79";
20
21
  TransactionHashPrefix["INVOKE"] = "0x696e766f6b65";
21
22
  TransactionHashPrefix["L1_HANDLER"] = "0x6c315f68616e646c6572";
@@ -1,5 +1,5 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Invocation, TransactionReceiptResponse } from '../types';
2
+ import { Abi, AddTransactionResponse, Call, CallContractResponse, DeclareContractPayload, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Invocation, TransactionReceiptResponse } from '../types';
3
3
  import { BigNumberish } from '../utils/number';
4
4
  import { ProviderInterface } from './interface';
5
5
  import { BlockIdentifier } from './utils';
@@ -110,6 +110,13 @@ export declare class Provider implements ProviderInterface {
110
110
  * @returns the transaction trace
111
111
  */
112
112
  getTransactionTrace(txHash: BigNumberish): Promise<GetTransactionTraceResponse>;
113
+ /**
114
+ * Declare a given compiled contract (json) on starknet
115
+ *
116
+ * @param contract - a json object containing the compiled contract
117
+ * @returns a confirmation of sending a transaction on the starknet contract
118
+ */
119
+ declareContract(payload: DeclareContractPayload): Promise<AddTransactionResponse>;
113
120
  /**
114
121
  * Deploys a given compiled contract (json) to starknet
115
122
  *
@@ -370,6 +370,25 @@ var Provider = /** @class */ (function () {
370
370
  });
371
371
  });
372
372
  };
373
+ /**
374
+ * Declare a given compiled contract (json) on starknet
375
+ *
376
+ * @param contract - a json object containing the compiled contract
377
+ * @returns a confirmation of sending a transaction on the starknet contract
378
+ */
379
+ Provider.prototype.declareContract = function (payload) {
380
+ var parsedContract = typeof payload.contract === 'string'
381
+ ? (0, json_1.parse)(payload.contract)
382
+ : payload.contract;
383
+ var contractDefinition = __assign(__assign({}, parsedContract), { program: (0, stark_1.compressProgram)(parsedContract.program) });
384
+ return this.fetchEndpoint('add_transaction', undefined, {
385
+ type: 'DECLARE',
386
+ contract_class: contractDefinition,
387
+ nonce: (0, number_1.toHex)(constants_1.ZERO),
388
+ signature: [],
389
+ sender_address: (0, number_1.toHex)(constants_1.ONE),
390
+ });
391
+ };
373
392
  /**
374
393
  * Deploys a given compiled contract (json) to starknet
375
394
  *
@@ -84,6 +84,13 @@ export declare type GetContractAddressesResponse = {
84
84
  Starknet: string;
85
85
  GpsStatementVerifier: string;
86
86
  };
87
+ export declare type DeclareTransaction = {
88
+ type: 'DECLARE';
89
+ contract_class: CompressedCompiledContract;
90
+ nonce: BigNumberish;
91
+ sender_address: BigNumberish;
92
+ signature: Signature;
93
+ };
87
94
  export declare type DeployTransaction = {
88
95
  type: 'DEPLOY';
89
96
  contract_definition: CompressedCompiledContract;
@@ -122,12 +129,12 @@ export declare type ExecutionResources = {
122
129
  bitwise_builtin: number;
123
130
  output_builtin: number;
124
131
  ecdsa_builtin: number;
125
- ec_op_builtin: number;
132
+ ec_op_builtin?: number;
126
133
  };
127
134
  n_memory_holes: number;
128
135
  };
129
136
  export declare type CallContractTransaction = Omit<InvokeFunctionTransaction, 'type' | 'entry_point_type' | 'nonce'>;
130
- export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
137
+ export declare type Transaction = DeclareTransaction | DeployTransaction | InvokeFunctionTransaction;
131
138
  export declare type CallContractResponse = {
132
139
  result: string[];
133
140
  };
@@ -153,8 +160,9 @@ export declare type GetBlockResponse = {
153
160
  transaction_index: number;
154
161
  };
155
162
  };
156
- previous_block_hash: string;
163
+ parent_block_hash: string;
157
164
  status: Status;
165
+ gas_price: string;
158
166
  };
159
167
  export declare type GetCodeResponse = {
160
168
  bytecode: string[];
@@ -162,9 +170,8 @@ export declare type GetCodeResponse = {
162
170
  };
163
171
  export declare type GetTransactionStatusResponse = {
164
172
  tx_status: Status;
165
- block_hash: string;
173
+ block_hash?: string;
166
174
  tx_failure_reason?: {
167
- tx_id: number;
168
175
  code: string;
169
176
  error_message: string;
170
177
  };
@@ -177,27 +184,36 @@ export declare type GetTransactionTraceResponse = {
177
184
  selector: string;
178
185
  calldata: RawArgs;
179
186
  result: Array<any>;
180
- execution_resources: any;
187
+ execution_resources: ExecutionResources;
181
188
  internal_call: Array<any>;
182
189
  events: Array<any>;
183
190
  messages: Array<any>;
184
191
  };
185
192
  signature: Signature;
186
193
  };
187
- export declare type GetTransactionResponse = {
194
+ export declare type SuccessfulTransactionResponse = {
188
195
  status: Status;
189
196
  transaction: Transaction;
190
197
  block_hash: string;
191
198
  block_number: BlockNumber;
192
199
  transaction_index: number;
193
- transaction_hash: string;
194
200
  };
201
+ export declare type FailedTransactionResponse = {
202
+ status: 'REJECTED';
203
+ transaction_failure_reason: {
204
+ code: string;
205
+ error_message: string;
206
+ };
207
+ transaction: Transaction;
208
+ };
209
+ export declare type GetTransactionResponse = SuccessfulTransactionResponse | FailedTransactionResponse;
195
210
  export declare type AddTransactionResponse = {
196
211
  code: TransactionStatus;
197
212
  transaction_hash: string;
198
213
  address?: string;
214
+ class_hash?: string;
199
215
  };
200
- export declare type TransactionReceiptResponse = {
216
+ export declare type SuccessfulTransactionReceiptResponse = {
201
217
  status: Status;
202
218
  transaction_hash: string;
203
219
  transaction_index: number;
@@ -205,7 +221,20 @@ export declare type TransactionReceiptResponse = {
205
221
  block_number: BlockNumber;
206
222
  l2_to_l1_messages: string[];
207
223
  events: string[];
224
+ actual_fee: string;
225
+ execution_resources: ExecutionResources;
226
+ };
227
+ export declare type FailedTransactionReceiptResponse = {
228
+ status: 'REJECTED';
229
+ transaction_failure_reason: {
230
+ code: string;
231
+ error_message: string;
232
+ };
233
+ transaction_hash: string;
234
+ l2_to_l1_messages: string[];
235
+ events: string[];
208
236
  };
237
+ export declare type TransactionReceiptResponse = SuccessfulTransactionReceiptResponse | FailedTransactionReceiptResponse;
209
238
  export declare type EstimateFeeResponse = {
210
239
  amount: BN;
211
240
  unit: string;
@@ -8,6 +8,9 @@ export declare type DeployContractPayload = {
8
8
  constructorCalldata?: RawCalldata;
9
9
  addressSalt?: BigNumberish;
10
10
  };
11
+ export declare type DeclareContractPayload = {
12
+ contract: CompiledContract | string;
13
+ };
11
14
  export declare type Invocation = {
12
15
  contractAddress: string;
13
16
  entrypoint: string;
@@ -22,7 +25,7 @@ export declare type InvocationsDetails = {
22
25
  };
23
26
  export declare type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'ACCEPTED_ON_L2' | 'ACCEPTED_ON_L1' | 'REJECTED';
24
27
  export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
25
- export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
28
+ export declare type Type = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION';
26
29
  export declare type EntryPointType = 'EXTERNAL';
27
30
  export declare type CompressedProgram = string;
28
31
  export declare type AbiEntry = {
@@ -1,5 +1,6 @@
1
1
  import BN from 'bn.js';
2
2
  import { StarknetChainId, TransactionHashPrefix } from '../constants';
3
+ import { RawCalldata } from '../types/lib';
3
4
  import { BigNumberish } from './number';
4
5
  export declare const transactionVersion = 0;
5
6
  export declare const feeTransactionVersion: BN;
@@ -24,3 +25,4 @@ export declare function computeHashOnElements(data: BigNumberish[]): string;
24
25
  export declare function calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish, contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData?: BigNumberish[]): string;
25
26
  export declare function calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: BigNumberish[], version: BigNumberish, chainId: StarknetChainId): string;
26
27
  export declare function calculcateTransactionHash(contractAddress: BigNumberish, version: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId): string;
28
+ export declare function calculateContractAddressFromHash(salt: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, deployerAddress: BigNumberish): string;
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  return (mod && mod.__esModule) ? mod : { "default": mod };
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.calculcateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.feeTransactionVersion = exports.transactionVersion = void 0;
31
+ exports.calculateContractAddressFromHash = exports.calculcateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.feeTransactionVersion = exports.transactionVersion = void 0;
32
32
  var keccak_1 = require("ethereum-cryptography/keccak");
33
33
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
34
34
  var constants_1 = require("../constants");
@@ -112,3 +112,16 @@ function calculcateTransactionHash(contractAddress, version, entryPointSelector,
112
112
  return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.INVOKE, version, contractAddress, entryPointSelector, calldata, maxFee, chainId);
113
113
  }
114
114
  exports.calculcateTransactionHash = calculcateTransactionHash;
115
+ function calculateContractAddressFromHash(salt, classHash, constructorCalldata, deployerAddress) {
116
+ var constructorCalldataHash = computeHashOnElements(constructorCalldata);
117
+ var CONTRACT_ADDRESS_PREFIX = (0, number_1.toFelt)('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
118
+ var dataToHash = [
119
+ CONTRACT_ADDRESS_PREFIX,
120
+ deployerAddress,
121
+ salt,
122
+ classHash,
123
+ constructorCalldataHash,
124
+ ];
125
+ return computeHashOnElements(dataToHash);
126
+ }
127
+ exports.calculateContractAddressFromHash = calculateContractAddressFromHash;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "3.13.1",
3
+ "version": "3.15.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -4,6 +4,7 @@ import {
4
4
  AddTransactionResponse,
5
5
  Call,
6
6
  CallContractResponse,
7
+ DeclareContractPayload,
7
8
  DeployContractPayload,
8
9
  Endpoints,
9
10
  GetBlockResponse,
@@ -147,6 +148,13 @@ export declare class Provider implements ProviderInterface {
147
148
  * @returns the transaction trace
148
149
  */
149
150
  getTransactionTrace(txHash: BigNumberish): Promise<GetTransactionTraceResponse>;
151
+ /**
152
+ * Declare a given compiled contract (json) on starknet
153
+ *
154
+ * @param contract - a json object containing the compiled contract
155
+ * @returns a confirmation of sending a transaction on the starknet contract
156
+ */
157
+ declareContract(payload: DeclareContractPayload): Promise<AddTransactionResponse>;
150
158
  /**
151
159
  * Deploys a given compiled contract (json) to starknet
152
160
  *
@@ -536,6 +536,26 @@ var Provider = /** @class */ (function () {
536
536
  });
537
537
  });
538
538
  };
539
+ /**
540
+ * Declare a given compiled contract (json) on starknet
541
+ *
542
+ * @param contract - a json object containing the compiled contract
543
+ * @returns a confirmation of sending a transaction on the starknet contract
544
+ */
545
+ Provider.prototype.declareContract = function (payload) {
546
+ var parsedContract =
547
+ typeof payload.contract === 'string' ? (0, json_1.parse)(payload.contract) : payload.contract;
548
+ var contractDefinition = __assign(__assign({}, parsedContract), {
549
+ program: (0, stark_1.compressProgram)(parsedContract.program),
550
+ });
551
+ return this.fetchEndpoint('add_transaction', undefined, {
552
+ type: 'DECLARE',
553
+ contract_class: contractDefinition,
554
+ nonce: (0, number_1.toHex)(constants_1.ZERO),
555
+ signature: [],
556
+ sender_address: (0, number_1.toHex)(constants_1.ONE),
557
+ });
558
+ };
539
559
  /**
540
560
  * Deploys a given compiled contract (json) to starknet
541
561
  *
@@ -143,6 +143,7 @@ export class Account extends Provider implements AccountInterface {
143
143
  */
144
144
  public async LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
145
145
  if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
146
+ if (transaction.type === 'DECLARE') throw new Error('No DECLARES');
146
147
 
147
148
  assert(
148
149
  !transaction.signature,
package/src/constants.ts CHANGED
@@ -13,6 +13,7 @@ export enum StarknetChainId {
13
13
  TESTNET = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI'),
14
14
  }
15
15
  export enum TransactionHashPrefix {
16
+ DECLARE = '0x6465636c617265', // encodeShortString('declare'),
16
17
  DEPLOY = '0x6465706c6f79', // encodeShortString('deploy'),
17
18
  INVOKE = '0x696e766f6b65', // encodeShortString('invoke'),
18
19
  L1_HANDLER = '0x6c315f68616e646c6572', // encodeShortString('l1_handler'),
@@ -1,13 +1,14 @@
1
1
  import fetch from 'cross-fetch';
2
2
  import urljoin from 'url-join';
3
3
 
4
- import { StarknetChainId } from '../constants';
4
+ import { ONE, StarknetChainId, ZERO } from '../constants';
5
5
  import {
6
6
  Abi,
7
7
  AddTransactionResponse,
8
8
  Call,
9
9
  CallContractResponse,
10
10
  CompiledContract,
11
+ DeclareContractPayload,
11
12
  DeployContractPayload,
12
13
  Endpoints,
13
14
  GetBlockResponse,
@@ -309,6 +310,31 @@ export class Provider implements ProviderInterface {
309
310
  return this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex });
310
311
  }
311
312
 
313
+ /**
314
+ * Declare a given compiled contract (json) on starknet
315
+ *
316
+ * @param contract - a json object containing the compiled contract
317
+ * @returns a confirmation of sending a transaction on the starknet contract
318
+ */
319
+ public declareContract(payload: DeclareContractPayload): Promise<AddTransactionResponse> {
320
+ const parsedContract =
321
+ typeof payload.contract === 'string'
322
+ ? (parse(payload.contract) as CompiledContract)
323
+ : payload.contract;
324
+ const contractDefinition = {
325
+ ...parsedContract,
326
+ program: compressProgram(parsedContract.program),
327
+ };
328
+
329
+ return this.fetchEndpoint('add_transaction', undefined, {
330
+ type: 'DECLARE',
331
+ contract_class: contractDefinition,
332
+ nonce: toHex(ZERO),
333
+ signature: [],
334
+ sender_address: toHex(ONE),
335
+ });
336
+ }
337
+
312
338
  /**
313
339
  * Deploys a given compiled contract (json) to starknet
314
340
  *
package/src/types/api.ts CHANGED
@@ -97,6 +97,14 @@ export type GetContractAddressesResponse = {
97
97
  GpsStatementVerifier: string;
98
98
  };
99
99
 
100
+ export type DeclareTransaction = {
101
+ type: 'DECLARE';
102
+ contract_class: CompressedCompiledContract;
103
+ nonce: BigNumberish;
104
+ sender_address: BigNumberish;
105
+ signature: Signature;
106
+ };
107
+
100
108
  export type DeployTransaction = {
101
109
  type: 'DEPLOY';
102
110
  contract_definition: CompressedCompiledContract;
@@ -138,7 +146,7 @@ export type ExecutionResources = {
138
146
  bitwise_builtin: number;
139
147
  output_builtin: number;
140
148
  ecdsa_builtin: number;
141
- ec_op_builtin: number;
149
+ ec_op_builtin?: number;
142
150
  };
143
151
  n_memory_holes: number;
144
152
  };
@@ -148,7 +156,7 @@ export type CallContractTransaction = Omit<
148
156
  'type' | 'entry_point_type' | 'nonce'
149
157
  >;
150
158
 
151
- export type Transaction = DeployTransaction | InvokeFunctionTransaction;
159
+ export type Transaction = DeclareTransaction | DeployTransaction | InvokeFunctionTransaction;
152
160
 
153
161
  export type CallContractResponse = {
154
162
  result: string[];
@@ -176,8 +184,9 @@ export type GetBlockResponse = {
176
184
  transaction_index: number;
177
185
  };
178
186
  };
179
- previous_block_hash: string;
187
+ parent_block_hash: string;
180
188
  status: Status;
189
+ gas_price: string;
181
190
  };
182
191
 
183
192
  export type GetCodeResponse = {
@@ -187,9 +196,8 @@ export type GetCodeResponse = {
187
196
 
188
197
  export type GetTransactionStatusResponse = {
189
198
  tx_status: Status;
190
- block_hash: string;
199
+ block_hash?: string;
191
200
  tx_failure_reason?: {
192
- tx_id: number;
193
201
  code: string;
194
202
  error_message: string;
195
203
  };
@@ -203,7 +211,7 @@ export type GetTransactionTraceResponse = {
203
211
  selector: string;
204
212
  calldata: RawArgs;
205
213
  result: Array<any>;
206
- execution_resources: any;
214
+ execution_resources: ExecutionResources;
207
215
  internal_call: Array<any>;
208
216
  events: Array<any>;
209
217
  messages: Array<any>;
@@ -211,22 +219,33 @@ export type GetTransactionTraceResponse = {
211
219
  signature: Signature;
212
220
  };
213
221
 
214
- export type GetTransactionResponse = {
222
+ export type SuccessfulTransactionResponse = {
215
223
  status: Status;
216
224
  transaction: Transaction;
217
225
  block_hash: string;
218
226
  block_number: BlockNumber;
219
227
  transaction_index: number;
220
- transaction_hash: string;
221
228
  };
222
229
 
230
+ export type FailedTransactionResponse = {
231
+ status: 'REJECTED';
232
+ transaction_failure_reason: {
233
+ code: string;
234
+ error_message: string;
235
+ };
236
+ transaction: Transaction;
237
+ };
238
+
239
+ export type GetTransactionResponse = SuccessfulTransactionResponse | FailedTransactionResponse;
240
+
223
241
  export type AddTransactionResponse = {
224
242
  code: TransactionStatus;
225
243
  transaction_hash: string;
226
244
  address?: string;
245
+ class_hash?: string;
227
246
  };
228
247
 
229
- export type TransactionReceiptResponse = {
248
+ export type SuccessfulTransactionReceiptResponse = {
230
249
  status: Status;
231
250
  transaction_hash: string;
232
251
  transaction_index: number;
@@ -234,8 +253,25 @@ export type TransactionReceiptResponse = {
234
253
  block_number: BlockNumber;
235
254
  l2_to_l1_messages: string[];
236
255
  events: string[];
256
+ actual_fee: string;
257
+ execution_resources: ExecutionResources;
237
258
  };
238
259
 
260
+ export type FailedTransactionReceiptResponse = {
261
+ status: 'REJECTED';
262
+ transaction_failure_reason: {
263
+ code: string;
264
+ error_message: string;
265
+ };
266
+ transaction_hash: string;
267
+ l2_to_l1_messages: string[];
268
+ events: string[];
269
+ };
270
+
271
+ export type TransactionReceiptResponse =
272
+ | SuccessfulTransactionReceiptResponse
273
+ | FailedTransactionReceiptResponse;
274
+
239
275
  export type EstimateFeeResponse = {
240
276
  amount: BN;
241
277
  unit: string;
package/src/types/lib.ts CHANGED
@@ -12,6 +12,10 @@ export type DeployContractPayload = {
12
12
  addressSalt?: BigNumberish;
13
13
  };
14
14
 
15
+ export type DeclareContractPayload = {
16
+ contract: CompiledContract | string;
17
+ };
18
+
15
19
  export type Invocation = {
16
20
  contractAddress: string;
17
21
  entrypoint: string;
@@ -35,7 +39,7 @@ export type Status =
35
39
  | 'ACCEPTED_ON_L1'
36
40
  | 'REJECTED';
37
41
  export type TransactionStatus = 'TRANSACTION_RECEIVED';
38
- export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
42
+ export type Type = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION';
39
43
  export type EntryPointType = 'EXTERNAL';
40
44
  export type CompressedProgram = string;
41
45
 
package/src/utils/hash.ts CHANGED
@@ -11,9 +11,10 @@ import {
11
11
  TransactionHashPrefix,
12
12
  ZERO,
13
13
  } from '../constants';
14
+ import { RawCalldata } from '../types/lib';
14
15
  import { ec } from './ellipticCurve';
15
16
  import { addHexPrefix, buf2hex, utf8ToArray } from './encode';
16
- import { BigNumberish, toBN, toHex } from './number';
17
+ import { BigNumberish, toBN, toFelt, toHex } from './number';
17
18
 
18
19
  export const transactionVersion = 0;
19
20
  export const feeTransactionVersion = toBN(2).pow(toBN(128)).add(toBN(transactionVersion));
@@ -132,3 +133,24 @@ export function calculcateTransactionHash(
132
133
  chainId
133
134
  );
134
135
  }
136
+
137
+ export function calculateContractAddressFromHash(
138
+ salt: BigNumberish,
139
+ classHash: BigNumberish,
140
+ constructorCalldata: RawCalldata,
141
+ deployerAddress: BigNumberish
142
+ ) {
143
+ const constructorCalldataHash = computeHashOnElements(constructorCalldata);
144
+
145
+ const CONTRACT_ADDRESS_PREFIX = toFelt('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
146
+
147
+ const dataToHash = [
148
+ CONTRACT_ADDRESS_PREFIX,
149
+ deployerAddress,
150
+ salt,
151
+ classHash,
152
+ constructorCalldataHash,
153
+ ];
154
+
155
+ return computeHashOnElements(dataToHash);
156
+ }
package/types/api.d.ts CHANGED
@@ -94,6 +94,13 @@ export declare type GetContractAddressesResponse = {
94
94
  Starknet: string;
95
95
  GpsStatementVerifier: string;
96
96
  };
97
+ export declare type DeclareTransaction = {
98
+ type: 'DECLARE';
99
+ contract_class: CompressedCompiledContract;
100
+ nonce: BigNumberish;
101
+ sender_address: BigNumberish;
102
+ signature: Signature;
103
+ };
97
104
  export declare type DeployTransaction = {
98
105
  type: 'DEPLOY';
99
106
  contract_definition: CompressedCompiledContract;
@@ -132,7 +139,7 @@ export declare type ExecutionResources = {
132
139
  bitwise_builtin: number;
133
140
  output_builtin: number;
134
141
  ecdsa_builtin: number;
135
- ec_op_builtin: number;
142
+ ec_op_builtin?: number;
136
143
  };
137
144
  n_memory_holes: number;
138
145
  };
@@ -140,7 +147,10 @@ export declare type CallContractTransaction = Omit<
140
147
  InvokeFunctionTransaction,
141
148
  'type' | 'entry_point_type' | 'nonce'
142
149
  >;
143
- export declare type Transaction = DeployTransaction | InvokeFunctionTransaction;
150
+ export declare type Transaction =
151
+ | DeclareTransaction
152
+ | DeployTransaction
153
+ | InvokeFunctionTransaction;
144
154
  export declare type CallContractResponse = {
145
155
  result: string[];
146
156
  };
@@ -166,8 +176,9 @@ export declare type GetBlockResponse = {
166
176
  transaction_index: number;
167
177
  };
168
178
  };
169
- previous_block_hash: string;
179
+ parent_block_hash: string;
170
180
  status: Status;
181
+ gas_price: string;
171
182
  };
172
183
  export declare type GetCodeResponse = {
173
184
  bytecode: string[];
@@ -175,9 +186,8 @@ export declare type GetCodeResponse = {
175
186
  };
176
187
  export declare type GetTransactionStatusResponse = {
177
188
  tx_status: Status;
178
- block_hash: string;
189
+ block_hash?: string;
179
190
  tx_failure_reason?: {
180
- tx_id: number;
181
191
  code: string;
182
192
  error_message: string;
183
193
  };
@@ -190,27 +200,38 @@ export declare type GetTransactionTraceResponse = {
190
200
  selector: string;
191
201
  calldata: RawArgs;
192
202
  result: Array<any>;
193
- execution_resources: any;
203
+ execution_resources: ExecutionResources;
194
204
  internal_call: Array<any>;
195
205
  events: Array<any>;
196
206
  messages: Array<any>;
197
207
  };
198
208
  signature: Signature;
199
209
  };
200
- export declare type GetTransactionResponse = {
210
+ export declare type SuccessfulTransactionResponse = {
201
211
  status: Status;
202
212
  transaction: Transaction;
203
213
  block_hash: string;
204
214
  block_number: BlockNumber;
205
215
  transaction_index: number;
206
- transaction_hash: string;
207
216
  };
217
+ export declare type FailedTransactionResponse = {
218
+ status: 'REJECTED';
219
+ transaction_failure_reason: {
220
+ code: string;
221
+ error_message: string;
222
+ };
223
+ transaction: Transaction;
224
+ };
225
+ export declare type GetTransactionResponse =
226
+ | SuccessfulTransactionResponse
227
+ | FailedTransactionResponse;
208
228
  export declare type AddTransactionResponse = {
209
229
  code: TransactionStatus;
210
230
  transaction_hash: string;
211
231
  address?: string;
232
+ class_hash?: string;
212
233
  };
213
- export declare type TransactionReceiptResponse = {
234
+ export declare type SuccessfulTransactionReceiptResponse = {
214
235
  status: Status;
215
236
  transaction_hash: string;
216
237
  transaction_index: number;
@@ -218,7 +239,22 @@ export declare type TransactionReceiptResponse = {
218
239
  block_number: BlockNumber;
219
240
  l2_to_l1_messages: string[];
220
241
  events: string[];
242
+ actual_fee: string;
243
+ execution_resources: ExecutionResources;
244
+ };
245
+ export declare type FailedTransactionReceiptResponse = {
246
+ status: 'REJECTED';
247
+ transaction_failure_reason: {
248
+ code: string;
249
+ error_message: string;
250
+ };
251
+ transaction_hash: string;
252
+ l2_to_l1_messages: string[];
253
+ events: string[];
221
254
  };
255
+ export declare type TransactionReceiptResponse =
256
+ | SuccessfulTransactionReceiptResponse
257
+ | FailedTransactionReceiptResponse;
222
258
  export declare type EstimateFeeResponse = {
223
259
  amount: BN;
224
260
  unit: string;
package/types/lib.d.ts CHANGED
@@ -9,6 +9,9 @@ export declare type DeployContractPayload = {
9
9
  constructorCalldata?: RawCalldata;
10
10
  addressSalt?: BigNumberish;
11
11
  };
12
+ export declare type DeclareContractPayload = {
13
+ contract: CompiledContract | string;
14
+ };
12
15
  export declare type Invocation = {
13
16
  contractAddress: string;
14
17
  entrypoint: string;
@@ -29,7 +32,7 @@ export declare type Status =
29
32
  | 'ACCEPTED_ON_L1'
30
33
  | 'REJECTED';
31
34
  export declare type TransactionStatus = 'TRANSACTION_RECEIVED';
32
- export declare type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
35
+ export declare type Type = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION';
33
36
  export declare type EntryPointType = 'EXTERNAL';
34
37
  export declare type CompressedProgram = string;
35
38
  export declare type AbiEntry = {
package/utils/hash.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import BN from 'bn.js';
2
2
 
3
3
  import { StarknetChainId, TransactionHashPrefix } from '../constants';
4
+ import { RawCalldata } from '../types/lib';
4
5
  import { BigNumberish } from './number';
5
6
  export declare const transactionVersion = 0;
6
7
  export declare const feeTransactionVersion: BN;
@@ -46,3 +47,9 @@ export declare function calculcateTransactionHash(
46
47
  maxFee: BigNumberish,
47
48
  chainId: StarknetChainId
48
49
  ): string;
50
+ export declare function calculateContractAddressFromHash(
51
+ salt: BigNumberish,
52
+ classHash: BigNumberish,
53
+ constructorCalldata: RawCalldata,
54
+ deployerAddress: BigNumberish
55
+ ): string;
package/utils/hash.js CHANGED
@@ -39,7 +39,8 @@ var __importDefault =
39
39
  return mod && mod.__esModule ? mod : { default: mod };
40
40
  };
41
41
  Object.defineProperty(exports, '__esModule', { value: true });
42
- exports.calculcateTransactionHash =
42
+ exports.calculateContractAddressFromHash =
43
+ exports.calculcateTransactionHash =
43
44
  exports.calculateDeployTransactionHash =
44
45
  exports.calculateTransactionHashCommon =
45
46
  exports.computeHashOnElements =
@@ -174,3 +175,18 @@ function calculcateTransactionHash(
174
175
  );
175
176
  }
176
177
  exports.calculcateTransactionHash = calculcateTransactionHash;
178
+ function calculateContractAddressFromHash(salt, classHash, constructorCalldata, deployerAddress) {
179
+ var constructorCalldataHash = computeHashOnElements(constructorCalldata);
180
+ var CONTRACT_ADDRESS_PREFIX = (0, number_1.toFelt)(
181
+ '0x535441524b4e45545f434f4e54524143545f41444452455353'
182
+ ); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
183
+ var dataToHash = [
184
+ CONTRACT_ADDRESS_PREFIX,
185
+ deployerAddress,
186
+ salt,
187
+ classHash,
188
+ constructorCalldataHash,
189
+ ];
190
+ return computeHashOnElements(dataToHash);
191
+ }
192
+ exports.calculateContractAddressFromHash = calculateContractAddressFromHash;
@@ -198,9 +198,26 @@ Gets the transaction trace from a tx hash.
198
198
 
199
199
  <hr/>
200
200
 
201
+ provider.**declareContract**(payload) => _Promise < AddTransactionResponse >_
202
+
203
+ Declares a contract on Starknet
204
+
205
+ ###### _AddTransactionResponse_
206
+
207
+ ```
208
+ {
209
+ code: 'TRANSACTION_RECEIVED';
210
+ transaction_hash: string;
211
+ class_hash: string;
212
+ };
213
+
214
+ <hr/>
215
+
216
+ ```
217
+
201
218
  provider.**deployContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_
202
219
 
203
- Gets the transaction trace from a tx hash.
220
+ Deploys a contract on Starknet
204
221
 
205
222
  ###### _AddTransactionResponse_
206
223