@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/index.js
CHANGED
|
@@ -685,7 +685,7 @@ function parseJsonFromBytes(bytes) {
|
|
|
685
685
|
}
|
|
686
686
|
}
|
|
687
687
|
function withBlockId(params, blockId) {
|
|
688
|
-
return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } :
|
|
688
|
+
return blockId === "final" || blockId === "optimistic" ? { ...params, finality: blockId } : blockId ? { ...params, block_id: blockId } : { ...params, finality: "optimistic" };
|
|
689
689
|
}
|
|
690
690
|
async function queryRpc(method, params) {
|
|
691
691
|
const response = await fetch(_config.nodeUrl, {
|
|
@@ -781,10 +781,10 @@ function convertUnit(s, ...args) {
|
|
|
781
781
|
});
|
|
782
782
|
}
|
|
783
783
|
if (typeof s == "string") {
|
|
784
|
-
|
|
784
|
+
const match = s.match(/([0-9.,_]+)\s*([a-zA-Z]+)?/);
|
|
785
785
|
if (match) {
|
|
786
|
-
|
|
787
|
-
|
|
786
|
+
const amount = match[1].replace(/[_,]/g, "");
|
|
787
|
+
const unitPart = match[2];
|
|
788
788
|
if (unitPart) {
|
|
789
789
|
switch (unitPart.toLowerCase()) {
|
|
790
790
|
case "near":
|
|
@@ -794,9 +794,10 @@ function convertUnit(s, ...args) {
|
|
|
794
794
|
case "ggas":
|
|
795
795
|
return (0, import_big2.default)(amount).mul((0, import_big2.default)(10).pow(9)).toFixed(0);
|
|
796
796
|
case "gas":
|
|
797
|
+
case "yoctonear":
|
|
797
798
|
return (0, import_big2.default)(amount).toFixed(0);
|
|
798
799
|
default:
|
|
799
|
-
throw new Error(`Unknown unit: ${
|
|
800
|
+
throw new Error(`Unknown unit: ${unitPart}`);
|
|
800
801
|
}
|
|
801
802
|
} else {
|
|
802
803
|
return (0, import_big2.default)(amount).toFixed(0);
|
|
@@ -847,7 +848,13 @@ var api = {
|
|
|
847
848
|
return "SignedIn";
|
|
848
849
|
},
|
|
849
850
|
// Query Methods
|
|
850
|
-
async view({
|
|
851
|
+
async view({
|
|
852
|
+
contractId,
|
|
853
|
+
methodName,
|
|
854
|
+
args,
|
|
855
|
+
argsBase64,
|
|
856
|
+
blockId
|
|
857
|
+
}) {
|
|
851
858
|
const encodedArgs = argsBase64 || (args ? toBase64(JSON.stringify(args)) : "");
|
|
852
859
|
const result = await queryRpc(
|
|
853
860
|
"query",
|
|
@@ -863,7 +870,10 @@ var api = {
|
|
|
863
870
|
);
|
|
864
871
|
return parseJsonFromBytes(result.result);
|
|
865
872
|
},
|
|
866
|
-
async account({
|
|
873
|
+
async account({
|
|
874
|
+
accountId,
|
|
875
|
+
blockId
|
|
876
|
+
}) {
|
|
867
877
|
return queryRpc(
|
|
868
878
|
"query",
|
|
869
879
|
withBlockId(
|
|
@@ -878,7 +888,11 @@ var api = {
|
|
|
878
888
|
async block({ blockId }) {
|
|
879
889
|
return queryRpc("block", withBlockId({}, blockId));
|
|
880
890
|
},
|
|
881
|
-
async accessKey({
|
|
891
|
+
async accessKey({
|
|
892
|
+
accountId,
|
|
893
|
+
publicKey,
|
|
894
|
+
blockId
|
|
895
|
+
}) {
|
|
882
896
|
return queryRpc(
|
|
883
897
|
"query",
|
|
884
898
|
withBlockId(
|
|
@@ -898,7 +912,11 @@ var api = {
|
|
|
898
912
|
return [..._txHistory];
|
|
899
913
|
},
|
|
900
914
|
// Transaction Methods
|
|
901
|
-
async sendTx({
|
|
915
|
+
async sendTx({
|
|
916
|
+
receiverId,
|
|
917
|
+
actions,
|
|
918
|
+
waitUntil
|
|
919
|
+
}) {
|
|
902
920
|
const signerId = _state.accountId;
|
|
903
921
|
if (!signerId) {
|
|
904
922
|
throw new Error("Not signed in");
|
|
@@ -918,7 +936,7 @@ var api = {
|
|
|
918
936
|
tx: jsonTransaction2,
|
|
919
937
|
finalState: false
|
|
920
938
|
});
|
|
921
|
-
const url = new URL(window.location.href);
|
|
939
|
+
const url = new URL(typeof window !== "undefined" ? window.location.href : "");
|
|
922
940
|
url.searchParams.set("txIds", txId);
|
|
923
941
|
_adapter.sendTransactions({
|
|
924
942
|
transactions: [jsonTransaction2],
|
|
@@ -927,16 +945,18 @@ var api = {
|
|
|
927
945
|
console.log("Transaction result:", result);
|
|
928
946
|
if (result.url) {
|
|
929
947
|
console.log("Redirecting to wallet:", result.url);
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
948
|
+
if (typeof window !== "undefined") {
|
|
949
|
+
setTimeout(() => {
|
|
950
|
+
window.location.href = result.url;
|
|
951
|
+
}, 100);
|
|
952
|
+
}
|
|
933
953
|
} else if (result.outcomes) {
|
|
934
|
-
result.outcomes.forEach((
|
|
954
|
+
result.outcomes.forEach((r) => {
|
|
935
955
|
updateTxHistory({
|
|
936
956
|
txId,
|
|
937
957
|
status: "Executed",
|
|
938
|
-
result:
|
|
939
|
-
txHash:
|
|
958
|
+
result: r,
|
|
959
|
+
txHash: r.transaction.hash,
|
|
940
960
|
finalState: true
|
|
941
961
|
});
|
|
942
962
|
});
|
|
@@ -964,10 +984,11 @@ var api = {
|
|
|
964
984
|
});
|
|
965
985
|
return txId;
|
|
966
986
|
}
|
|
967
|
-
const toDoPromises = {};
|
|
968
987
|
let nonce = lsGet("nonce");
|
|
988
|
+
let block = lsGet("block");
|
|
989
|
+
const toDoPromises = {};
|
|
969
990
|
if (nonce === null || nonce === void 0) {
|
|
970
|
-
toDoPromises.nonce =
|
|
991
|
+
toDoPromises.nonce = api.accessKey({
|
|
971
992
|
accountId: signerId,
|
|
972
993
|
publicKey
|
|
973
994
|
}).then((accessKey) => {
|
|
@@ -978,30 +999,30 @@ var api = {
|
|
|
978
999
|
return accessKey.nonce;
|
|
979
1000
|
});
|
|
980
1001
|
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
block2 = {
|
|
1002
|
+
if (!block || !block.header || parseFloat(block.header.timestamp_nanosec) / 1e6 + MaxBlockDelayMs < Date.now()) {
|
|
1003
|
+
toDoPromises.block = api.block({ blockId: "final" }).then((b) => {
|
|
1004
|
+
const newBlock = {
|
|
985
1005
|
header: {
|
|
986
|
-
prev_hash:
|
|
987
|
-
timestamp_nanosec:
|
|
1006
|
+
prev_hash: b.header.prev_hash,
|
|
1007
|
+
timestamp_nanosec: b.header.timestamp_nanosec
|
|
988
1008
|
}
|
|
989
1009
|
};
|
|
990
|
-
lsSet("block",
|
|
991
|
-
return
|
|
1010
|
+
lsSet("block", newBlock);
|
|
1011
|
+
return newBlock;
|
|
992
1012
|
});
|
|
993
1013
|
}
|
|
994
1014
|
if (Object.keys(toDoPromises).length > 0) {
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1015
|
+
const results = await Promise.all(Object.values(toDoPromises));
|
|
1016
|
+
const keys = Object.keys(toDoPromises);
|
|
1017
|
+
results.forEach((res, i) => {
|
|
1018
|
+
if (keys[i] === "nonce") {
|
|
1019
|
+
nonce = res;
|
|
1020
|
+
} else if (keys[i] === "block") {
|
|
1021
|
+
block = res;
|
|
1001
1022
|
}
|
|
1002
|
-
}
|
|
1023
|
+
});
|
|
1003
1024
|
}
|
|
1004
|
-
const newNonce = nonce + 1;
|
|
1025
|
+
const newNonce = (nonce ?? 0) + 1;
|
|
1005
1026
|
lsSet("nonce", newNonce);
|
|
1006
1027
|
const blockHash = block.header.prev_hash;
|
|
1007
1028
|
const jsonTransaction = {
|
|
@@ -1016,11 +1037,8 @@ var api = {
|
|
|
1016
1037
|
const transaction = serializeTransaction(jsonTransaction);
|
|
1017
1038
|
const txHash = (0, import_base58_js.binary_to_base58)(import_sha2.sha256(transaction));
|
|
1018
1039
|
const signature = signHash(txHash, privateKey);
|
|
1019
|
-
const
|
|
1020
|
-
|
|
1021
|
-
signature
|
|
1022
|
-
);
|
|
1023
|
-
const signedTxBase64 = toBase64(singedTransaction);
|
|
1040
|
+
const signedTransaction = serializeSignedTransaction(jsonTransaction, signature);
|
|
1041
|
+
const signedTxBase64 = toBase64(signedTransaction);
|
|
1024
1042
|
updateTxHistory({
|
|
1025
1043
|
status: "Pending",
|
|
1026
1044
|
txId,
|
|
@@ -1053,9 +1071,11 @@ var api = {
|
|
|
1053
1071
|
}
|
|
1054
1072
|
if (result.url) {
|
|
1055
1073
|
console.log("Redirecting to wallet:", result.url);
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1074
|
+
if (typeof window !== "undefined") {
|
|
1075
|
+
setTimeout(() => {
|
|
1076
|
+
window.location.href = result.url;
|
|
1077
|
+
}, 100);
|
|
1078
|
+
}
|
|
1059
1079
|
} else if (result.accountId) {
|
|
1060
1080
|
updateState({
|
|
1061
1081
|
accountId: result.accountId
|
|
@@ -1088,7 +1108,13 @@ var api = {
|
|
|
1088
1108
|
},
|
|
1089
1109
|
// Action Helpers
|
|
1090
1110
|
actions: {
|
|
1091
|
-
functionCall: ({
|
|
1111
|
+
functionCall: ({
|
|
1112
|
+
methodName,
|
|
1113
|
+
gas,
|
|
1114
|
+
deposit,
|
|
1115
|
+
args,
|
|
1116
|
+
argsBase64
|
|
1117
|
+
}) => ({
|
|
1092
1118
|
type: "FunctionCall",
|
|
1093
1119
|
methodName,
|
|
1094
1120
|
args,
|
|
@@ -1149,66 +1175,68 @@ var api = {
|
|
|
1149
1175
|
}
|
|
1150
1176
|
};
|
|
1151
1177
|
try {
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
if (accountId && publicKey) {
|
|
1163
|
-
if (publicKey === _state.publicKey) {
|
|
1164
|
-
updateState({
|
|
1165
|
-
accountId
|
|
1166
|
-
});
|
|
1167
|
-
} else {
|
|
1168
|
-
console.error(
|
|
1169
|
-
new Error("Public key mismatch from wallet redirect"),
|
|
1170
|
-
publicKey,
|
|
1171
|
-
_state.publicKey
|
|
1172
|
-
);
|
|
1178
|
+
if (typeof window !== "undefined") {
|
|
1179
|
+
const url = new URL(window.location.href);
|
|
1180
|
+
const accountId = url.searchParams.get("account_id");
|
|
1181
|
+
const publicKey = url.searchParams.get("public_key");
|
|
1182
|
+
const errorCode = url.searchParams.get("errorCode");
|
|
1183
|
+
const errorMessage = url.searchParams.get("errorMessage");
|
|
1184
|
+
const transactionHashes = url.searchParams.get("transactionHashes");
|
|
1185
|
+
const txIds = url.searchParams.get("txIds");
|
|
1186
|
+
if (errorCode || errorMessage) {
|
|
1187
|
+
console.warn(new Error(`Wallet error: ${errorCode} ${errorMessage}`));
|
|
1173
1188
|
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
if (txIdsArray.length > txHashes.length) {
|
|
1179
|
-
txIdsArray.forEach((txId, i) => {
|
|
1180
|
-
updateTxHistory({
|
|
1181
|
-
txId,
|
|
1182
|
-
status: "RejectedByUser",
|
|
1183
|
-
finalState: true
|
|
1189
|
+
if (accountId && publicKey) {
|
|
1190
|
+
if (publicKey === _state.publicKey) {
|
|
1191
|
+
updateState({
|
|
1192
|
+
accountId
|
|
1184
1193
|
});
|
|
1185
|
-
}
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1194
|
+
} else {
|
|
1195
|
+
console.error(
|
|
1196
|
+
new Error("Public key mismatch from wallet redirect"),
|
|
1197
|
+
publicKey,
|
|
1198
|
+
_state.publicKey
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
if (transactionHashes || txIds) {
|
|
1203
|
+
const txHashes = transactionHashes ? transactionHashes.split(",") : [];
|
|
1204
|
+
const txIdsArray = txIds ? txIds.split(",") : [];
|
|
1205
|
+
if (txIdsArray.length > txHashes.length) {
|
|
1206
|
+
txIdsArray.forEach((txId, i) => {
|
|
1207
|
+
updateTxHistory({
|
|
1208
|
+
txId,
|
|
1209
|
+
status: "RejectedByUser",
|
|
1210
|
+
finalState: true
|
|
1211
|
+
});
|
|
1193
1212
|
});
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1213
|
+
} else if (txIdsArray.length === txHashes.length) {
|
|
1214
|
+
txIdsArray.forEach((txId, i) => {
|
|
1215
|
+
updateTxHistory({
|
|
1216
|
+
txId,
|
|
1217
|
+
status: "PendingGotTxHash",
|
|
1218
|
+
txHash: txHashes[i],
|
|
1219
|
+
finalState: false
|
|
1220
|
+
});
|
|
1221
|
+
afterTxSent(txId);
|
|
1222
|
+
});
|
|
1223
|
+
} else {
|
|
1224
|
+
console.error(
|
|
1225
|
+
new Error("Transaction hash mismatch from wallet redirect"),
|
|
1226
|
+
txIdsArray,
|
|
1227
|
+
txHashes
|
|
1228
|
+
);
|
|
1229
|
+
}
|
|
1202
1230
|
}
|
|
1231
|
+
url.searchParams.delete("account_id");
|
|
1232
|
+
url.searchParams.delete("public_key");
|
|
1233
|
+
url.searchParams.delete("errorCode");
|
|
1234
|
+
url.searchParams.delete("errorMessage");
|
|
1235
|
+
url.searchParams.delete("all_keys");
|
|
1236
|
+
url.searchParams.delete("transactionHashes");
|
|
1237
|
+
url.searchParams.delete("txIds");
|
|
1238
|
+
window.history.replaceState({}, "", url.toString());
|
|
1203
1239
|
}
|
|
1204
|
-
url.searchParams.delete("account_id");
|
|
1205
|
-
url.searchParams.delete("public_key");
|
|
1206
|
-
url.searchParams.delete("errorCode");
|
|
1207
|
-
url.searchParams.delete("errorMessage");
|
|
1208
|
-
url.searchParams.delete("all_keys");
|
|
1209
|
-
url.searchParams.delete("transactionHashes");
|
|
1210
|
-
url.searchParams.delete("txIds");
|
|
1211
|
-
window.history.replaceState({}, "", url.toString());
|
|
1212
1240
|
} catch (e) {
|
|
1213
1241
|
console.error("Error handling wallet redirect:", e);
|
|
1214
1242
|
}
|