btc-wallet 0.5.10-beta → 0.5.12-beta
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/config.d.ts +4 -1
- package/dist/connector/injected.d.ts +0 -1
- package/dist/core/btcUtils.d.ts +17 -9
- package/dist/index.js +227 -158
- package/dist/index.js.map +2 -2
- package/dist/utils/nearUtils.d.ts +7 -1
- package/dist/utils/request.d.ts +1 -0
- package/esm/index.js +227 -158
- package/esm/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -102,7 +102,7 @@ __export(src_exports, {
|
|
102
102
|
getConfig: () => getConfig,
|
103
103
|
getCsnaAccountId: () => getCsnaAccountId,
|
104
104
|
getDepositAmount: () => getDepositAmount,
|
105
|
-
|
105
|
+
getTokenBalance: () => getTokenBalance,
|
106
106
|
getVersion: () => getVersion,
|
107
107
|
getWithdrawTransaction: () => getWithdrawTransaction,
|
108
108
|
sendBitcoin: () => sendBitcoin,
|
@@ -432,11 +432,11 @@ var MobileWalletConnect = class {
|
|
432
432
|
}
|
433
433
|
static redirectToWallet(walletId) {
|
434
434
|
return __async(this, null, function* () {
|
435
|
-
var _a;
|
436
435
|
if (isMobile()) {
|
437
436
|
const currentUrl = window.location.href;
|
438
437
|
const universalLink = this.getUniversalLink(walletId, currentUrl);
|
439
|
-
|
438
|
+
const showGuideDialog = () => __async(this, null, function* () {
|
439
|
+
var _a;
|
440
440
|
try {
|
441
441
|
yield (_a = navigator.clipboard) == null ? void 0 : _a.writeText(currentUrl);
|
442
442
|
} catch (error) {
|
@@ -454,9 +454,23 @@ var MobileWalletConnect = class {
|
|
454
454
|
`,
|
455
455
|
dangerouslyUseHTML: true
|
456
456
|
});
|
457
|
+
});
|
458
|
+
if (!universalLink) {
|
459
|
+
yield showGuideDialog();
|
457
460
|
return false;
|
458
461
|
}
|
462
|
+
const openWallet = () => {
|
463
|
+
const iframe = document.createElement("iframe");
|
464
|
+
iframe.style.display = "none";
|
465
|
+
iframe.src = universalLink;
|
466
|
+
document.body.appendChild(iframe);
|
467
|
+
setTimeout(() => __async(this, null, function* () {
|
468
|
+
document.body.removeChild(iframe);
|
469
|
+
yield showGuideDialog();
|
470
|
+
}), 2e3);
|
471
|
+
};
|
459
472
|
window.location.href = universalLink;
|
473
|
+
setTimeout(openWallet, 100);
|
460
474
|
return true;
|
461
475
|
}
|
462
476
|
return false;
|
@@ -505,24 +519,6 @@ var InjectedConnector = class extends BaseConnector {
|
|
505
519
|
return accounts;
|
506
520
|
});
|
507
521
|
}
|
508
|
-
getUniversalLink(url) {
|
509
|
-
url = "https://www.deltatrade.ai/";
|
510
|
-
console.log(this.metadata.id);
|
511
|
-
switch (this.metadata.id) {
|
512
|
-
case "unisat":
|
513
|
-
return `unisat://dapp?url=${encodeURIComponent(url)}`;
|
514
|
-
case "okx":
|
515
|
-
return `okx://wallet/dapp/url?dappUrl=${encodeURIComponent(url)}`;
|
516
|
-
case "bitget":
|
517
|
-
return `https://bkcode.vip?action=dapp&url=${encodeURIComponent(url)}`;
|
518
|
-
case "binance":
|
519
|
-
return `binance://dapp?url=${encodeURIComponent(url)}`;
|
520
|
-
case "xverse":
|
521
|
-
return `https://connect.xverse.app/browser?url=${encodeURIComponent(url)}`;
|
522
|
-
default:
|
523
|
-
return url;
|
524
|
-
}
|
525
|
-
}
|
526
522
|
getAccounts() {
|
527
523
|
return __async(this, null, function* () {
|
528
524
|
const accounts = yield this.getProviderOrThrow().getAccounts();
|
@@ -801,18 +797,10 @@ var XverseConnector = class extends BaseConnector {
|
|
801
797
|
message: signStr,
|
802
798
|
protocol: "ECDSA"
|
803
799
|
});
|
804
|
-
const modifiedSig = Buffer.from(result.signature, "base64");
|
805
800
|
console.log("xverse walletType", address.walletType);
|
806
|
-
console.log("xverse raw sig", result.signature
|
807
|
-
|
808
|
-
|
809
|
-
modifiedSig[0] = 31 + (modifiedSig[0] - 39);
|
810
|
-
} else if (address.addressType === "p2sh") {
|
811
|
-
modifiedSig[0] = 31 + (modifiedSig[0] - 35);
|
812
|
-
}
|
813
|
-
} else {
|
814
|
-
modifiedSig[0] = 31 + (modifiedSig[0] - 31) % 4;
|
815
|
-
}
|
801
|
+
console.log("xverse raw sig", result.signature);
|
802
|
+
const modifiedSig = Buffer.from(result.signature, "base64");
|
803
|
+
modifiedSig[0] = 31 + (modifiedSig[0] - 31) % 4;
|
816
804
|
const sig = modifiedSig.toString("base64");
|
817
805
|
console.log("xverse modified sig", sig);
|
818
806
|
return sig;
|
@@ -2896,7 +2884,10 @@ var import_js_sha256 = require("js-sha256");
|
|
2896
2884
|
var walletConfig = {
|
2897
2885
|
dev: {
|
2898
2886
|
base_url: "https://api.dev.satoshibridge.top",
|
2899
|
-
|
2887
|
+
btcToken: "nbtc-dev.testnet",
|
2888
|
+
btcTokenDecimals: 8,
|
2889
|
+
nearToken: "wrap.testnet",
|
2890
|
+
nearTokenDecimals: 24,
|
2900
2891
|
accountContractId: "acc-dev.testnet",
|
2901
2892
|
bridgeContractId: "brg-dev.testnet",
|
2902
2893
|
walletUrl: "https://wallet-dev.satoshibridge.top",
|
@@ -2904,7 +2895,10 @@ var walletConfig = {
|
|
2904
2895
|
},
|
2905
2896
|
testnet: {
|
2906
2897
|
base_url: "https://api.testnet.satoshibridge.top",
|
2907
|
-
|
2898
|
+
btcToken: "nbtc2-nsp.testnet",
|
2899
|
+
btcTokenDecimals: 8,
|
2900
|
+
nearToken: "wrap.testnet",
|
2901
|
+
nearTokenDecimals: 24,
|
2908
2902
|
accountContractId: "acc2-nsp.testnet",
|
2909
2903
|
bridgeContractId: "brg2-nsp.testnet",
|
2910
2904
|
walletUrl: "https://wallet-test.satoshibridge.top",
|
@@ -2912,7 +2906,10 @@ var walletConfig = {
|
|
2912
2906
|
},
|
2913
2907
|
private_mainnet: {
|
2914
2908
|
base_url: "https://api.stg.satoshibridge.top",
|
2915
|
-
|
2909
|
+
btcToken: "nbtc.toalice.near",
|
2910
|
+
btcTokenDecimals: 8,
|
2911
|
+
nearToken: "wrap.near",
|
2912
|
+
nearTokenDecimals: 24,
|
2916
2913
|
accountContractId: "acc.toalice.near",
|
2917
2914
|
bridgeContractId: "brg.toalice.near",
|
2918
2915
|
walletUrl: "https://wallet-stg.satoshibridge.top",
|
@@ -2920,7 +2917,10 @@ var walletConfig = {
|
|
2920
2917
|
},
|
2921
2918
|
mainnet: {
|
2922
2919
|
base_url: "https://api.satos.network",
|
2923
|
-
|
2920
|
+
btcToken: "nbtc.bridge.near",
|
2921
|
+
btcTokenDecimals: 8,
|
2922
|
+
nearToken: "wrap.near",
|
2923
|
+
nearTokenDecimals: 24,
|
2924
2924
|
accountContractId: "acc.ref-labs.near",
|
2925
2925
|
bridgeContractId: "btc-connector.bridge.near",
|
2926
2926
|
walletUrl: "https://wallet.satoshibridge.top",
|
@@ -2946,69 +2946,24 @@ var import_big = __toESM(require("big.js"), 1);
|
|
2946
2946
|
|
2947
2947
|
// src/utils/nearUtils.ts
|
2948
2948
|
var import_near_api_js = require("near-api-js");
|
2949
|
-
function nearCallFunction(contractId, methodName, args, options) {
|
2950
|
-
return __async(this, null, function* () {
|
2951
|
-
const nearProvider = (options == null ? void 0 : options.provider) || new import_near_api_js.providers.FailoverRpcProvider(
|
2952
|
-
nearRpcUrls[options == null ? void 0 : options.network].map(
|
2953
|
-
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2954
|
-
)
|
2955
|
-
);
|
2956
|
-
const res = yield nearProvider.query({
|
2957
|
-
request_type: "call_function",
|
2958
|
-
account_id: contractId,
|
2959
|
-
method_name: methodName,
|
2960
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2961
|
-
finality: "final"
|
2962
|
-
});
|
2963
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
2964
|
-
});
|
2965
|
-
}
|
2966
|
-
function pollTransactionStatuses(network, hashes) {
|
2967
|
-
return __async(this, null, function* () {
|
2968
|
-
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
2969
|
-
Object.values(nearRpcUrls[network]).map(
|
2970
|
-
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2971
|
-
)
|
2972
|
-
);
|
2973
|
-
const maxAttempts = 30;
|
2974
|
-
let currentAttempt = 0;
|
2975
|
-
const pendingHashes = new Set(hashes);
|
2976
|
-
const results = /* @__PURE__ */ new Map();
|
2977
|
-
while (pendingHashes.size > 0 && currentAttempt < maxAttempts) {
|
2978
|
-
currentAttempt++;
|
2979
|
-
const promises = Array.from(pendingHashes).map((hash) => __async(this, null, function* () {
|
2980
|
-
try {
|
2981
|
-
const result2 = yield provider.txStatus(hash, "unused", "FINAL");
|
2982
|
-
if (result2 && result2.status) {
|
2983
|
-
console.log(`Transaction ${hash} result:`, result2);
|
2984
|
-
results.set(hash, result2);
|
2985
|
-
pendingHashes.delete(hash);
|
2986
|
-
}
|
2987
|
-
} catch (error) {
|
2988
|
-
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
2989
|
-
}
|
2990
|
-
}));
|
2991
|
-
yield Promise.all(promises);
|
2992
|
-
if (pendingHashes.size > 0) {
|
2993
|
-
if (currentAttempt === maxAttempts) {
|
2994
|
-
throw new Error(
|
2995
|
-
`Transactions not found after max attempts: ${Array.from(pendingHashes).join(", ")}`
|
2996
|
-
);
|
2997
|
-
}
|
2998
|
-
console.log(
|
2999
|
-
`Waiting for ${pendingHashes.size} transactions, retrying ${maxAttempts - currentAttempt} more times`
|
3000
|
-
);
|
3001
|
-
yield delay(1e4);
|
3002
|
-
}
|
3003
|
-
}
|
3004
|
-
const result = hashes.map((hash) => results.get(hash)).filter(Boolean);
|
3005
|
-
return result;
|
3006
|
-
});
|
3007
|
-
}
|
3008
2949
|
|
3009
2950
|
// src/utils/request.ts
|
3010
2951
|
var cache = /* @__PURE__ */ new Map();
|
3011
2952
|
var defaultCacheTimeout = 3e3;
|
2953
|
+
function withCache(key, fetcher, timeout = defaultCacheTimeout) {
|
2954
|
+
const cached = cache.get(key);
|
2955
|
+
const isCacheValid = cached && Date.now() - cached.timestamp < timeout;
|
2956
|
+
if (isCacheValid) {
|
2957
|
+
return Promise.resolve(cached.data);
|
2958
|
+
}
|
2959
|
+
return fetcher().then((data) => {
|
2960
|
+
cache.set(key, { timestamp: Date.now(), data });
|
2961
|
+
setTimeout(() => {
|
2962
|
+
cache.delete(key);
|
2963
|
+
}, timeout);
|
2964
|
+
return data;
|
2965
|
+
});
|
2966
|
+
}
|
3012
2967
|
function request(url, options) {
|
3013
2968
|
return __async(this, null, function* () {
|
3014
2969
|
var _a;
|
@@ -3050,13 +3005,7 @@ function request(url, options) {
|
|
3050
3005
|
if (options.shouldStopPolling(data)) {
|
3051
3006
|
return data;
|
3052
3007
|
}
|
3053
|
-
|
3054
|
-
yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
|
3055
|
-
return request(url, __spreadProps(__spreadValues({}, options), {
|
3056
|
-
maxPollingAttempts: options.maxPollingAttempts - 1
|
3057
|
-
}));
|
3058
|
-
}
|
3059
|
-
throw new Error("Polling failed: maximum attempts reached without meeting the condition");
|
3008
|
+
throw new Error("Polling should continue");
|
3060
3009
|
}
|
3061
3010
|
if (cacheKey) {
|
3062
3011
|
cache.set(cacheKey, { timestamp: Date.now(), data });
|
@@ -3066,6 +3015,7 @@ function request(url, options) {
|
|
3066
3015
|
}
|
3067
3016
|
return data;
|
3068
3017
|
} catch (err) {
|
3018
|
+
console.error(err);
|
3069
3019
|
if (retryCount > 0) {
|
3070
3020
|
console.log(`Retrying... attempts left: ${retryCount}`);
|
3071
3021
|
return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount - 1 }));
|
@@ -3079,8 +3029,85 @@ function request(url, options) {
|
|
3079
3029
|
}));
|
3080
3030
|
}
|
3081
3031
|
}
|
3082
|
-
|
3032
|
+
return Promise.reject(err);
|
3033
|
+
}
|
3034
|
+
});
|
3035
|
+
}
|
3036
|
+
|
3037
|
+
// src/utils/nearUtils.ts
|
3038
|
+
function getNearProvider(option) {
|
3039
|
+
return option.provider || new import_near_api_js.providers.FailoverRpcProvider(
|
3040
|
+
nearRpcUrls[option == null ? void 0 : option.network].map(
|
3041
|
+
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
3042
|
+
)
|
3043
|
+
);
|
3044
|
+
}
|
3045
|
+
function nearCallFunction(_0, _1, _2) {
|
3046
|
+
return __async(this, arguments, function* (contractId, methodName, args, options = {}) {
|
3047
|
+
if (!options.skipCache) {
|
3048
|
+
const cacheKey = `near:${contractId}:${methodName}:${args ? JSON.stringify(args) : ""}`;
|
3049
|
+
return withCache(
|
3050
|
+
cacheKey,
|
3051
|
+
() => executeNearCall(contractId, methodName, args, options),
|
3052
|
+
options.cacheTimeout
|
3053
|
+
);
|
3054
|
+
}
|
3055
|
+
return executeNearCall(contractId, methodName, args, options);
|
3056
|
+
});
|
3057
|
+
}
|
3058
|
+
function executeNearCall(contractId, methodName, args, options) {
|
3059
|
+
return __async(this, null, function* () {
|
3060
|
+
const nearProvider = getNearProvider(options);
|
3061
|
+
const res = yield nearProvider.query({
|
3062
|
+
request_type: "call_function",
|
3063
|
+
account_id: contractId,
|
3064
|
+
method_name: methodName,
|
3065
|
+
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
3066
|
+
finality: "final"
|
3067
|
+
});
|
3068
|
+
return JSON.parse(Buffer.from(res.result).toString());
|
3069
|
+
});
|
3070
|
+
}
|
3071
|
+
function pollTransactionStatuses(network, hashes) {
|
3072
|
+
return __async(this, null, function* () {
|
3073
|
+
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
3074
|
+
Object.values(nearRpcUrls[network]).map(
|
3075
|
+
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
3076
|
+
)
|
3077
|
+
);
|
3078
|
+
const maxAttempts = 30;
|
3079
|
+
let currentAttempt = 0;
|
3080
|
+
const pendingHashes = new Set(hashes);
|
3081
|
+
const results = /* @__PURE__ */ new Map();
|
3082
|
+
while (pendingHashes.size > 0 && currentAttempt < maxAttempts) {
|
3083
|
+
currentAttempt++;
|
3084
|
+
const promises = Array.from(pendingHashes).map((hash) => __async(this, null, function* () {
|
3085
|
+
try {
|
3086
|
+
const result2 = yield provider.txStatus(hash, "unused", "FINAL");
|
3087
|
+
if (result2 && result2.status) {
|
3088
|
+
console.log(`Transaction ${hash} result:`, result2);
|
3089
|
+
results.set(hash, result2);
|
3090
|
+
pendingHashes.delete(hash);
|
3091
|
+
}
|
3092
|
+
} catch (error) {
|
3093
|
+
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
3094
|
+
}
|
3095
|
+
}));
|
3096
|
+
yield Promise.all(promises);
|
3097
|
+
if (pendingHashes.size > 0) {
|
3098
|
+
if (currentAttempt === maxAttempts) {
|
3099
|
+
throw new Error(
|
3100
|
+
`Transactions not found after max attempts: ${Array.from(pendingHashes).join(", ")}`
|
3101
|
+
);
|
3102
|
+
}
|
3103
|
+
console.log(
|
3104
|
+
`Waiting for ${pendingHashes.size} transactions, retrying ${maxAttempts - currentAttempt} more times`
|
3105
|
+
);
|
3106
|
+
yield delay(1e4);
|
3107
|
+
}
|
3083
3108
|
}
|
3109
|
+
const result = hashes.map((hash) => results.get(hash)).filter(Boolean);
|
3110
|
+
return result;
|
3084
3111
|
});
|
3085
3112
|
}
|
3086
3113
|
|
@@ -3237,9 +3264,10 @@ function nearCall(contractId, methodName, args) {
|
|
3237
3264
|
return nearCallFunction(contractId, methodName, args, { network });
|
3238
3265
|
});
|
3239
3266
|
}
|
3240
|
-
function getAccountInfo(
|
3241
|
-
return __async(this,
|
3242
|
-
const
|
3267
|
+
function getAccountInfo(_0) {
|
3268
|
+
return __async(this, arguments, function* ({ csna, env }) {
|
3269
|
+
const config = yield getConfig(env);
|
3270
|
+
const accountInfo = yield nearCall(config.accountContractId, "get_account", {
|
3243
3271
|
account_id: csna
|
3244
3272
|
}).catch((error) => {
|
3245
3273
|
return void 0;
|
@@ -3248,16 +3276,46 @@ function getAccountInfo(csna, accountContractId) {
|
|
3248
3276
|
return accountInfo;
|
3249
3277
|
});
|
3250
3278
|
}
|
3251
|
-
function
|
3279
|
+
function getTokenBalance(_0) {
|
3280
|
+
return __async(this, arguments, function* ({
|
3281
|
+
csna,
|
3282
|
+
tokenId,
|
3283
|
+
env
|
3284
|
+
}) {
|
3285
|
+
const network = yield getNetwork();
|
3286
|
+
const config = yield getConfig(env);
|
3287
|
+
const nearProvider = getNearProvider({ network });
|
3288
|
+
try {
|
3289
|
+
if (tokenId === config.nearToken) {
|
3290
|
+
const nearAccount = yield nearProvider.query({
|
3291
|
+
request_type: "view_account",
|
3292
|
+
account_id: csna,
|
3293
|
+
finality: "final"
|
3294
|
+
});
|
3295
|
+
const balance = parseFloat(nearAccount.amount) / __pow(10, config.nearTokenDecimals);
|
3296
|
+
return { balance, rawBalance: nearAccount.amount };
|
3297
|
+
} else {
|
3298
|
+
const res = yield nearCall(tokenId, "ft_balance_of", { account_id: csna });
|
3299
|
+
const decimals = tokenId === config.btcToken ? config.btcTokenDecimals : (yield nearCall(tokenId, "ft_metadata", {})).decimals;
|
3300
|
+
const balance = parseFloat(res) / __pow(10, decimals);
|
3301
|
+
return { balance, rawBalance: res };
|
3302
|
+
}
|
3303
|
+
} catch (error) {
|
3304
|
+
console.error("getTokenBalance error:", error);
|
3305
|
+
return { balance: 0, rawBalance: "0" };
|
3306
|
+
}
|
3307
|
+
});
|
3308
|
+
}
|
3309
|
+
function checkGasTokenBalance(csna, minAmount, env) {
|
3252
3310
|
return __async(this, null, function* () {
|
3253
|
-
const
|
3254
|
-
|
3255
|
-
|
3311
|
+
const config = yield getConfig(env);
|
3312
|
+
const { rawBalance } = yield getTokenBalance({ csna, tokenId: config.btcToken, env });
|
3313
|
+
console.log("gas token balance:", rawBalance);
|
3314
|
+
if (new import_big.default(rawBalance).lt(minAmount)) {
|
3256
3315
|
yield Dialog.confirm({
|
3257
3316
|
title: "Gas token balance is insufficient",
|
3258
3317
|
message: "Please deposit gas token to continue, will open bridge website."
|
3259
3318
|
});
|
3260
|
-
const config = yield getConfig(env);
|
3261
3319
|
window.open(config.bridgeUrl, "_blank");
|
3262
3320
|
throw new Error("Gas token balance is insufficient");
|
3263
3321
|
}
|
@@ -3338,18 +3396,6 @@ function getBtcBalance() {
|
|
3338
3396
|
};
|
3339
3397
|
});
|
3340
3398
|
}
|
3341
|
-
function getNBTCBalance(address, env) {
|
3342
|
-
return __async(this, null, function* () {
|
3343
|
-
const config = yield getConfig(env || "mainnet");
|
3344
|
-
const rawBalance = yield nearCall(config.token, "ft_balance_of", {
|
3345
|
-
account_id: address
|
3346
|
-
});
|
3347
|
-
const balance = new import_big.default(rawBalance).div(__pow(10, 8)).round(8, import_big.default.roundDown).toNumber();
|
3348
|
-
const rawAvailableBalance = new import_big.default(rawBalance).minus(1e3).toNumber();
|
3349
|
-
const availableBalance = new import_big.default(rawAvailableBalance).div(__pow(10, 8)).round(8, import_big.default.roundDown).toNumber();
|
3350
|
-
return { balance, availableBalance, rawBalance, rawAvailableBalance };
|
3351
|
-
});
|
3352
|
-
}
|
3353
3399
|
function sendBitcoin(address, amount, feeRate) {
|
3354
3400
|
return __async(this, null, function* () {
|
3355
3401
|
const { sendBitcoin: sendBitcoin2 } = getBtcProvider();
|
@@ -3369,7 +3415,7 @@ function getDepositAmount(amount, option) {
|
|
3369
3415
|
const _newAccountMinDepositAmount = (_a = option == null ? void 0 : option.newAccountMinDepositAmount) != null ? _a : true;
|
3370
3416
|
const config = yield getConfig(env);
|
3371
3417
|
const csna = yield getCsnaAccountId(env);
|
3372
|
-
const accountInfo = yield getAccountInfo(csna,
|
3418
|
+
const accountInfo = yield getAccountInfo({ csna, env });
|
3373
3419
|
const debtAction = yield checkGasTokenDebt(accountInfo, env, false);
|
3374
3420
|
const repayAmount = (debtAction == null ? void 0 : debtAction.amount) || 0;
|
3375
3421
|
const {
|
@@ -3448,7 +3494,7 @@ function executeBTCDepositAndAction(_0) {
|
|
3448
3494
|
env,
|
3449
3495
|
newAccountMinDepositAmount
|
3450
3496
|
});
|
3451
|
-
const accountInfo = yield getAccountInfo(csna,
|
3497
|
+
const accountInfo = yield getAccountInfo({ csna, env });
|
3452
3498
|
const newActions = [];
|
3453
3499
|
const debtAction = yield checkGasTokenDebt(accountInfo, env, false);
|
3454
3500
|
if (debtAction) {
|
@@ -3462,12 +3508,12 @@ function executeBTCDepositAndAction(_0) {
|
|
3462
3508
|
}));
|
3463
3509
|
}
|
3464
3510
|
const storageDepositMsg = {};
|
3465
|
-
const registerRes = yield nearCall((action == null ? void 0 : action.receiver_id) || config.
|
3511
|
+
const registerRes = yield nearCall((action == null ? void 0 : action.receiver_id) || config.btcToken, "storage_balance_of", {
|
3466
3512
|
account_id: csna
|
3467
3513
|
});
|
3468
3514
|
if (!(registerRes == null ? void 0 : registerRes.available)) {
|
3469
3515
|
storageDepositMsg.storage_deposit_msg = {
|
3470
|
-
contract_id: (action == null ? void 0 : action.receiver_id) || config.
|
3516
|
+
contract_id: (action == null ? void 0 : action.receiver_id) || config.btcToken,
|
3471
3517
|
deposit: registerDeposit || NEAR_STORAGE_DEPOSIT_AMOUNT,
|
3472
3518
|
registration_only: true
|
3473
3519
|
};
|
@@ -3673,7 +3719,7 @@ function getWithdrawTransaction(_0) {
|
|
3673
3719
|
};
|
3674
3720
|
const csna = yield getCsnaAccountId(env);
|
3675
3721
|
const transaction = {
|
3676
|
-
receiverId: config.
|
3722
|
+
receiverId: config.btcToken,
|
3677
3723
|
signerId: csna,
|
3678
3724
|
actions: [
|
3679
3725
|
{
|
@@ -4217,20 +4263,16 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4217
4263
|
throw new Error("Wallet state is invalid, please reconnect your wallet.");
|
4218
4264
|
}
|
4219
4265
|
const btcContext = window.btcContext;
|
4220
|
-
const
|
4221
|
-
const accountInfo = yield getAccountInfo(
|
4266
|
+
const csna = state.getAccount();
|
4267
|
+
const accountInfo = yield getAccountInfo({ csna, env });
|
4222
4268
|
yield checkGasTokenDebt(accountInfo, env, true);
|
4223
4269
|
const trans = [...params.transactions];
|
4224
4270
|
console.log("signAndSendTransactions raw trans:", trans);
|
4225
|
-
const
|
4226
|
-
const { transferGasTransaction, useNearPayGas, gasLimit } = yield calculateGasStrategy(
|
4227
|
-
gasTokenBalance,
|
4228
|
-
trans
|
4229
|
-
);
|
4271
|
+
const { transferGasTransaction, useNearPayGas, gasLimit } = yield calculateGasStrategy(trans);
|
4230
4272
|
console.log("transferGasTransaction:", transferGasTransaction);
|
4231
4273
|
console.log("useNearPayGas:", useNearPayGas);
|
4232
4274
|
console.log("gasLimit:", gasLimit);
|
4233
|
-
yield checkGasTokenBalance(
|
4275
|
+
yield checkGasTokenBalance(csna, gasLimit, env);
|
4234
4276
|
if (transferGasTransaction) {
|
4235
4277
|
trans.unshift(transferGasTransaction);
|
4236
4278
|
}
|
@@ -4238,14 +4280,14 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4238
4280
|
const newTrans = yield Promise.all(
|
4239
4281
|
trans.map((transaction, index) => convertTransactionToTxHex(transaction, index))
|
4240
4282
|
);
|
4241
|
-
const nonceFromApi = yield getNonce(currentConfig.base_url,
|
4283
|
+
const nonceFromApi = yield getNonce(currentConfig.base_url, csna);
|
4242
4284
|
const nonceFromContract = (accountInfo == null ? void 0 : accountInfo.nonce) || 0;
|
4243
4285
|
const nonce = Number(nonceFromApi) > Number(nonceFromContract) ? String(nonceFromApi) : String(nonceFromContract);
|
4244
4286
|
const intention = {
|
4245
4287
|
chain_id: "397",
|
4246
|
-
csna
|
4288
|
+
csna,
|
4247
4289
|
near_transactions: newTrans.map((t) => t.txHex),
|
4248
|
-
gas_token: currentConfig.
|
4290
|
+
gas_token: currentConfig.btcToken,
|
4249
4291
|
gas_limit: gasLimit,
|
4250
4292
|
use_near_pay_gas: useNearPayGas,
|
4251
4293
|
nonce
|
@@ -4258,7 +4300,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4258
4300
|
data: toHex(strIntention)
|
4259
4301
|
});
|
4260
4302
|
yield checkBtcTransactionStatus(currentConfig.base_url, signature);
|
4261
|
-
const hash = newTrans.map((t) => t.hash);
|
4303
|
+
const hash = newTrans.slice(1).map((t) => t.hash);
|
4262
4304
|
console.log("txHash:", hash);
|
4263
4305
|
const result = yield pollTransactionStatuses(options.network.networkId, hash);
|
4264
4306
|
return result;
|
@@ -4266,12 +4308,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4266
4308
|
}
|
4267
4309
|
function calculateGasLimit(params) {
|
4268
4310
|
return __async(this, null, function* () {
|
4269
|
-
const accountId = state.getAccount();
|
4270
|
-
const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
|
4271
4311
|
const trans = [...params.transactions];
|
4272
4312
|
console.log("raw trans:", trans);
|
4273
|
-
const
|
4274
|
-
const { gasLimit } = yield calculateGasStrategy(gasTokenBalance, trans);
|
4313
|
+
const { gasLimit } = yield calculateGasStrategy(trans);
|
4275
4314
|
return gasLimit;
|
4276
4315
|
});
|
4277
4316
|
}
|
@@ -4279,7 +4318,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4279
4318
|
return __async(this, null, function* () {
|
4280
4319
|
return {
|
4281
4320
|
signerId: accountId,
|
4282
|
-
receiverId: currentConfig.
|
4321
|
+
receiverId: currentConfig.btcToken,
|
4283
4322
|
actions: [
|
4284
4323
|
{
|
4285
4324
|
type: "FunctionCall",
|
@@ -4307,7 +4346,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4307
4346
|
} else {
|
4308
4347
|
newGasLimit = yield getPredictedGasAmount(
|
4309
4348
|
currentConfig.accountContractId,
|
4310
|
-
currentConfig.
|
4349
|
+
currentConfig.btcToken,
|
4311
4350
|
[transferTxHex, ...transactions2.map((t) => t.txHex)]
|
4312
4351
|
);
|
4313
4352
|
}
|
@@ -4328,31 +4367,61 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4328
4367
|
return gasAmount.toString();
|
4329
4368
|
});
|
4330
4369
|
}
|
4331
|
-
function calculateGasStrategy(
|
4370
|
+
function calculateGasStrategy(transactions2) {
|
4332
4371
|
return __async(this, null, function* () {
|
4333
4372
|
var _a;
|
4334
4373
|
const accountId = state.getAccount();
|
4335
|
-
const
|
4336
|
-
|
4337
|
-
|
4338
|
-
|
4374
|
+
const accountInfo = yield getAccountInfo({ csna: accountId, env });
|
4375
|
+
const gasTokenBalance = (accountInfo == null ? void 0 : accountInfo.gas_token[currentConfig.btcToken]) || "0";
|
4376
|
+
const { balance: nearBalance } = yield getTokenBalance({
|
4377
|
+
csna: accountId,
|
4378
|
+
tokenId: currentConfig.nearToken,
|
4379
|
+
env
|
4380
|
+
});
|
4381
|
+
const { balance: btcBalance } = yield getTokenBalance({
|
4382
|
+
csna: accountId,
|
4383
|
+
tokenId: currentConfig.btcToken,
|
4384
|
+
env
|
4339
4385
|
});
|
4340
|
-
const
|
4341
|
-
|
4386
|
+
const transferAmount = transactions2.reduce(
|
4387
|
+
(acc, tx) => {
|
4388
|
+
tx.actions.forEach((action) => {
|
4389
|
+
if (action.params.deposit) {
|
4390
|
+
const amount = Number(action.params.deposit) / __pow(10, currentConfig.nearTokenDecimals);
|
4391
|
+
console.log("near deposit amount:", amount);
|
4392
|
+
acc.near = acc.near.plus(amount);
|
4393
|
+
}
|
4394
|
+
if (tx.receiverId === currentConfig.btcToken && ["ft_transfer_call", "ft_transfer"].includes(action.params.methodName)) {
|
4395
|
+
const amount = Number(action.params.args.amount) / __pow(10, currentConfig.btcTokenDecimals);
|
4396
|
+
console.log("btc transfer amount:", amount);
|
4397
|
+
acc.btc = acc.btc.plus(amount);
|
4398
|
+
}
|
4399
|
+
});
|
4400
|
+
return acc;
|
4401
|
+
},
|
4402
|
+
{ near: new import_big2.default(0), btc: new import_big2.default(0) }
|
4403
|
+
);
|
4404
|
+
const nearAvailableBalance = new import_big2.default(nearBalance).minus(transferAmount.near).toNumber();
|
4405
|
+
const btcAvailableBalance = new import_big2.default(btcBalance).minus(transferAmount.btc).toNumber();
|
4406
|
+
if (btcAvailableBalance < 8e-6) {
|
4407
|
+
throw new Error("BTC balance is not enough, please deposit more BTC.");
|
4408
|
+
}
|
4409
|
+
console.log("available near balance:", nearAvailableBalance);
|
4410
|
+
console.log("available btc balance:", btcAvailableBalance);
|
4342
4411
|
console.log("available gas token balance:", gasTokenBalance);
|
4343
4412
|
const convertTx = yield Promise.all(
|
4344
4413
|
transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
|
4345
4414
|
);
|
4346
|
-
if (
|
4415
|
+
if (nearAvailableBalance > 0.5) {
|
4347
4416
|
console.log("near balance is enough, get the protocol fee of each transaction");
|
4348
4417
|
const gasTokens = yield nearCall2(
|
4349
4418
|
currentConfig.accountContractId,
|
4350
4419
|
"list_gas_token",
|
4351
|
-
{ token_ids: [currentConfig.
|
4420
|
+
{ token_ids: [currentConfig.btcToken] }
|
4352
4421
|
);
|
4353
4422
|
console.log("list_gas_token gas tokens:", gasTokens);
|
4354
4423
|
const perTxFee = Math.max(
|
4355
|
-
Number(((_a = gasTokens[currentConfig.
|
4424
|
+
Number(((_a = gasTokens[currentConfig.btcToken]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
|
4356
4425
|
100
|
4357
4426
|
);
|
4358
4427
|
console.log("perTxFee:", perTxFee);
|
@@ -4370,7 +4439,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4370
4439
|
console.log("near balance is not enough, predict the gas token amount required");
|
4371
4440
|
const adjustedGas = yield getPredictedGasAmount(
|
4372
4441
|
currentConfig.accountContractId,
|
4373
|
-
currentConfig.
|
4442
|
+
currentConfig.btcToken,
|
4374
4443
|
convertTx.map((t) => t.txHex)
|
4375
4444
|
);
|
4376
4445
|
if (new import_big2.default(gasTokenBalance).gte(adjustedGas)) {
|
@@ -4486,7 +4555,7 @@ function setupBTCWallet({
|
|
4486
4555
|
|
4487
4556
|
// src/index.ts
|
4488
4557
|
var getVersion = () => {
|
4489
|
-
return "0.5.
|
4558
|
+
return "0.5.12-beta";
|
4490
4559
|
};
|
4491
4560
|
if (typeof window !== "undefined") {
|
4492
4561
|
window.__BTC_WALLET_VERSION = getVersion();
|