@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/cjs/index.esm.js +126 -98
- package/dist/cjs/index.esm.js.map +3 -3
- package/dist/cjs/index.js +126 -98
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/near.js +126 -98
- package/dist/cjs/near.js.map +3 -3
- package/dist/esm/{chunk-B2HMQPYI.js → chunk-OR3WITSY.js} +127 -99
- package/dist/esm/chunk-OR3WITSY.js.map +7 -0
- package/dist/esm/index.esm.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/near.js +1 -1
- package/dist/umd/index.js +126 -98
- package/dist/umd/index.js.map +3 -3
- package/package.json +6 -4
- package/dist/esm/chunk-B2HMQPYI.js.map +0 -7
package/dist/cjs/near.js
CHANGED
|
@@ -655,7 +655,7 @@ function parseJsonFromBytes(bytes) {
|
|
|
655
655
|
}
|
|
656
656
|
}
|
|
657
657
|
function withBlockId(params, blockId) {
|
|
658
|
-
return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } :
|
|
658
|
+
return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } : blockId ? { ...params, block_id: blockId } : { ...params, finality: "optimistic" };
|
|
659
659
|
}
|
|
660
660
|
async function queryRpc(method, params) {
|
|
661
661
|
const response = await fetch(_config.nodeUrl, {
|
|
@@ -751,10 +751,10 @@ function convertUnit(s, ...args) {
|
|
|
751
751
|
});
|
|
752
752
|
}
|
|
753
753
|
if (typeof s == "string") {
|
|
754
|
-
|
|
754
|
+
const match = s.match(/([0-9.,_]+)\s*([a-zA-Z]+)?/);
|
|
755
755
|
if (match) {
|
|
756
|
-
|
|
757
|
-
|
|
756
|
+
const amount = match[1].replace(/[_,]/g, "");
|
|
757
|
+
const unitPart = match[2];
|
|
758
758
|
if (unitPart) {
|
|
759
759
|
switch (unitPart.toLowerCase()) {
|
|
760
760
|
case "near":
|
|
@@ -764,9 +764,10 @@ function convertUnit(s, ...args) {
|
|
|
764
764
|
case "ggas":
|
|
765
765
|
return (0, import_big2.default)(amount).mul((0, import_big2.default)(10).pow(9)).toFixed(0);
|
|
766
766
|
case "gas":
|
|
767
|
+
case "yoctonear":
|
|
767
768
|
return (0, import_big2.default)(amount).toFixed(0);
|
|
768
769
|
default:
|
|
769
|
-
throw new Error(`Unknown unit: ${
|
|
770
|
+
throw new Error(`Unknown unit: ${unitPart}`);
|
|
770
771
|
}
|
|
771
772
|
} else {
|
|
772
773
|
return (0, import_big2.default)(amount).toFixed(0);
|
|
@@ -817,7 +818,13 @@ var api = {
|
|
|
817
818
|
return "SignedIn";
|
|
818
819
|
},
|
|
819
820
|
// Query Methods
|
|
820
|
-
async view({
|
|
821
|
+
async view({
|
|
822
|
+
contractId,
|
|
823
|
+
methodName,
|
|
824
|
+
args,
|
|
825
|
+
argsBase64,
|
|
826
|
+
blockId
|
|
827
|
+
}) {
|
|
821
828
|
const encodedArgs = argsBase64 || (args ? toBase64(JSON.stringify(args)) : "");
|
|
822
829
|
const result = await queryRpc(
|
|
823
830
|
"query",
|
|
@@ -833,7 +840,10 @@ var api = {
|
|
|
833
840
|
);
|
|
834
841
|
return parseJsonFromBytes(result.result);
|
|
835
842
|
},
|
|
836
|
-
async account({
|
|
843
|
+
async account({
|
|
844
|
+
accountId,
|
|
845
|
+
blockId
|
|
846
|
+
}) {
|
|
837
847
|
return queryRpc(
|
|
838
848
|
"query",
|
|
839
849
|
withBlockId(
|
|
@@ -848,7 +858,11 @@ var api = {
|
|
|
848
858
|
async block({ blockId }) {
|
|
849
859
|
return queryRpc("block", withBlockId({}, blockId));
|
|
850
860
|
},
|
|
851
|
-
async accessKey({
|
|
861
|
+
async accessKey({
|
|
862
|
+
accountId,
|
|
863
|
+
publicKey,
|
|
864
|
+
blockId
|
|
865
|
+
}) {
|
|
852
866
|
return queryRpc(
|
|
853
867
|
"query",
|
|
854
868
|
withBlockId(
|
|
@@ -868,7 +882,11 @@ var api = {
|
|
|
868
882
|
return [..._txHistory];
|
|
869
883
|
},
|
|
870
884
|
// Transaction Methods
|
|
871
|
-
async sendTx({
|
|
885
|
+
async sendTx({
|
|
886
|
+
receiverId,
|
|
887
|
+
actions,
|
|
888
|
+
waitUntil
|
|
889
|
+
}) {
|
|
872
890
|
const signerId = _state.accountId;
|
|
873
891
|
if (!signerId) {
|
|
874
892
|
throw new Error("Not signed in");
|
|
@@ -888,7 +906,7 @@ var api = {
|
|
|
888
906
|
tx: jsonTransaction2,
|
|
889
907
|
finalState: false
|
|
890
908
|
});
|
|
891
|
-
const url = new URL(window.location.href);
|
|
909
|
+
const url = new URL(typeof window !== "undefined" ? window.location.href : "");
|
|
892
910
|
url.searchParams.set("txIds", txId);
|
|
893
911
|
_adapter.sendTransactions({
|
|
894
912
|
transactions: [jsonTransaction2],
|
|
@@ -897,16 +915,18 @@ var api = {
|
|
|
897
915
|
console.log("Transaction result:", result);
|
|
898
916
|
if (result.url) {
|
|
899
917
|
console.log("Redirecting to wallet:", result.url);
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
918
|
+
if (typeof window !== "undefined") {
|
|
919
|
+
setTimeout(() => {
|
|
920
|
+
window.location.href = result.url;
|
|
921
|
+
}, 100);
|
|
922
|
+
}
|
|
903
923
|
} else if (result.outcomes) {
|
|
904
|
-
result.outcomes.forEach((
|
|
924
|
+
result.outcomes.forEach((r) => {
|
|
905
925
|
updateTxHistory({
|
|
906
926
|
txId,
|
|
907
927
|
status: "Executed",
|
|
908
|
-
result:
|
|
909
|
-
txHash:
|
|
928
|
+
result: r,
|
|
929
|
+
txHash: r.transaction.hash,
|
|
910
930
|
finalState: true
|
|
911
931
|
});
|
|
912
932
|
});
|
|
@@ -934,10 +954,11 @@ var api = {
|
|
|
934
954
|
});
|
|
935
955
|
return txId;
|
|
936
956
|
}
|
|
937
|
-
const toDoPromises = {};
|
|
938
957
|
let nonce = lsGet("nonce");
|
|
958
|
+
let block = lsGet("block");
|
|
959
|
+
const toDoPromises = {};
|
|
939
960
|
if (nonce === null || nonce === void 0) {
|
|
940
|
-
toDoPromises.nonce =
|
|
961
|
+
toDoPromises.nonce = api.accessKey({
|
|
941
962
|
accountId: signerId,
|
|
942
963
|
publicKey
|
|
943
964
|
}).then((accessKey) => {
|
|
@@ -948,30 +969,30 @@ var api = {
|
|
|
948
969
|
return accessKey.nonce;
|
|
949
970
|
});
|
|
950
971
|
}
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
block2 = {
|
|
972
|
+
if (!block || !block.header || parseFloat(block.header.timestamp_nanosec) / 1e6 + MaxBlockDelayMs < Date.now()) {
|
|
973
|
+
toDoPromises.block = api.block({ blockId: "final" }).then((b) => {
|
|
974
|
+
const newBlock = {
|
|
955
975
|
header: {
|
|
956
|
-
prev_hash:
|
|
957
|
-
timestamp_nanosec:
|
|
976
|
+
prev_hash: b.header.prev_hash,
|
|
977
|
+
timestamp_nanosec: b.header.timestamp_nanosec
|
|
958
978
|
}
|
|
959
979
|
};
|
|
960
|
-
lsSet("block",
|
|
961
|
-
return
|
|
980
|
+
lsSet("block", newBlock);
|
|
981
|
+
return newBlock;
|
|
962
982
|
});
|
|
963
983
|
}
|
|
964
984
|
if (Object.keys(toDoPromises).length > 0) {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
985
|
+
const results = await Promise.all(Object.values(toDoPromises));
|
|
986
|
+
const keys = Object.keys(toDoPromises);
|
|
987
|
+
results.forEach((res, i) => {
|
|
988
|
+
if (keys[i] === "nonce") {
|
|
989
|
+
nonce = res;
|
|
990
|
+
} else if (keys[i] === "block") {
|
|
991
|
+
block = res;
|
|
971
992
|
}
|
|
972
|
-
}
|
|
993
|
+
});
|
|
973
994
|
}
|
|
974
|
-
const newNonce = nonce + 1;
|
|
995
|
+
const newNonce = (nonce ?? 0) + 1;
|
|
975
996
|
lsSet("nonce", newNonce);
|
|
976
997
|
const blockHash = block.header.prev_hash;
|
|
977
998
|
const jsonTransaction = {
|
|
@@ -986,11 +1007,8 @@ var api = {
|
|
|
986
1007
|
const transaction = serializeTransaction(jsonTransaction);
|
|
987
1008
|
const txHash = (0, import_base58_js.binary_to_base58)(import_sha2.sha256(transaction));
|
|
988
1009
|
const signature = signHash(txHash, privateKey);
|
|
989
|
-
const
|
|
990
|
-
|
|
991
|
-
signature
|
|
992
|
-
);
|
|
993
|
-
const signedTxBase64 = toBase64(singedTransaction);
|
|
1010
|
+
const signedTransaction = serializeSignedTransaction(jsonTransaction, signature);
|
|
1011
|
+
const signedTxBase64 = toBase64(signedTransaction);
|
|
994
1012
|
updateTxHistory({
|
|
995
1013
|
status: "Pending",
|
|
996
1014
|
txId,
|
|
@@ -1023,9 +1041,11 @@ var api = {
|
|
|
1023
1041
|
}
|
|
1024
1042
|
if (result.url) {
|
|
1025
1043
|
console.log("Redirecting to wallet:", result.url);
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1044
|
+
if (typeof window !== "undefined") {
|
|
1045
|
+
setTimeout(() => {
|
|
1046
|
+
window.location.href = result.url;
|
|
1047
|
+
}, 100);
|
|
1048
|
+
}
|
|
1029
1049
|
} else if (result.accountId) {
|
|
1030
1050
|
updateState({
|
|
1031
1051
|
accountId: result.accountId
|
|
@@ -1058,7 +1078,13 @@ var api = {
|
|
|
1058
1078
|
},
|
|
1059
1079
|
// Action Helpers
|
|
1060
1080
|
actions: {
|
|
1061
|
-
functionCall: ({
|
|
1081
|
+
functionCall: ({
|
|
1082
|
+
methodName,
|
|
1083
|
+
gas,
|
|
1084
|
+
deposit,
|
|
1085
|
+
args,
|
|
1086
|
+
argsBase64
|
|
1087
|
+
}) => ({
|
|
1062
1088
|
type: "FunctionCall",
|
|
1063
1089
|
methodName,
|
|
1064
1090
|
args,
|
|
@@ -1119,66 +1145,68 @@ var api = {
|
|
|
1119
1145
|
}
|
|
1120
1146
|
};
|
|
1121
1147
|
try {
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
if (accountId && publicKey) {
|
|
1133
|
-
if (publicKey === _state.publicKey) {
|
|
1134
|
-
updateState({
|
|
1135
|
-
accountId
|
|
1136
|
-
});
|
|
1137
|
-
} else {
|
|
1138
|
-
console.error(
|
|
1139
|
-
new Error("Public key mismatch from wallet redirect"),
|
|
1140
|
-
publicKey,
|
|
1141
|
-
_state.publicKey
|
|
1142
|
-
);
|
|
1148
|
+
if (typeof window !== "undefined") {
|
|
1149
|
+
const url = new URL(window.location.href);
|
|
1150
|
+
const accountId = url.searchParams.get("account_id");
|
|
1151
|
+
const publicKey = url.searchParams.get("public_key");
|
|
1152
|
+
const errorCode = url.searchParams.get("errorCode");
|
|
1153
|
+
const errorMessage = url.searchParams.get("errorMessage");
|
|
1154
|
+
const transactionHashes = url.searchParams.get("transactionHashes");
|
|
1155
|
+
const txIds = url.searchParams.get("txIds");
|
|
1156
|
+
if (errorCode || errorMessage) {
|
|
1157
|
+
console.warn(new Error(`Wallet error: ${errorCode} ${errorMessage}`));
|
|
1143
1158
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
if (txIdsArray.length > txHashes.length) {
|
|
1149
|
-
txIdsArray.forEach((txId, i) => {
|
|
1150
|
-
updateTxHistory({
|
|
1151
|
-
txId,
|
|
1152
|
-
status: "RejectedByUser",
|
|
1153
|
-
finalState: true
|
|
1159
|
+
if (accountId && publicKey) {
|
|
1160
|
+
if (publicKey === _state.publicKey) {
|
|
1161
|
+
updateState({
|
|
1162
|
+
accountId
|
|
1154
1163
|
});
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1164
|
+
} else {
|
|
1165
|
+
console.error(
|
|
1166
|
+
new Error("Public key mismatch from wallet redirect"),
|
|
1167
|
+
publicKey,
|
|
1168
|
+
_state.publicKey
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
if (transactionHashes || txIds) {
|
|
1173
|
+
const txHashes = transactionHashes ? transactionHashes.split(",") : [];
|
|
1174
|
+
const txIdsArray = txIds ? txIds.split(",") : [];
|
|
1175
|
+
if (txIdsArray.length > txHashes.length) {
|
|
1176
|
+
txIdsArray.forEach((txId, i) => {
|
|
1177
|
+
updateTxHistory({
|
|
1178
|
+
txId,
|
|
1179
|
+
status: "RejectedByUser",
|
|
1180
|
+
finalState: true
|
|
1181
|
+
});
|
|
1163
1182
|
});
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1183
|
+
} else if (txIdsArray.length === txHashes.length) {
|
|
1184
|
+
txIdsArray.forEach((txId, i) => {
|
|
1185
|
+
updateTxHistory({
|
|
1186
|
+
txId,
|
|
1187
|
+
status: "PendingGotTxHash",
|
|
1188
|
+
txHash: txHashes[i],
|
|
1189
|
+
finalState: false
|
|
1190
|
+
});
|
|
1191
|
+
afterTxSent(txId);
|
|
1192
|
+
});
|
|
1193
|
+
} else {
|
|
1194
|
+
console.error(
|
|
1195
|
+
new Error("Transaction hash mismatch from wallet redirect"),
|
|
1196
|
+
txIdsArray,
|
|
1197
|
+
txHashes
|
|
1198
|
+
);
|
|
1199
|
+
}
|
|
1172
1200
|
}
|
|
1201
|
+
url.searchParams.delete("account_id");
|
|
1202
|
+
url.searchParams.delete("public_key");
|
|
1203
|
+
url.searchParams.delete("errorCode");
|
|
1204
|
+
url.searchParams.delete("errorMessage");
|
|
1205
|
+
url.searchParams.delete("all_keys");
|
|
1206
|
+
url.searchParams.delete("transactionHashes");
|
|
1207
|
+
url.searchParams.delete("txIds");
|
|
1208
|
+
window.history.replaceState({}, "", url.toString());
|
|
1173
1209
|
}
|
|
1174
|
-
url.searchParams.delete("account_id");
|
|
1175
|
-
url.searchParams.delete("public_key");
|
|
1176
|
-
url.searchParams.delete("errorCode");
|
|
1177
|
-
url.searchParams.delete("errorMessage");
|
|
1178
|
-
url.searchParams.delete("all_keys");
|
|
1179
|
-
url.searchParams.delete("transactionHashes");
|
|
1180
|
-
url.searchParams.delete("txIds");
|
|
1181
|
-
window.history.replaceState({}, "", url.toString());
|
|
1182
1210
|
} catch (e) {
|
|
1183
1211
|
console.error("Error handling wallet redirect:", e);
|
|
1184
1212
|
}
|