btc-wallet 0.5.11-beta → 0.5.12-beta
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/config.d.ts +4 -1
- package/dist/core/btcUtils.d.ts +17 -9
- package/dist/index.js +224 -129
- 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 +224 -129
- package/esm/index.js.map +2 -2
- package/package.json +1 -1
package/dist/config.d.ts
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
export type ENV = 'mainnet' | 'testnet' | 'private_mainnet' | 'dev';
|
2
2
|
export declare const walletConfig: Record<ENV, {
|
3
3
|
base_url: string;
|
4
|
-
|
4
|
+
btcToken: string;
|
5
|
+
btcTokenDecimals: number;
|
6
|
+
nearToken: string;
|
7
|
+
nearTokenDecimals: number;
|
5
8
|
accountContractId: string;
|
6
9
|
bridgeContractId: string;
|
7
10
|
walletUrl: string;
|
package/dist/core/btcUtils.d.ts
CHANGED
@@ -2,7 +2,10 @@ import type { ENV } from '../config';
|
|
2
2
|
import type { FinalExecutionOutcome, Transaction } from '@near-wallet-selector/core';
|
3
3
|
export declare function getConfig(env: ENV): Promise<{
|
4
4
|
base_url: string;
|
5
|
-
|
5
|
+
btcToken: string;
|
6
|
+
btcTokenDecimals: number;
|
7
|
+
nearToken: string;
|
8
|
+
nearTokenDecimals: number;
|
6
9
|
accountContractId: string;
|
7
10
|
bridgeContractId: string;
|
8
11
|
walletUrl: string;
|
@@ -20,8 +23,19 @@ export interface AccountInfo {
|
|
20
23
|
amount?: string;
|
21
24
|
};
|
22
25
|
}
|
23
|
-
export declare function getAccountInfo(csna
|
24
|
-
|
26
|
+
export declare function getAccountInfo({ csna, env }: {
|
27
|
+
csna: string;
|
28
|
+
env: ENV;
|
29
|
+
}): Promise<AccountInfo | undefined>;
|
30
|
+
export declare function getTokenBalance({ csna, tokenId, env, }: {
|
31
|
+
csna: string;
|
32
|
+
tokenId: string;
|
33
|
+
env: ENV;
|
34
|
+
}): Promise<{
|
35
|
+
balance: number;
|
36
|
+
rawBalance: string;
|
37
|
+
}>;
|
38
|
+
export declare function checkGasTokenBalance(csna: string, minAmount: string, env: ENV): Promise<void>;
|
25
39
|
type CheckGasTokenDebtReturnType<T extends boolean> = T extends true ? void : {
|
26
40
|
receiver_id: string;
|
27
41
|
amount: string;
|
@@ -34,12 +48,6 @@ export declare function getBtcBalance(): Promise<{
|
|
34
48
|
balance: number;
|
35
49
|
availableBalance: number;
|
36
50
|
}>;
|
37
|
-
export declare function getNBTCBalance(address: string, env?: ENV): Promise<{
|
38
|
-
balance: number;
|
39
|
-
availableBalance: number;
|
40
|
-
rawBalance: string;
|
41
|
-
rawAvailableBalance: number;
|
42
|
-
}>;
|
43
51
|
export declare function sendBitcoin(address: string, amount: number, feeRate: number): Promise<string>;
|
44
52
|
/** estimate deposit receive amount, deduct protocol fee and repay amount */
|
45
53
|
export declare function estimateDepositAmount(amount: string, option?: {
|
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;
|
@@ -2870,7 +2884,10 @@ var import_js_sha256 = require("js-sha256");
|
|
2870
2884
|
var walletConfig = {
|
2871
2885
|
dev: {
|
2872
2886
|
base_url: "https://api.dev.satoshibridge.top",
|
2873
|
-
|
2887
|
+
btcToken: "nbtc-dev.testnet",
|
2888
|
+
btcTokenDecimals: 8,
|
2889
|
+
nearToken: "wrap.testnet",
|
2890
|
+
nearTokenDecimals: 24,
|
2874
2891
|
accountContractId: "acc-dev.testnet",
|
2875
2892
|
bridgeContractId: "brg-dev.testnet",
|
2876
2893
|
walletUrl: "https://wallet-dev.satoshibridge.top",
|
@@ -2878,7 +2895,10 @@ var walletConfig = {
|
|
2878
2895
|
},
|
2879
2896
|
testnet: {
|
2880
2897
|
base_url: "https://api.testnet.satoshibridge.top",
|
2881
|
-
|
2898
|
+
btcToken: "nbtc2-nsp.testnet",
|
2899
|
+
btcTokenDecimals: 8,
|
2900
|
+
nearToken: "wrap.testnet",
|
2901
|
+
nearTokenDecimals: 24,
|
2882
2902
|
accountContractId: "acc2-nsp.testnet",
|
2883
2903
|
bridgeContractId: "brg2-nsp.testnet",
|
2884
2904
|
walletUrl: "https://wallet-test.satoshibridge.top",
|
@@ -2886,7 +2906,10 @@ var walletConfig = {
|
|
2886
2906
|
},
|
2887
2907
|
private_mainnet: {
|
2888
2908
|
base_url: "https://api.stg.satoshibridge.top",
|
2889
|
-
|
2909
|
+
btcToken: "nbtc.toalice.near",
|
2910
|
+
btcTokenDecimals: 8,
|
2911
|
+
nearToken: "wrap.near",
|
2912
|
+
nearTokenDecimals: 24,
|
2890
2913
|
accountContractId: "acc.toalice.near",
|
2891
2914
|
bridgeContractId: "brg.toalice.near",
|
2892
2915
|
walletUrl: "https://wallet-stg.satoshibridge.top",
|
@@ -2894,7 +2917,10 @@ var walletConfig = {
|
|
2894
2917
|
},
|
2895
2918
|
mainnet: {
|
2896
2919
|
base_url: "https://api.satos.network",
|
2897
|
-
|
2920
|
+
btcToken: "nbtc.bridge.near",
|
2921
|
+
btcTokenDecimals: 8,
|
2922
|
+
nearToken: "wrap.near",
|
2923
|
+
nearTokenDecimals: 24,
|
2898
2924
|
accountContractId: "acc.ref-labs.near",
|
2899
2925
|
bridgeContractId: "btc-connector.bridge.near",
|
2900
2926
|
walletUrl: "https://wallet.satoshibridge.top",
|
@@ -2920,69 +2946,24 @@ var import_big = __toESM(require("big.js"), 1);
|
|
2920
2946
|
|
2921
2947
|
// src/utils/nearUtils.ts
|
2922
2948
|
var import_near_api_js = require("near-api-js");
|
2923
|
-
function nearCallFunction(contractId, methodName, args, options) {
|
2924
|
-
return __async(this, null, function* () {
|
2925
|
-
const nearProvider = (options == null ? void 0 : options.provider) || new import_near_api_js.providers.FailoverRpcProvider(
|
2926
|
-
nearRpcUrls[options == null ? void 0 : options.network].map(
|
2927
|
-
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2928
|
-
)
|
2929
|
-
);
|
2930
|
-
const res = yield nearProvider.query({
|
2931
|
-
request_type: "call_function",
|
2932
|
-
account_id: contractId,
|
2933
|
-
method_name: methodName,
|
2934
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2935
|
-
finality: "final"
|
2936
|
-
});
|
2937
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
2938
|
-
});
|
2939
|
-
}
|
2940
|
-
function pollTransactionStatuses(network, hashes) {
|
2941
|
-
return __async(this, null, function* () {
|
2942
|
-
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
2943
|
-
Object.values(nearRpcUrls[network]).map(
|
2944
|
-
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2945
|
-
)
|
2946
|
-
);
|
2947
|
-
const maxAttempts = 30;
|
2948
|
-
let currentAttempt = 0;
|
2949
|
-
const pendingHashes = new Set(hashes);
|
2950
|
-
const results = /* @__PURE__ */ new Map();
|
2951
|
-
while (pendingHashes.size > 0 && currentAttempt < maxAttempts) {
|
2952
|
-
currentAttempt++;
|
2953
|
-
const promises = Array.from(pendingHashes).map((hash) => __async(this, null, function* () {
|
2954
|
-
try {
|
2955
|
-
const result2 = yield provider.txStatus(hash, "unused", "FINAL");
|
2956
|
-
if (result2 && result2.status) {
|
2957
|
-
console.log(`Transaction ${hash} result:`, result2);
|
2958
|
-
results.set(hash, result2);
|
2959
|
-
pendingHashes.delete(hash);
|
2960
|
-
}
|
2961
|
-
} catch (error) {
|
2962
|
-
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
2963
|
-
}
|
2964
|
-
}));
|
2965
|
-
yield Promise.all(promises);
|
2966
|
-
if (pendingHashes.size > 0) {
|
2967
|
-
if (currentAttempt === maxAttempts) {
|
2968
|
-
throw new Error(
|
2969
|
-
`Transactions not found after max attempts: ${Array.from(pendingHashes).join(", ")}`
|
2970
|
-
);
|
2971
|
-
}
|
2972
|
-
console.log(
|
2973
|
-
`Waiting for ${pendingHashes.size} transactions, retrying ${maxAttempts - currentAttempt} more times`
|
2974
|
-
);
|
2975
|
-
yield delay(1e4);
|
2976
|
-
}
|
2977
|
-
}
|
2978
|
-
const result = hashes.map((hash) => results.get(hash)).filter(Boolean);
|
2979
|
-
return result;
|
2980
|
-
});
|
2981
|
-
}
|
2982
2949
|
|
2983
2950
|
// src/utils/request.ts
|
2984
2951
|
var cache = /* @__PURE__ */ new Map();
|
2985
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
|
+
}
|
2986
2967
|
function request(url, options) {
|
2987
2968
|
return __async(this, null, function* () {
|
2988
2969
|
var _a;
|
@@ -3024,13 +3005,7 @@ function request(url, options) {
|
|
3024
3005
|
if (options.shouldStopPolling(data)) {
|
3025
3006
|
return data;
|
3026
3007
|
}
|
3027
|
-
|
3028
|
-
yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
|
3029
|
-
return request(url, __spreadProps(__spreadValues({}, options), {
|
3030
|
-
maxPollingAttempts: options.maxPollingAttempts - 1
|
3031
|
-
}));
|
3032
|
-
}
|
3033
|
-
throw new Error("Polling failed: maximum attempts reached without meeting the condition");
|
3008
|
+
throw new Error("Polling should continue");
|
3034
3009
|
}
|
3035
3010
|
if (cacheKey) {
|
3036
3011
|
cache.set(cacheKey, { timestamp: Date.now(), data });
|
@@ -3040,6 +3015,7 @@ function request(url, options) {
|
|
3040
3015
|
}
|
3041
3016
|
return data;
|
3042
3017
|
} catch (err) {
|
3018
|
+
console.error(err);
|
3043
3019
|
if (retryCount > 0) {
|
3044
3020
|
console.log(`Retrying... attempts left: ${retryCount}`);
|
3045
3021
|
return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount - 1 }));
|
@@ -3053,8 +3029,85 @@ function request(url, options) {
|
|
3053
3029
|
}));
|
3054
3030
|
}
|
3055
3031
|
}
|
3056
|
-
|
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
|
+
}
|
3057
3108
|
}
|
3109
|
+
const result = hashes.map((hash) => results.get(hash)).filter(Boolean);
|
3110
|
+
return result;
|
3058
3111
|
});
|
3059
3112
|
}
|
3060
3113
|
|
@@ -3211,9 +3264,10 @@ function nearCall(contractId, methodName, args) {
|
|
3211
3264
|
return nearCallFunction(contractId, methodName, args, { network });
|
3212
3265
|
});
|
3213
3266
|
}
|
3214
|
-
function getAccountInfo(
|
3215
|
-
return __async(this,
|
3216
|
-
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", {
|
3217
3271
|
account_id: csna
|
3218
3272
|
}).catch((error) => {
|
3219
3273
|
return void 0;
|
@@ -3222,16 +3276,46 @@ function getAccountInfo(csna, accountContractId) {
|
|
3222
3276
|
return accountInfo;
|
3223
3277
|
});
|
3224
3278
|
}
|
3225
|
-
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) {
|
3226
3310
|
return __async(this, null, function* () {
|
3227
|
-
const
|
3228
|
-
|
3229
|
-
|
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)) {
|
3230
3315
|
yield Dialog.confirm({
|
3231
3316
|
title: "Gas token balance is insufficient",
|
3232
3317
|
message: "Please deposit gas token to continue, will open bridge website."
|
3233
3318
|
});
|
3234
|
-
const config = yield getConfig(env);
|
3235
3319
|
window.open(config.bridgeUrl, "_blank");
|
3236
3320
|
throw new Error("Gas token balance is insufficient");
|
3237
3321
|
}
|
@@ -3312,18 +3396,6 @@ function getBtcBalance() {
|
|
3312
3396
|
};
|
3313
3397
|
});
|
3314
3398
|
}
|
3315
|
-
function getNBTCBalance(address, env) {
|
3316
|
-
return __async(this, null, function* () {
|
3317
|
-
const config = yield getConfig(env || "mainnet");
|
3318
|
-
const rawBalance = yield nearCall(config.token, "ft_balance_of", {
|
3319
|
-
account_id: address
|
3320
|
-
});
|
3321
|
-
const balance = new import_big.default(rawBalance).div(__pow(10, 8)).round(8, import_big.default.roundDown).toNumber();
|
3322
|
-
const rawAvailableBalance = new import_big.default(rawBalance).minus(1e3).toNumber();
|
3323
|
-
const availableBalance = new import_big.default(rawAvailableBalance).div(__pow(10, 8)).round(8, import_big.default.roundDown).toNumber();
|
3324
|
-
return { balance, availableBalance, rawBalance, rawAvailableBalance };
|
3325
|
-
});
|
3326
|
-
}
|
3327
3399
|
function sendBitcoin(address, amount, feeRate) {
|
3328
3400
|
return __async(this, null, function* () {
|
3329
3401
|
const { sendBitcoin: sendBitcoin2 } = getBtcProvider();
|
@@ -3343,7 +3415,7 @@ function getDepositAmount(amount, option) {
|
|
3343
3415
|
const _newAccountMinDepositAmount = (_a = option == null ? void 0 : option.newAccountMinDepositAmount) != null ? _a : true;
|
3344
3416
|
const config = yield getConfig(env);
|
3345
3417
|
const csna = yield getCsnaAccountId(env);
|
3346
|
-
const accountInfo = yield getAccountInfo(csna,
|
3418
|
+
const accountInfo = yield getAccountInfo({ csna, env });
|
3347
3419
|
const debtAction = yield checkGasTokenDebt(accountInfo, env, false);
|
3348
3420
|
const repayAmount = (debtAction == null ? void 0 : debtAction.amount) || 0;
|
3349
3421
|
const {
|
@@ -3422,7 +3494,7 @@ function executeBTCDepositAndAction(_0) {
|
|
3422
3494
|
env,
|
3423
3495
|
newAccountMinDepositAmount
|
3424
3496
|
});
|
3425
|
-
const accountInfo = yield getAccountInfo(csna,
|
3497
|
+
const accountInfo = yield getAccountInfo({ csna, env });
|
3426
3498
|
const newActions = [];
|
3427
3499
|
const debtAction = yield checkGasTokenDebt(accountInfo, env, false);
|
3428
3500
|
if (debtAction) {
|
@@ -3436,12 +3508,12 @@ function executeBTCDepositAndAction(_0) {
|
|
3436
3508
|
}));
|
3437
3509
|
}
|
3438
3510
|
const storageDepositMsg = {};
|
3439
|
-
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", {
|
3440
3512
|
account_id: csna
|
3441
3513
|
});
|
3442
3514
|
if (!(registerRes == null ? void 0 : registerRes.available)) {
|
3443
3515
|
storageDepositMsg.storage_deposit_msg = {
|
3444
|
-
contract_id: (action == null ? void 0 : action.receiver_id) || config.
|
3516
|
+
contract_id: (action == null ? void 0 : action.receiver_id) || config.btcToken,
|
3445
3517
|
deposit: registerDeposit || NEAR_STORAGE_DEPOSIT_AMOUNT,
|
3446
3518
|
registration_only: true
|
3447
3519
|
};
|
@@ -3647,7 +3719,7 @@ function getWithdrawTransaction(_0) {
|
|
3647
3719
|
};
|
3648
3720
|
const csna = yield getCsnaAccountId(env);
|
3649
3721
|
const transaction = {
|
3650
|
-
receiverId: config.
|
3722
|
+
receiverId: config.btcToken,
|
3651
3723
|
signerId: csna,
|
3652
3724
|
actions: [
|
3653
3725
|
{
|
@@ -4191,20 +4263,16 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4191
4263
|
throw new Error("Wallet state is invalid, please reconnect your wallet.");
|
4192
4264
|
}
|
4193
4265
|
const btcContext = window.btcContext;
|
4194
|
-
const
|
4195
|
-
const accountInfo = yield getAccountInfo(
|
4266
|
+
const csna = state.getAccount();
|
4267
|
+
const accountInfo = yield getAccountInfo({ csna, env });
|
4196
4268
|
yield checkGasTokenDebt(accountInfo, env, true);
|
4197
4269
|
const trans = [...params.transactions];
|
4198
4270
|
console.log("signAndSendTransactions raw trans:", trans);
|
4199
|
-
const
|
4200
|
-
const { transferGasTransaction, useNearPayGas, gasLimit } = yield calculateGasStrategy(
|
4201
|
-
gasTokenBalance,
|
4202
|
-
trans
|
4203
|
-
);
|
4271
|
+
const { transferGasTransaction, useNearPayGas, gasLimit } = yield calculateGasStrategy(trans);
|
4204
4272
|
console.log("transferGasTransaction:", transferGasTransaction);
|
4205
4273
|
console.log("useNearPayGas:", useNearPayGas);
|
4206
4274
|
console.log("gasLimit:", gasLimit);
|
4207
|
-
yield checkGasTokenBalance(
|
4275
|
+
yield checkGasTokenBalance(csna, gasLimit, env);
|
4208
4276
|
if (transferGasTransaction) {
|
4209
4277
|
trans.unshift(transferGasTransaction);
|
4210
4278
|
}
|
@@ -4212,14 +4280,14 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4212
4280
|
const newTrans = yield Promise.all(
|
4213
4281
|
trans.map((transaction, index) => convertTransactionToTxHex(transaction, index))
|
4214
4282
|
);
|
4215
|
-
const nonceFromApi = yield getNonce(currentConfig.base_url,
|
4283
|
+
const nonceFromApi = yield getNonce(currentConfig.base_url, csna);
|
4216
4284
|
const nonceFromContract = (accountInfo == null ? void 0 : accountInfo.nonce) || 0;
|
4217
4285
|
const nonce = Number(nonceFromApi) > Number(nonceFromContract) ? String(nonceFromApi) : String(nonceFromContract);
|
4218
4286
|
const intention = {
|
4219
4287
|
chain_id: "397",
|
4220
|
-
csna
|
4288
|
+
csna,
|
4221
4289
|
near_transactions: newTrans.map((t) => t.txHex),
|
4222
|
-
gas_token: currentConfig.
|
4290
|
+
gas_token: currentConfig.btcToken,
|
4223
4291
|
gas_limit: gasLimit,
|
4224
4292
|
use_near_pay_gas: useNearPayGas,
|
4225
4293
|
nonce
|
@@ -4232,7 +4300,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4232
4300
|
data: toHex(strIntention)
|
4233
4301
|
});
|
4234
4302
|
yield checkBtcTransactionStatus(currentConfig.base_url, signature);
|
4235
|
-
const hash = newTrans.map((t) => t.hash);
|
4303
|
+
const hash = newTrans.slice(1).map((t) => t.hash);
|
4236
4304
|
console.log("txHash:", hash);
|
4237
4305
|
const result = yield pollTransactionStatuses(options.network.networkId, hash);
|
4238
4306
|
return result;
|
@@ -4240,12 +4308,9 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4240
4308
|
}
|
4241
4309
|
function calculateGasLimit(params) {
|
4242
4310
|
return __async(this, null, function* () {
|
4243
|
-
const accountId = state.getAccount();
|
4244
|
-
const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
|
4245
4311
|
const trans = [...params.transactions];
|
4246
4312
|
console.log("raw trans:", trans);
|
4247
|
-
const
|
4248
|
-
const { gasLimit } = yield calculateGasStrategy(gasTokenBalance, trans);
|
4313
|
+
const { gasLimit } = yield calculateGasStrategy(trans);
|
4249
4314
|
return gasLimit;
|
4250
4315
|
});
|
4251
4316
|
}
|
@@ -4253,7 +4318,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4253
4318
|
return __async(this, null, function* () {
|
4254
4319
|
return {
|
4255
4320
|
signerId: accountId,
|
4256
|
-
receiverId: currentConfig.
|
4321
|
+
receiverId: currentConfig.btcToken,
|
4257
4322
|
actions: [
|
4258
4323
|
{
|
4259
4324
|
type: "FunctionCall",
|
@@ -4281,7 +4346,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4281
4346
|
} else {
|
4282
4347
|
newGasLimit = yield getPredictedGasAmount(
|
4283
4348
|
currentConfig.accountContractId,
|
4284
|
-
currentConfig.
|
4349
|
+
currentConfig.btcToken,
|
4285
4350
|
[transferTxHex, ...transactions2.map((t) => t.txHex)]
|
4286
4351
|
);
|
4287
4352
|
}
|
@@ -4302,31 +4367,61 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4302
4367
|
return gasAmount.toString();
|
4303
4368
|
});
|
4304
4369
|
}
|
4305
|
-
function calculateGasStrategy(
|
4370
|
+
function calculateGasStrategy(transactions2) {
|
4306
4371
|
return __async(this, null, function* () {
|
4307
4372
|
var _a;
|
4308
4373
|
const accountId = state.getAccount();
|
4309
|
-
const
|
4310
|
-
|
4311
|
-
|
4312
|
-
|
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
|
4313
4380
|
});
|
4314
|
-
const
|
4315
|
-
|
4381
|
+
const { balance: btcBalance } = yield getTokenBalance({
|
4382
|
+
csna: accountId,
|
4383
|
+
tokenId: currentConfig.btcToken,
|
4384
|
+
env
|
4385
|
+
});
|
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);
|
4316
4411
|
console.log("available gas token balance:", gasTokenBalance);
|
4317
4412
|
const convertTx = yield Promise.all(
|
4318
4413
|
transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
|
4319
4414
|
);
|
4320
|
-
if (
|
4415
|
+
if (nearAvailableBalance > 0.5) {
|
4321
4416
|
console.log("near balance is enough, get the protocol fee of each transaction");
|
4322
4417
|
const gasTokens = yield nearCall2(
|
4323
4418
|
currentConfig.accountContractId,
|
4324
4419
|
"list_gas_token",
|
4325
|
-
{ token_ids: [currentConfig.
|
4420
|
+
{ token_ids: [currentConfig.btcToken] }
|
4326
4421
|
);
|
4327
4422
|
console.log("list_gas_token gas tokens:", gasTokens);
|
4328
4423
|
const perTxFee = Math.max(
|
4329
|
-
Number(((_a = gasTokens[currentConfig.
|
4424
|
+
Number(((_a = gasTokens[currentConfig.btcToken]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
|
4330
4425
|
100
|
4331
4426
|
);
|
4332
4427
|
console.log("perTxFee:", perTxFee);
|
@@ -4344,7 +4439,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
4344
4439
|
console.log("near balance is not enough, predict the gas token amount required");
|
4345
4440
|
const adjustedGas = yield getPredictedGasAmount(
|
4346
4441
|
currentConfig.accountContractId,
|
4347
|
-
currentConfig.
|
4442
|
+
currentConfig.btcToken,
|
4348
4443
|
convertTx.map((t) => t.txHex)
|
4349
4444
|
);
|
4350
4445
|
if (new import_big2.default(gasTokenBalance).gte(adjustedGas)) {
|
@@ -4460,7 +4555,7 @@ function setupBTCWallet({
|
|
4460
4555
|
|
4461
4556
|
// src/index.ts
|
4462
4557
|
var getVersion = () => {
|
4463
|
-
return "0.5.
|
4558
|
+
return "0.5.12-beta";
|
4464
4559
|
};
|
4465
4560
|
if (typeof window !== "undefined") {
|
4466
4561
|
window.__BTC_WALLET_VERSION = getVersion();
|