@fastnear/api 0.3.0 → 0.4.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/umd/index.js CHANGED
@@ -3640,7 +3640,7 @@ var FastNear = (() => {
3640
3640
  }
3641
3641
  }
3642
3642
  function withBlockId(params, blockId) {
3643
- return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } : !!blockId ? { ...params, block_id: blockId } : { ...params, finality: "optimistic" };
3643
+ return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } : blockId ? { ...params, block_id: blockId } : { ...params, finality: "optimistic" };
3644
3644
  }
3645
3645
  async function queryRpc(method, params) {
3646
3646
  const response = await fetch(_config.nodeUrl, {
@@ -3736,10 +3736,10 @@ var FastNear = (() => {
3736
3736
  });
3737
3737
  }
3738
3738
  if (typeof s == "string") {
3739
- let match = s.match(/([0-9.,_]+)\s*([a-zA-Z]+)?/);
3739
+ const match = s.match(/([0-9.,_]+)\s*([a-zA-Z]+)?/);
3740
3740
  if (match) {
3741
- let amount = match[1].replace(/[_,]/g, "");
3742
- let unitPart = match[2];
3741
+ const amount = match[1].replace(/[_,]/g, "");
3742
+ const unitPart = match[2];
3743
3743
  if (unitPart) {
3744
3744
  switch (unitPart.toLowerCase()) {
3745
3745
  case "near":
@@ -3749,9 +3749,10 @@ var FastNear = (() => {
3749
3749
  case "ggas":
3750
3750
  return big_default(amount).mul(big_default(10).pow(9)).toFixed(0);
3751
3751
  case "gas":
3752
+ case "yoctonear":
3752
3753
  return big_default(amount).toFixed(0);
3753
3754
  default:
3754
- throw new Error(`Unknown unit: ${unit}`);
3755
+ throw new Error(`Unknown unit: ${unitPart}`);
3755
3756
  }
3756
3757
  } else {
3757
3758
  return big_default(amount).toFixed(0);
@@ -3802,7 +3803,13 @@ var FastNear = (() => {
3802
3803
  return "SignedIn";
3803
3804
  },
3804
3805
  // Query Methods
3805
- async view({ contractId, methodName, args, argsBase64, blockId }) {
3806
+ async view({
3807
+ contractId,
3808
+ methodName,
3809
+ args,
3810
+ argsBase64,
3811
+ blockId
3812
+ }) {
3806
3813
  const encodedArgs = argsBase64 || (args ? toBase64(JSON.stringify(args)) : "");
3807
3814
  const result = await queryRpc(
3808
3815
  "query",
@@ -3818,7 +3825,10 @@ var FastNear = (() => {
3818
3825
  );
3819
3826
  return parseJsonFromBytes(result.result);
3820
3827
  },
3821
- async account({ accountId, blockId }) {
3828
+ async account({
3829
+ accountId,
3830
+ blockId
3831
+ }) {
3822
3832
  return queryRpc(
3823
3833
  "query",
3824
3834
  withBlockId(
@@ -3833,7 +3843,11 @@ var FastNear = (() => {
3833
3843
  async block({ blockId }) {
3834
3844
  return queryRpc("block", withBlockId({}, blockId));
3835
3845
  },
3836
- async accessKey({ accountId, publicKey, blockId }) {
3846
+ async accessKey({
3847
+ accountId,
3848
+ publicKey,
3849
+ blockId
3850
+ }) {
3837
3851
  return queryRpc(
3838
3852
  "query",
3839
3853
  withBlockId(
@@ -3853,7 +3867,11 @@ var FastNear = (() => {
3853
3867
  return [..._txHistory];
3854
3868
  },
3855
3869
  // Transaction Methods
3856
- async sendTx({ receiverId, actions, waitUntil }) {
3870
+ async sendTx({
3871
+ receiverId,
3872
+ actions,
3873
+ waitUntil
3874
+ }) {
3857
3875
  const signerId = _state.accountId;
3858
3876
  if (!signerId) {
3859
3877
  throw new Error("Not signed in");
@@ -3873,7 +3891,7 @@ var FastNear = (() => {
3873
3891
  tx: jsonTransaction2,
3874
3892
  finalState: false
3875
3893
  });
3876
- const url = new URL(window.location.href);
3894
+ const url = new URL(typeof window !== "undefined" ? window.location.href : "");
3877
3895
  url.searchParams.set("txIds", txId);
3878
3896
  _adapter.sendTransactions({
3879
3897
  transactions: [jsonTransaction2],
@@ -3882,16 +3900,18 @@ var FastNear = (() => {
3882
3900
  console.log("Transaction result:", result);
3883
3901
  if (result.url) {
3884
3902
  console.log("Redirecting to wallet:", result.url);
3885
- setTimeout(() => {
3886
- window.location.href = result.url;
3887
- }, 100);
3903
+ if (typeof window !== "undefined") {
3904
+ setTimeout(() => {
3905
+ window.location.href = result.url;
3906
+ }, 100);
3907
+ }
3888
3908
  } else if (result.outcomes) {
3889
- result.outcomes.forEach((result2) => {
3909
+ result.outcomes.forEach((r) => {
3890
3910
  updateTxHistory({
3891
3911
  txId,
3892
3912
  status: "Executed",
3893
- result: result2,
3894
- txHash: result2.transaction.hash,
3913
+ result: r,
3914
+ txHash: r.transaction.hash,
3895
3915
  finalState: true
3896
3916
  });
3897
3917
  });
@@ -3919,10 +3939,11 @@ var FastNear = (() => {
3919
3939
  });
3920
3940
  return txId;
3921
3941
  }
3922
- const toDoPromises = {};
3923
3942
  let nonce = lsGet("nonce");
3943
+ let block = lsGet("block");
3944
+ const toDoPromises = {};
3924
3945
  if (nonce === null || nonce === void 0) {
3925
- toDoPromises.nonce = this.accessKey({
3946
+ toDoPromises.nonce = api.accessKey({
3926
3947
  accountId: signerId,
3927
3948
  publicKey
3928
3949
  }).then((accessKey) => {
@@ -3933,30 +3954,30 @@ var FastNear = (() => {
3933
3954
  return accessKey.nonce;
3934
3955
  });
3935
3956
  }
3936
- let block = lsGet("block");
3937
- if (!block || parseFloat(block.header.timestamp_nanosec) / 1e6 + MaxBlockDelayMs < Date.now()) {
3938
- toDoPromises.block = this.block({ blockId: "final" }).then((block2) => {
3939
- block2 = {
3957
+ if (!block || !block.header || parseFloat(block.header.timestamp_nanosec) / 1e6 + MaxBlockDelayMs < Date.now()) {
3958
+ toDoPromises.block = api.block({ blockId: "final" }).then((b) => {
3959
+ const newBlock = {
3940
3960
  header: {
3941
- prev_hash: block2.header.prev_hash,
3942
- timestamp_nanosec: block2.header.timestamp_nanosec
3961
+ prev_hash: b.header.prev_hash,
3962
+ timestamp_nanosec: b.header.timestamp_nanosec
3943
3963
  }
3944
3964
  };
3945
- lsSet("block", block2);
3946
- return block2;
3965
+ lsSet("block", newBlock);
3966
+ return newBlock;
3947
3967
  });
3948
3968
  }
3949
3969
  if (Object.keys(toDoPromises).length > 0) {
3950
- let results = await Promise.all(Object.values(toDoPromises));
3951
- for (let i = 0; i < results.length; i++) {
3952
- if (Object.keys(toDoPromises)[i] === "nonce") {
3953
- nonce = results[i];
3954
- } else if (Object.keys(toDoPromises)[i] === "block") {
3955
- block = results[i];
3970
+ const results = await Promise.all(Object.values(toDoPromises));
3971
+ const keys = Object.keys(toDoPromises);
3972
+ results.forEach((res, i) => {
3973
+ if (keys[i] === "nonce") {
3974
+ nonce = res;
3975
+ } else if (keys[i] === "block") {
3976
+ block = res;
3956
3977
  }
3957
- }
3978
+ });
3958
3979
  }
3959
- const newNonce = nonce + 1;
3980
+ const newNonce = (nonce ?? 0) + 1;
3960
3981
  lsSet("nonce", newNonce);
3961
3982
  const blockHash = block.header.prev_hash;
3962
3983
  const jsonTransaction = {
@@ -3971,11 +3992,8 @@ var FastNear = (() => {
3971
3992
  const transaction = serializeTransaction(jsonTransaction);
3972
3993
  const txHash = binary_to_base58_default(sha256(transaction));
3973
3994
  const signature = signHash(txHash, privateKey);
3974
- const singedTransaction = serializeSignedTransaction(
3975
- jsonTransaction,
3976
- signature
3977
- );
3978
- const signedTxBase64 = toBase64(singedTransaction);
3995
+ const signedTransaction = serializeSignedTransaction(jsonTransaction, signature);
3996
+ const signedTxBase64 = toBase64(signedTransaction);
3979
3997
  updateTxHistory({
3980
3998
  status: "Pending",
3981
3999
  txId,
@@ -4008,9 +4026,11 @@ var FastNear = (() => {
4008
4026
  }
4009
4027
  if (result.url) {
4010
4028
  console.log("Redirecting to wallet:", result.url);
4011
- setTimeout(() => {
4012
- window.location.href = result.url;
4013
- }, 100);
4029
+ if (typeof window !== "undefined") {
4030
+ setTimeout(() => {
4031
+ window.location.href = result.url;
4032
+ }, 100);
4033
+ }
4014
4034
  } else if (result.accountId) {
4015
4035
  updateState({
4016
4036
  accountId: result.accountId
@@ -4043,7 +4063,13 @@ var FastNear = (() => {
4043
4063
  },
4044
4064
  // Action Helpers
4045
4065
  actions: {
4046
- functionCall: ({ methodName, gas, deposit, args, argsBase64 }) => ({
4066
+ functionCall: ({
4067
+ methodName,
4068
+ gas,
4069
+ deposit,
4070
+ args,
4071
+ argsBase64
4072
+ }) => ({
4047
4073
  type: "FunctionCall",
4048
4074
  methodName,
4049
4075
  args,
@@ -4104,66 +4130,68 @@ var FastNear = (() => {
4104
4130
  }
4105
4131
  };
4106
4132
  try {
4107
- const url = new URL(window.location.href);
4108
- const accountId = url.searchParams.get("account_id");
4109
- const publicKey = url.searchParams.get("public_key");
4110
- const errorCode = url.searchParams.get("errorCode");
4111
- const errorMessage = url.searchParams.get("errorMessage");
4112
- const transactionHashes = url.searchParams.get("transactionHashes");
4113
- const txIds = url.searchParams.get("txIds");
4114
- if (errorCode || errorMessage) {
4115
- console.warn(new Error(`Wallet error: ${errorCode} ${errorMessage}`));
4116
- }
4117
- if (accountId && publicKey) {
4118
- if (publicKey === _state.publicKey) {
4119
- updateState({
4120
- accountId
4121
- });
4122
- } else {
4123
- console.error(
4124
- new Error("Public key mismatch from wallet redirect"),
4125
- publicKey,
4126
- _state.publicKey
4127
- );
4133
+ if (typeof window !== "undefined") {
4134
+ const url = new URL(window.location.href);
4135
+ const accountId = url.searchParams.get("account_id");
4136
+ const publicKey = url.searchParams.get("public_key");
4137
+ const errorCode = url.searchParams.get("errorCode");
4138
+ const errorMessage = url.searchParams.get("errorMessage");
4139
+ const transactionHashes = url.searchParams.get("transactionHashes");
4140
+ const txIds = url.searchParams.get("txIds");
4141
+ if (errorCode || errorMessage) {
4142
+ console.warn(new Error(`Wallet error: ${errorCode} ${errorMessage}`));
4128
4143
  }
4129
- }
4130
- if (transactionHashes || txIds) {
4131
- const txHashes = transactionHashes ? transactionHashes.split(",") : [];
4132
- const txIdsArray = txIds ? txIds.split(",") : [];
4133
- if (txIdsArray.length > txHashes.length) {
4134
- txIdsArray.forEach((txId, i) => {
4135
- updateTxHistory({
4136
- txId,
4137
- status: "RejectedByUser",
4138
- finalState: true
4144
+ if (accountId && publicKey) {
4145
+ if (publicKey === _state.publicKey) {
4146
+ updateState({
4147
+ accountId
4139
4148
  });
4140
- });
4141
- } else if (txIdsArray.length === txHashes.length) {
4142
- txIdsArray.forEach((txId, i) => {
4143
- updateTxHistory({
4144
- txId,
4145
- status: "PendingGotTxHash",
4146
- txHash: txHashes[i],
4147
- finalState: false
4149
+ } else {
4150
+ console.error(
4151
+ new Error("Public key mismatch from wallet redirect"),
4152
+ publicKey,
4153
+ _state.publicKey
4154
+ );
4155
+ }
4156
+ }
4157
+ if (transactionHashes || txIds) {
4158
+ const txHashes = transactionHashes ? transactionHashes.split(",") : [];
4159
+ const txIdsArray = txIds ? txIds.split(",") : [];
4160
+ if (txIdsArray.length > txHashes.length) {
4161
+ txIdsArray.forEach((txId, i) => {
4162
+ updateTxHistory({
4163
+ txId,
4164
+ status: "RejectedByUser",
4165
+ finalState: true
4166
+ });
4148
4167
  });
4149
- afterTxSent(txId);
4150
- });
4151
- } else {
4152
- console.error(
4153
- new Error("Transaction hash mismatch from wallet redirect"),
4154
- txIdsArray,
4155
- txHashes
4156
- );
4168
+ } else if (txIdsArray.length === txHashes.length) {
4169
+ txIdsArray.forEach((txId, i) => {
4170
+ updateTxHistory({
4171
+ txId,
4172
+ status: "PendingGotTxHash",
4173
+ txHash: txHashes[i],
4174
+ finalState: false
4175
+ });
4176
+ afterTxSent(txId);
4177
+ });
4178
+ } else {
4179
+ console.error(
4180
+ new Error("Transaction hash mismatch from wallet redirect"),
4181
+ txIdsArray,
4182
+ txHashes
4183
+ );
4184
+ }
4157
4185
  }
4186
+ url.searchParams.delete("account_id");
4187
+ url.searchParams.delete("public_key");
4188
+ url.searchParams.delete("errorCode");
4189
+ url.searchParams.delete("errorMessage");
4190
+ url.searchParams.delete("all_keys");
4191
+ url.searchParams.delete("transactionHashes");
4192
+ url.searchParams.delete("txIds");
4193
+ window.history.replaceState({}, "", url.toString());
4158
4194
  }
4159
- url.searchParams.delete("account_id");
4160
- url.searchParams.delete("public_key");
4161
- url.searchParams.delete("errorCode");
4162
- url.searchParams.delete("errorMessage");
4163
- url.searchParams.delete("all_keys");
4164
- url.searchParams.delete("transactionHashes");
4165
- url.searchParams.delete("txIds");
4166
- window.history.replaceState({}, "", url.toString());
4167
4195
  } catch (e) {
4168
4196
  console.error("Error handling wallet redirect:", e);
4169
4197
  }