starknet 6.4.2 → 6.6.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/dist/index.mjs CHANGED
@@ -21,7 +21,8 @@ __export(constants_exports, {
21
21
  TRANSACTION_VERSION: () => ETransactionVersion4,
22
22
  TransactionHashPrefix: () => TransactionHashPrefix,
23
23
  UDC: () => UDC,
24
- ZERO: () => ZERO
24
+ ZERO: () => ZERO,
25
+ feeMarginPercentage: () => feeMarginPercentage
25
26
  });
26
27
 
27
28
  // src/types/api/index.ts
@@ -348,11 +349,17 @@ var TransactionHashPrefix = /* @__PURE__ */ ((TransactionHashPrefix2) => {
348
349
  TransactionHashPrefix2["L1_HANDLER"] = "0x6c315f68616e646c6572";
349
350
  return TransactionHashPrefix2;
350
351
  })(TransactionHashPrefix || {});
352
+ var feeMarginPercentage = /* @__PURE__ */ ((feeMarginPercentage2) => {
353
+ feeMarginPercentage2[feeMarginPercentage2["L1_BOUND_MAX_AMOUNT"] = 50] = "L1_BOUND_MAX_AMOUNT";
354
+ feeMarginPercentage2[feeMarginPercentage2["L1_BOUND_MAX_PRICE_PER_UNIT"] = 50] = "L1_BOUND_MAX_PRICE_PER_UNIT";
355
+ feeMarginPercentage2[feeMarginPercentage2["MAX_FEE"] = 50] = "MAX_FEE";
356
+ return feeMarginPercentage2;
357
+ })(feeMarginPercentage || {});
351
358
  var UDC = {
352
359
  ADDRESS: "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf",
353
360
  ENTRYPOINT: "deployContract"
354
361
  };
355
- var RPC_DEFAULT_VERSION = "v0_6";
362
+ var RPC_DEFAULT_VERSION = "v0_7";
356
363
  var RPC_NODES = {
357
364
  SN_GOERLI: [
358
365
  `https://starknet-testnet.public.blastapi.io/rpc/${RPC_DEFAULT_VERSION}`,
@@ -2837,10 +2844,10 @@ function signatureToDecimalArray(sig) {
2837
2844
  function signatureToHexArray(sig) {
2838
2845
  return bigNumberishArrayToHexadecimalStringArray(formatSignature(sig));
2839
2846
  }
2840
- function estimatedFeeToMaxFee(estimatedFee, overhead = 0.5) {
2841
- return addPercent(estimatedFee, overhead * 100);
2847
+ function estimatedFeeToMaxFee(estimatedFee, overhead = 50 /* MAX_FEE */) {
2848
+ return addPercent(estimatedFee, overhead);
2842
2849
  }
2843
- function estimateFeeToBounds(estimate, amountOverhead = 10, priceOverhead = 50) {
2850
+ function estimateFeeToBounds(estimate, amountOverhead = 50 /* L1_BOUND_MAX_AMOUNT */, priceOverhead = 50 /* L1_BOUND_MAX_PRICE_PER_UNIT */) {
2844
2851
  if (typeof estimate === "bigint") {
2845
2852
  return {
2846
2853
  l2_gas: { max_amount: "0x0", max_price_per_unit: "0x0" },
@@ -2850,7 +2857,7 @@ function estimateFeeToBounds(estimate, amountOverhead = 10, priceOverhead = 50)
2850
2857
  if (typeof estimate.gas_consumed === "undefined" || typeof estimate.gas_price === "undefined") {
2851
2858
  throw Error("estimateFeeToBounds: estimate is undefined");
2852
2859
  }
2853
- const maxUnits = toHex(addPercent(estimate.gas_consumed, amountOverhead));
2860
+ const maxUnits = estimate.data_gas_consumed !== void 0 && estimate.data_gas_price !== void 0 ? toHex(addPercent(BigInt(estimate.overall_fee) / BigInt(estimate.gas_price), amountOverhead)) : toHex(addPercent(estimate.gas_consumed, amountOverhead));
2854
2861
  const maxUnitPrice = toHex(addPercent(estimate.gas_price, priceOverhead));
2855
2862
  return {
2856
2863
  l2_gas: { max_amount: "0x0", max_price_per_unit: "0x0" },
@@ -3076,6 +3083,7 @@ function isPendingStateUpdate(response) {
3076
3083
  // src/utils/transaction.ts
3077
3084
  var transaction_exports = {};
3078
3085
  __export(transaction_exports, {
3086
+ buildUDCCall: () => buildUDCCall,
3079
3087
  fromCallsToExecuteCalldata: () => fromCallsToExecuteCalldata,
3080
3088
  fromCallsToExecuteCalldataWithNonce: () => fromCallsToExecuteCalldataWithNonce,
3081
3089
  fromCallsToExecuteCalldata_cairo1: () => fromCallsToExecuteCalldata_cairo1,
@@ -3133,6 +3141,41 @@ var getExecuteCalldata = (calls, cairoVersion = "0") => {
3133
3141
  }
3134
3142
  return fromCallsToExecuteCalldata(calls);
3135
3143
  };
3144
+ function buildUDCCall(payload, address) {
3145
+ const params = [].concat(payload).map((it) => {
3146
+ const {
3147
+ classHash,
3148
+ salt,
3149
+ unique = true,
3150
+ constructorCalldata = []
3151
+ } = it;
3152
+ const compiledConstructorCallData = CallData.compile(constructorCalldata);
3153
+ const deploySalt = salt ?? randomAddress();
3154
+ return {
3155
+ call: {
3156
+ contractAddress: UDC.ADDRESS,
3157
+ entrypoint: UDC.ENTRYPOINT,
3158
+ calldata: [
3159
+ classHash,
3160
+ deploySalt,
3161
+ toCairoBool(unique),
3162
+ compiledConstructorCallData.length,
3163
+ ...compiledConstructorCallData
3164
+ ]
3165
+ },
3166
+ address: calculateContractAddressFromHash(
3167
+ unique ? starkCurve.pedersen(address, deploySalt) : deploySalt,
3168
+ classHash,
3169
+ compiledConstructorCallData,
3170
+ unique ? UDC.ADDRESS : 0
3171
+ )
3172
+ };
3173
+ });
3174
+ return {
3175
+ calls: params.map((it) => it.call),
3176
+ addresses: params.map((it) => it.address)
3177
+ };
3178
+ }
3136
3179
  function getVersionsByType(versionType) {
3137
3180
  return versionType === "fee" ? {
3138
3181
  v1: "0x100000000000000000000000000000001" /* F1 */,
@@ -3174,6 +3217,9 @@ var RpcChannel = class {
3174
3217
  this.waitMode = waitMode || false;
3175
3218
  this.requestId = 0;
3176
3219
  }
3220
+ setChainId(chainId) {
3221
+ this.chainId = chainId;
3222
+ }
3177
3223
  fetch(method, params, id = 0) {
3178
3224
  const rpcRequestBody = {
3179
3225
  id,
@@ -3704,6 +3750,9 @@ var RpcChannel2 = class {
3704
3750
  this.waitMode = waitMode || false;
3705
3751
  this.requestId = 0;
3706
3752
  }
3753
+ setChainId(chainId) {
3754
+ this.chainId = chainId;
3755
+ }
3707
3756
  fetch(method, params, id = 0) {
3708
3757
  const rpcRequestBody = {
3709
3758
  id,
@@ -4204,6 +4253,20 @@ var RpcChannel2 = class {
4204
4253
 
4205
4254
  // src/utils/responseParser/rpc.ts
4206
4255
  var RPCResponseParser = class {
4256
+ margin;
4257
+ constructor(margin) {
4258
+ this.margin = margin;
4259
+ }
4260
+ estimatedFeeToMaxFee(estimatedFee) {
4261
+ return estimatedFeeToMaxFee(estimatedFee, this.margin?.maxFee);
4262
+ }
4263
+ estimateFeeToBounds(estimate) {
4264
+ return estimateFeeToBounds(
4265
+ estimate,
4266
+ this.margin?.l1BoundMaxAmount,
4267
+ this.margin?.l1BoundMaxPricePerUnit
4268
+ );
4269
+ }
4207
4270
  parseGetBlockResponse(res) {
4208
4271
  return { status: "PENDING", ...res };
4209
4272
  }
@@ -4226,8 +4289,8 @@ var RPCResponseParser = class {
4226
4289
  gas_consumed: toBigInt(val.gas_consumed),
4227
4290
  gas_price: toBigInt(val.gas_price),
4228
4291
  unit: val.unit,
4229
- suggestedMaxFee: estimatedFeeToMaxFee(val.overall_fee),
4230
- resourceBounds: estimateFeeToBounds(val)
4292
+ suggestedMaxFee: this.estimatedFeeToMaxFee(val.overall_fee),
4293
+ resourceBounds: this.estimateFeeToBounds(val)
4231
4294
  };
4232
4295
  }
4233
4296
  parseFeeEstimateBulkResponse(res) {
@@ -4236,16 +4299,16 @@ var RPCResponseParser = class {
4236
4299
  gas_consumed: toBigInt(val.gas_consumed),
4237
4300
  gas_price: toBigInt(val.gas_price),
4238
4301
  unit: val.unit,
4239
- suggestedMaxFee: estimatedFeeToMaxFee(val.overall_fee),
4240
- resourceBounds: estimateFeeToBounds(val)
4302
+ suggestedMaxFee: this.estimatedFeeToMaxFee(val.overall_fee),
4303
+ resourceBounds: this.estimateFeeToBounds(val)
4241
4304
  }));
4242
4305
  }
4243
4306
  parseSimulateTransactionResponse(res) {
4244
4307
  return res.map((it) => {
4245
4308
  return {
4246
4309
  ...it,
4247
- suggestedMaxFee: estimatedFeeToMaxFee(BigInt(it.fee_estimation.overall_fee)),
4248
- resourceBounds: estimateFeeToBounds(it.fee_estimation)
4310
+ suggestedMaxFee: this.estimatedFeeToMaxFee(it.fee_estimation.overall_fee),
4311
+ resourceBounds: this.estimateFeeToBounds(it.fee_estimation)
4249
4312
  };
4250
4313
  });
4251
4314
  }
@@ -4259,13 +4322,15 @@ var RPCResponseParser = class {
4259
4322
 
4260
4323
  // src/provider/rpc.ts
4261
4324
  var RpcProvider = class {
4262
- responseParser = new RPCResponseParser();
4325
+ responseParser;
4263
4326
  channel;
4264
4327
  constructor(optionsOrProvider) {
4265
4328
  if (optionsOrProvider && "channel" in optionsOrProvider) {
4266
4329
  this.channel = optionsOrProvider.channel;
4330
+ this.responseParser = optionsOrProvider.responseParser;
4267
4331
  } else {
4268
4332
  this.channel = new RpcChannel2({ ...optionsOrProvider, waitMode: false });
4333
+ this.responseParser = new RPCResponseParser(optionsOrProvider?.feeMarginPercentage);
4269
4334
  }
4270
4335
  }
4271
4336
  fetch(method, params, id = 0) {
@@ -4358,7 +4423,7 @@ var RpcProvider = class {
4358
4423
  * - skipFeeCharge (default true)<br/>
4359
4424
  */
4360
4425
  async getSimulateTransaction(invocations, options) {
4361
- return this.channel.simulateTransaction(invocations, options).then(this.responseParser.parseSimulateTransactionResponse);
4426
+ return this.channel.simulateTransaction(invocations, options).then((r) => this.responseParser.parseSimulateTransactionResponse(r));
4362
4427
  }
4363
4428
  async waitForTransaction(txHash, options) {
4364
4429
  return this.channel.waitForTransaction(txHash, options);
@@ -4415,7 +4480,7 @@ var RpcProvider = class {
4415
4480
  }
4416
4481
  ],
4417
4482
  { blockIdentifier, skipValidate }
4418
- ).then(this.responseParser.parseFeeEstimateResponse);
4483
+ ).then((r) => this.responseParser.parseFeeEstimateResponse(r));
4419
4484
  }
4420
4485
  async getDeclareEstimateFee(invocation, details, blockIdentifier, skipValidate) {
4421
4486
  return this.channel.getEstimateFee(
@@ -4427,7 +4492,7 @@ var RpcProvider = class {
4427
4492
  }
4428
4493
  ],
4429
4494
  { blockIdentifier, skipValidate }
4430
- ).then(this.responseParser.parseFeeEstimateResponse);
4495
+ ).then((r) => this.responseParser.parseFeeEstimateResponse(r));
4431
4496
  }
4432
4497
  async getDeployAccountEstimateFee(invocation, details, blockIdentifier, skipValidate) {
4433
4498
  return this.channel.getEstimateFee(
@@ -4439,10 +4504,10 @@ var RpcProvider = class {
4439
4504
  }
4440
4505
  ],
4441
4506
  { blockIdentifier, skipValidate }
4442
- ).then(this.responseParser.parseFeeEstimateResponse);
4507
+ ).then((r) => this.responseParser.parseFeeEstimateResponse(r));
4443
4508
  }
4444
4509
  async getEstimateFeeBulk(invocations, options) {
4445
- return this.channel.getEstimateFee(invocations, options).then(this.responseParser.parseFeeEstimateBulkResponse);
4510
+ return this.channel.getEstimateFee(invocations, options).then((r) => this.responseParser.parseFeeEstimateBulkResponse(r));
4446
4511
  }
4447
4512
  async invokeFunction(functionInvocation, details) {
4448
4513
  return this.channel.invoke(functionInvocation, details);
@@ -5783,37 +5848,7 @@ var Account = class extends RpcProvider2 {
5783
5848
  return this.declareContract(declareContractTransaction, declareDetails);
5784
5849
  }
5785
5850
  async deploy(payload, details = {}) {
5786
- const params = [].concat(payload).map((it) => {
5787
- const {
5788
- classHash,
5789
- salt,
5790
- unique = true,
5791
- constructorCalldata = []
5792
- } = it;
5793
- const compiledConstructorCallData = CallData.compile(constructorCalldata);
5794
- const deploySalt = salt ?? randomAddress();
5795
- return {
5796
- call: {
5797
- contractAddress: UDC.ADDRESS,
5798
- entrypoint: UDC.ENTRYPOINT,
5799
- calldata: [
5800
- classHash,
5801
- deploySalt,
5802
- toCairoBool(unique),
5803
- compiledConstructorCallData.length,
5804
- ...compiledConstructorCallData
5805
- ]
5806
- },
5807
- address: calculateContractAddressFromHash(
5808
- unique ? starkCurve.pedersen(this.address, deploySalt) : deploySalt,
5809
- classHash,
5810
- compiledConstructorCallData,
5811
- unique ? UDC.ADDRESS : 0
5812
- )
5813
- };
5814
- });
5815
- const calls = params.map((it) => it.call);
5816
- const addresses = params.map((it) => it.address);
5851
+ const { calls, addresses } = buildUDCCall(payload, this.address);
5817
5852
  const invokeResponse = await this.execute(calls, void 0, details);
5818
5853
  return {
5819
5854
  ...invokeResponse,
@@ -6128,6 +6163,186 @@ var Account = class extends RpcProvider2 {
6128
6163
  var AccountInterface = class extends ProviderInterface {
6129
6164
  };
6130
6165
 
6166
+ // src/wallet/connect.ts
6167
+ function requestAccounts(swo, silentMode = false) {
6168
+ return swo.request({
6169
+ type: "wallet_requestAccounts",
6170
+ params: {
6171
+ silentMode
6172
+ }
6173
+ });
6174
+ }
6175
+ function getPermissions(swo) {
6176
+ return swo.request({ type: "wallet_getPermissions" });
6177
+ }
6178
+ function watchAsset(swo, asset) {
6179
+ return swo.request({
6180
+ type: "wallet_watchAsset",
6181
+ params: asset
6182
+ });
6183
+ }
6184
+ function addStarknetChain(swo, chain) {
6185
+ return swo.request({
6186
+ type: "wallet_addStarknetChain",
6187
+ params: chain
6188
+ });
6189
+ }
6190
+ function switchStarknetChain(swo, chainId) {
6191
+ return swo.request({
6192
+ type: "wallet_switchStarknetChain",
6193
+ params: {
6194
+ chainId
6195
+ }
6196
+ });
6197
+ }
6198
+ function requestChainId(swo) {
6199
+ return swo.request({ type: "wallet_requestChainId" });
6200
+ }
6201
+ function deploymentData(swo) {
6202
+ return swo.request({ type: "wallet_deploymentData" });
6203
+ }
6204
+ function addInvokeTransaction(swo, params) {
6205
+ return swo.request({
6206
+ type: "starknet_addInvokeTransaction",
6207
+ params
6208
+ });
6209
+ }
6210
+ function addDeclareTransaction(swo, params) {
6211
+ return swo.request({
6212
+ type: "starknet_addDeclareTransaction",
6213
+ params
6214
+ });
6215
+ }
6216
+ function addDeployAccountTransaction(swo, params) {
6217
+ return swo.request({
6218
+ type: "starknet_addDeployAccountTransaction",
6219
+ params
6220
+ });
6221
+ }
6222
+ function signMessage(swo, typedData) {
6223
+ return swo.request({
6224
+ type: "starknet_signTypedData",
6225
+ params: typedData
6226
+ });
6227
+ }
6228
+ function supportedSpecs(swo) {
6229
+ return swo.request({ type: "starknet_supportedSpecs" });
6230
+ }
6231
+ function onAccountChange(swo, callback) {
6232
+ swo.on("accountsChanged", callback);
6233
+ }
6234
+ function onNetworkChanged(swo, callback) {
6235
+ swo.on("networkChanged", callback);
6236
+ }
6237
+
6238
+ // src/wallet/account.ts
6239
+ var WalletAccount = class extends Account {
6240
+ address = "";
6241
+ walletProvider;
6242
+ constructor(providerOrOptions, walletProvider, cairoVersion) {
6243
+ super(providerOrOptions, "", "", cairoVersion);
6244
+ this.walletProvider = walletProvider;
6245
+ this.walletProvider.on("accountsChanged", (res) => {
6246
+ if (!res)
6247
+ return;
6248
+ this.address = res[0].toLowerCase();
6249
+ });
6250
+ this.walletProvider.on("networkChanged", (res) => {
6251
+ if (!res)
6252
+ return;
6253
+ this.channel.setChainId(res);
6254
+ });
6255
+ walletProvider.request({
6256
+ type: "wallet_requestAccounts",
6257
+ params: {
6258
+ silentMode: false
6259
+ }
6260
+ }).then((res) => {
6261
+ this.address = res[0].toLowerCase();
6262
+ });
6263
+ }
6264
+ /**
6265
+ * WALLET EVENTS
6266
+ */
6267
+ onAccountChange(callback) {
6268
+ onAccountChange(this.walletProvider, callback);
6269
+ }
6270
+ onNetworkChanged(callback) {
6271
+ onNetworkChanged(this.walletProvider, callback);
6272
+ }
6273
+ /**
6274
+ * WALLET SPECIFIC METHODS
6275
+ */
6276
+ requestAccounts(silentMode = false) {
6277
+ return requestAccounts(this.walletProvider, silentMode);
6278
+ }
6279
+ getPermissions() {
6280
+ return getPermissions(this.walletProvider);
6281
+ }
6282
+ switchStarknetChain(chainId) {
6283
+ return switchStarknetChain(this.walletProvider, chainId);
6284
+ }
6285
+ watchAsset(asset) {
6286
+ return watchAsset(this.walletProvider, asset);
6287
+ }
6288
+ addStarknetChain(chain) {
6289
+ return addStarknetChain(this.walletProvider, chain);
6290
+ }
6291
+ /**
6292
+ * ACCOUNT METHODS
6293
+ */
6294
+ execute(calls) {
6295
+ const txCalls = [].concat(calls).map((it) => {
6296
+ const { contractAddress, entrypoint, calldata } = it;
6297
+ return {
6298
+ contract_address: contractAddress,
6299
+ entrypoint,
6300
+ calldata
6301
+ };
6302
+ });
6303
+ const params = {
6304
+ calls: txCalls
6305
+ };
6306
+ return addInvokeTransaction(this.walletProvider, params);
6307
+ }
6308
+ declare(payload) {
6309
+ const declareContractPayload = extractContractHashes(payload);
6310
+ const pContract = payload.contract;
6311
+ const cairo1Contract = {
6312
+ ...pContract,
6313
+ abi: stringify2(pContract.abi)
6314
+ };
6315
+ if (!declareContractPayload.compiledClassHash) {
6316
+ throw Error("compiledClassHash is required");
6317
+ }
6318
+ const params = {
6319
+ compiled_class_hash: declareContractPayload.compiledClassHash,
6320
+ contract_class: cairo1Contract
6321
+ };
6322
+ return addDeclareTransaction(this.walletProvider, params);
6323
+ }
6324
+ async deploy(payload) {
6325
+ const { calls, addresses } = buildUDCCall(payload, this.address);
6326
+ const invokeResponse = await this.execute(calls);
6327
+ return {
6328
+ ...invokeResponse,
6329
+ contract_address: addresses
6330
+ };
6331
+ }
6332
+ deployAccount(payload) {
6333
+ const params = {
6334
+ contract_address_salt: payload.addressSalt?.toString() || "0",
6335
+ constructor_calldata: payload.constructorCalldata ? CallData.compile(payload.constructorCalldata) : [],
6336
+ class_hash: payload.classHash
6337
+ };
6338
+ return addDeployAccountTransaction(this.walletProvider, params);
6339
+ }
6340
+ signMessage(typedData) {
6341
+ return signMessage(this.walletProvider, typedData);
6342
+ }
6343
+ // TODO: MISSING ESTIMATES
6344
+ };
6345
+
6131
6346
  // src/utils/events/index.ts
6132
6347
  var events_exports = {};
6133
6348
  __export(events_exports, {
@@ -6638,13 +6853,19 @@ export {
6638
6853
  TypedDataRevision,
6639
6854
  Uint,
6640
6855
  ValidateType,
6856
+ WalletAccount,
6641
6857
  addAddressPadding,
6858
+ addDeclareTransaction,
6859
+ addDeployAccountTransaction,
6860
+ addInvokeTransaction,
6861
+ addStarknetChain,
6642
6862
  buildUrl,
6643
6863
  byteArray_exports as byteArray,
6644
6864
  cairo_exports as cairo,
6645
6865
  constants_exports as constants,
6646
6866
  contractClassResponseToLegacyCompiledContract,
6647
6867
  defaultProvider,
6868
+ deploymentData,
6648
6869
  ec_exports as ec,
6649
6870
  encode_exports as encode,
6650
6871
  eth_exports as eth,
@@ -6654,6 +6875,7 @@ export {
6654
6875
  fixStack,
6655
6876
  getCalldata,
6656
6877
  getChecksumAddress,
6878
+ getPermissions,
6657
6879
  hash_exports as hash,
6658
6880
  isSierra,
6659
6881
  isUrl,
@@ -6661,13 +6883,20 @@ export {
6661
6883
  merkle_exports as merkle,
6662
6884
  num_exports as num,
6663
6885
  number,
6886
+ onAccountChange,
6887
+ onNetworkChanged,
6664
6888
  parseUDCEvent,
6665
6889
  provider_exports as provider,
6890
+ requestAccounts,
6891
+ requestChainId,
6666
6892
  selector_exports as selector,
6667
6893
  shortString_exports as shortString,
6894
+ signMessage,
6668
6895
  splitArgsAndOptions,
6669
6896
  stark_exports as stark,
6670
6897
  starknetId_exports as starknetId,
6898
+ supportedSpecs,
6899
+ switchStarknetChain,
6671
6900
  transaction_exports as transaction,
6672
6901
  typedData_exports as typedData,
6673
6902
  types_exports as types,
@@ -6675,6 +6904,7 @@ export {
6675
6904
  v2_exports as v2hash,
6676
6905
  v3_exports as v3hash,
6677
6906
  validateAndParseAddress,
6678
- validateChecksumAddress
6907
+ validateChecksumAddress,
6908
+ watchAsset
6679
6909
  };
6680
6910
  //# sourceMappingURL=index.mjs.map