btc-wallet 0.5.2-beta → 0.5.4-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.
@@ -1,5 +1,13 @@
1
1
  import type { ENV } from '../config';
2
2
  import type { FinalExecutionOutcome, Transaction } from '@near-wallet-selector/core';
3
+ export declare function getConfig(env: ENV): Promise<{
4
+ base_url: string;
5
+ token: string;
6
+ accountContractId: string;
7
+ bridgeContractId: string;
8
+ walletUrl: string;
9
+ bridgeUrl: string;
10
+ }>;
3
11
  export interface AccountInfo {
4
12
  nonce: string;
5
13
  gas_token: Record<string, string>;
package/dist/index.js CHANGED
@@ -99,6 +99,7 @@ __export(src_exports, {
99
99
  getAccountInfo: () => getAccountInfo,
100
100
  getBtcBalance: () => getBtcBalance,
101
101
  getBtcGasPrice: () => getBtcGasPrice,
102
+ getConfig: () => getConfig,
102
103
  getCsnaAccountId: () => getCsnaAccountId,
103
104
  getDepositAmount: () => getDepositAmount,
104
105
  getNBTCBalance: () => getNBTCBalance,
@@ -2951,231 +2952,8 @@ var btcRpcUrls = {
2951
2952
  testnet: "https://mempool.space/testnet/api"
2952
2953
  };
2953
2954
 
2954
- // src/utils/initWalletButton.ts
2955
- function setupWalletButton(env, wallet, originalWallet) {
2956
- console.log("setupWalletButton");
2957
- if (document.getElementById("satoshi-wallet-button")) {
2958
- return;
2959
- }
2960
- const iframe = createIframe({
2961
- iframeUrl: walletConfig[env].walletUrl,
2962
- iframeStyle: { width: "400px", height: "650px" }
2963
- });
2964
- iframe.addEventListener("mouseenter", () => {
2965
- var _a;
2966
- if (document.activeElement !== iframe) {
2967
- (_a = document.activeElement) == null ? void 0 : _a.setAttribute("tabindex", "null");
2968
- setTimeout(() => {
2969
- iframe.focus();
2970
- }, 0);
2971
- }
2972
- });
2973
- const button = createFloatingButtonWithIframe({
2974
- openImageUrl: "https://assets.deltatrade.ai/wallet-assets/wallet-btn.png",
2975
- closeImageUrl: "https://assets.deltatrade.ai/wallet-assets/wallet-btn-active.png",
2976
- iframe
2977
- });
2978
- setupButtonClickHandler(button, iframe, wallet, originalWallet);
2979
- }
2980
- function createFloatingButtonWithIframe({
2981
- openImageUrl,
2982
- closeImageUrl,
2983
- iframe
2984
- }) {
2985
- const button = document.createElement("img");
2986
- button.id = "satoshi-wallet-button";
2987
- const isIframeVisible = localStorage.getItem("btc-wallet-iframe-visible") === "true";
2988
- button.src = isIframeVisible ? closeImageUrl : openImageUrl;
2989
- iframe.style.display = isIframeVisible ? "block" : "none";
2990
- const windowWidth = window.innerWidth;
2991
- const windowHeight = window.innerHeight;
2992
- const savedPosition = JSON.parse(
2993
- localStorage.getItem("btc-wallet-button-position") || '{"right": "20px", "bottom": "20px"}'
2994
- );
2995
- const right = Math.min(Math.max(20, parseInt(savedPosition.right)), windowWidth - 80);
2996
- const bottom = Math.min(Math.max(20, parseInt(savedPosition.bottom)), windowHeight - 80);
2997
- Object.assign(button.style, {
2998
- position: "fixed",
2999
- bottom: `${bottom}px`,
3000
- right: `${right}px`,
3001
- zIndex: "100000",
3002
- width: "60px",
3003
- height: "60px",
3004
- borderRadius: "50%",
3005
- cursor: "grab",
3006
- transition: "transform 0.15s ease",
3007
- userSelect: "none"
3008
- });
3009
- document.body.appendChild(button);
3010
- updateIframePosition(iframe, right, bottom, windowWidth, windowHeight);
3011
- let isDragging = false;
3012
- let startX = 0;
3013
- let startY = 0;
3014
- let initialRight = 0;
3015
- let initialBottom = 0;
3016
- let dragStartTime = 0;
3017
- button.addEventListener("mousedown", (e) => {
3018
- isDragging = true;
3019
- startX = e.clientX;
3020
- startY = e.clientY;
3021
- initialRight = parseInt(button.style.right);
3022
- initialBottom = parseInt(button.style.bottom);
3023
- dragStartTime = Date.now();
3024
- button.style.cursor = "grabbing";
3025
- button.style.transition = "none";
3026
- e.preventDefault();
3027
- });
3028
- document.addEventListener("mousemove", (e) => {
3029
- if (!isDragging)
3030
- return;
3031
- const deltaX = startX - e.clientX;
3032
- const deltaY = startY - e.clientY;
3033
- let newRight = initialRight + deltaX;
3034
- let newBottom = initialBottom + deltaY;
3035
- newRight = Math.min(Math.max(20, newRight), windowWidth - 80);
3036
- newBottom = Math.min(Math.max(20, newBottom), windowHeight - 80);
3037
- const snapThreshold = 20;
3038
- const buttonLeft = windowWidth - newRight - 60;
3039
- if (buttonLeft < snapThreshold) {
3040
- newRight = windowWidth - 80;
3041
- } else if (buttonLeft > windowWidth - snapThreshold - 60) {
3042
- newRight = 20;
3043
- }
3044
- if (newBottom < snapThreshold) {
3045
- newBottom = 20;
3046
- } else if (newBottom > windowHeight - snapThreshold - 60) {
3047
- newBottom = windowHeight - 80;
3048
- }
3049
- button.style.right = `${newRight}px`;
3050
- button.style.bottom = `${newBottom}px`;
3051
- updateIframePosition(iframe, newRight, newBottom, windowWidth, windowHeight);
3052
- });
3053
- document.addEventListener("mouseup", () => {
3054
- if (!isDragging)
3055
- return;
3056
- const dragEndTime = Date.now();
3057
- const isDragEvent = dragEndTime - dragStartTime > 200;
3058
- isDragging = false;
3059
- button.style.cursor = "grab";
3060
- button.style.transition = "transform 0.15s ease";
3061
- localStorage.setItem(
3062
- "btc-wallet-button-position",
3063
- JSON.stringify({
3064
- right: button.style.right,
3065
- bottom: button.style.bottom
3066
- })
3067
- );
3068
- if (!isDragEvent) {
3069
- handleButtonClick();
3070
- }
3071
- });
3072
- const handleButtonClick = () => {
3073
- const isCurrentlyVisible = iframe.style.display === "block";
3074
- button.style.transform = "scale(0.8)";
3075
- setTimeout(() => {
3076
- button.style.transform = "scale(1)";
3077
- }, 150);
3078
- const newVisibleState = !isCurrentlyVisible;
3079
- iframe.style.display = newVisibleState ? "block" : "none";
3080
- button.src = newVisibleState ? closeImageUrl : openImageUrl;
3081
- localStorage.setItem("btc-wallet-iframe-visible", String(newVisibleState));
3082
- setTimeout(() => {
3083
- if (newVisibleState) {
3084
- iframe.focus();
3085
- }
3086
- }, 0);
3087
- };
3088
- button.onclick = null;
3089
- return button;
3090
- }
3091
- function createIframe({
3092
- iframeUrl,
3093
- iframeStyle = {}
3094
- }) {
3095
- const iframe = document.createElement("iframe");
3096
- iframe.id = "satoshi-wallet-iframe";
3097
- iframe.allow = "clipboard-read; clipboard-write";
3098
- iframe.src = iframeUrl;
3099
- const isVisible = localStorage.getItem("btc-wallet-iframe-visible") === "true";
3100
- Object.assign(iframe.style, __spreadValues({
3101
- position: "fixed",
3102
- bottom: "90px",
3103
- right: "20px",
3104
- zIndex: "100000",
3105
- boxShadow: "0 0 10px rgba(0, 0, 0, 0.1)",
3106
- borderRadius: "10px",
3107
- display: isVisible ? "block" : "none",
3108
- border: "none"
3109
- }, iframeStyle));
3110
- document.body.appendChild(iframe);
3111
- return iframe;
3112
- }
3113
- function setupButtonClickHandler(button, iframe, wallet, originalWallet) {
3114
- return __async(this, null, function* () {
3115
- var _a;
3116
- const accountId = (_a = yield wallet == null ? void 0 : wallet.getAccounts()) == null ? void 0 : _a[0].accountId;
3117
- const originalAccountId = originalWallet.account;
3118
- const originalPublicKey = yield originalWallet.getPublicKey();
3119
- console.log({ accountId, originalAccountId, originalPublicKey });
3120
- const iframeSrc = new URL(iframe.src);
3121
- iframeSrc.searchParams.set("origin", window.location.origin);
3122
- accountId && iframeSrc.searchParams.set("accountId", accountId);
3123
- originalAccountId && iframeSrc.searchParams.set("originalAccountId", originalAccountId);
3124
- originalPublicKey && iframeSrc.searchParams.set("originalPublicKey", originalPublicKey);
3125
- iframe.src = iframeSrc.toString();
3126
- window.addEventListener("message", (event) => __async(this, null, function* () {
3127
- var _a2, _b;
3128
- if (event.origin !== iframeSrc.origin)
3129
- return;
3130
- const { action, requestId, data } = event.data;
3131
- if (action === "signAndSendTransaction") {
3132
- console.log("signAndSendTransaction message", event.data);
3133
- try {
3134
- const result = yield wallet.signAndSendTransaction(data);
3135
- console.log("signAndSendTransaction result", result);
3136
- (_a2 = event.source) == null ? void 0 : _a2.postMessage(
3137
- {
3138
- requestId,
3139
- data,
3140
- success: true
3141
- },
3142
- { targetOrigin: event.origin }
3143
- );
3144
- } catch (error) {
3145
- console.error("signAndSendTransaction error", error);
3146
- (_b = event.source) == null ? void 0 : _b.postMessage(
3147
- {
3148
- requestId,
3149
- error: error.message,
3150
- success: false
3151
- },
3152
- { targetOrigin: event.origin }
3153
- );
3154
- }
3155
- }
3156
- }));
3157
- });
3158
- }
3159
- function removeWalletButton() {
3160
- const button = document.getElementById("satoshi-wallet-button");
3161
- button == null ? void 0 : button.remove();
3162
- const iframe = document.getElementById("satoshi-wallet-iframe");
3163
- iframe == null ? void 0 : iframe.remove();
3164
- }
3165
- function updateIframePosition(iframe, buttonRight, buttonBottom, windowWidth, windowHeight) {
3166
- const iframeWidth = parseInt(iframe.style.width);
3167
- const iframeHeight = parseInt(iframe.style.height);
3168
- let iframeRight = buttonRight;
3169
- let iframeBottom = buttonBottom + 70;
3170
- if (iframeRight + iframeWidth > windowWidth - 20) {
3171
- iframeRight = Math.max(20, windowWidth - iframeWidth - 20);
3172
- }
3173
- if (iframeBottom + iframeHeight > windowHeight - 20) {
3174
- iframeBottom = Math.max(20, buttonBottom - iframeHeight - 10);
3175
- }
3176
- iframe.style.right = `${iframeRight}px`;
3177
- iframe.style.bottom = `${iframeBottom}px`;
3178
- }
2955
+ // src/core/btcUtils.ts
2956
+ var import_big = __toESM(require("big.js"), 1);
3179
2957
 
3180
2958
  // src/utils/nearUtils.ts
3181
2959
  var import_near_api_js = require("near-api-js");
@@ -3239,12 +3017,6 @@ function pollTransactionStatuses(network, hashes) {
3239
3017
  });
3240
3018
  }
3241
3019
 
3242
- // src/core/setupBTCWallet.ts
3243
- var import_big2 = __toESM(require("big.js"), 1);
3244
-
3245
- // src/core/btcUtils.ts
3246
- var import_big = __toESM(require("big.js"), 1);
3247
-
3248
3020
  // src/utils/request.ts
3249
3021
  var cache = /* @__PURE__ */ new Map();
3250
3022
  var defaultCacheTimeout = 3e3;
@@ -3447,11 +3219,10 @@ var NBTC_STORAGE_DEPOSIT_AMOUNT = "3000";
3447
3219
  var GAS_LIMIT = "50000000000000";
3448
3220
  var NEW_ACCOUNT_MIN_DEPOSIT_AMOUNT = "1000";
3449
3221
  function getBtcProvider() {
3450
- var _a, _b;
3451
- if (typeof window === "undefined" || !window.btcContext && !((_a = window.top) == null ? void 0 : _a.btcContext)) {
3222
+ if (typeof window === "undefined" || !window.btcContext) {
3452
3223
  throw new Error("BTC Provider is not initialized.");
3453
3224
  }
3454
- return window.btcContext || ((_b = window.top) == null ? void 0 : _b.btcContext);
3225
+ return window.btcContext;
3455
3226
  }
3456
3227
  function getNetwork() {
3457
3228
  return __async(this, null, function* () {
@@ -3514,7 +3285,6 @@ function checkGasTokenDebt(accountInfo, env, autoDeposit) {
3514
3285
  return;
3515
3286
  const config = yield getConfig(env);
3516
3287
  const transferAmount = hasDebtArrears ? debtAmount : relayerFeeAmount;
3517
- console.log("get_account:", accountInfo);
3518
3288
  const action = {
3519
3289
  receiver_id: config.accountContractId,
3520
3290
  amount: transferAmount.toString(),
@@ -3925,7 +3695,239 @@ function uint8ArrayToHex(uint8Array) {
3925
3695
  return Array.from(uint8Array).map((byte) => byte.toString(16).padStart(2, "0")).join("");
3926
3696
  }
3927
3697
 
3698
+ // src/utils/initWalletButton.ts
3699
+ function setupWalletButton(env, wallet, originalWallet) {
3700
+ console.log("setupWalletButton");
3701
+ if (document.getElementById("satoshi-wallet-button")) {
3702
+ return;
3703
+ }
3704
+ const iframe = createIframe({
3705
+ iframeUrl: walletConfig[env].walletUrl,
3706
+ iframeStyle: { width: "400px", height: "650px" }
3707
+ });
3708
+ iframe.addEventListener("mouseenter", () => {
3709
+ var _a;
3710
+ if (document.activeElement !== iframe) {
3711
+ (_a = document.activeElement) == null ? void 0 : _a.setAttribute("tabindex", "null");
3712
+ setTimeout(() => {
3713
+ iframe.focus();
3714
+ }, 0);
3715
+ }
3716
+ });
3717
+ const button = createFloatingButtonWithIframe({
3718
+ openImageUrl: "https://assets.deltatrade.ai/wallet-assets/wallet-btn.png",
3719
+ closeImageUrl: "https://assets.deltatrade.ai/wallet-assets/wallet-btn-active.png",
3720
+ iframe
3721
+ });
3722
+ setupButtonClickHandler(button, iframe, wallet, originalWallet);
3723
+ }
3724
+ function createFloatingButtonWithIframe({
3725
+ openImageUrl,
3726
+ closeImageUrl,
3727
+ iframe
3728
+ }) {
3729
+ const button = document.createElement("img");
3730
+ button.id = "satoshi-wallet-button";
3731
+ const isIframeVisible = localStorage.getItem("btc-wallet-iframe-visible") === "true";
3732
+ button.src = isIframeVisible ? closeImageUrl : openImageUrl;
3733
+ iframe.style.display = isIframeVisible ? "block" : "none";
3734
+ const windowWidth = window.innerWidth;
3735
+ const windowHeight = window.innerHeight;
3736
+ const savedPosition = JSON.parse(
3737
+ localStorage.getItem("btc-wallet-button-position") || '{"right": "20px", "bottom": "20px"}'
3738
+ );
3739
+ const right = Math.min(Math.max(20, parseInt(savedPosition.right)), windowWidth - 80);
3740
+ const bottom = Math.min(Math.max(20, parseInt(savedPosition.bottom)), windowHeight - 80);
3741
+ Object.assign(button.style, {
3742
+ position: "fixed",
3743
+ bottom: `${bottom}px`,
3744
+ right: `${right}px`,
3745
+ zIndex: "100000",
3746
+ width: "60px",
3747
+ height: "60px",
3748
+ borderRadius: "50%",
3749
+ cursor: "grab",
3750
+ transition: "transform 0.15s ease",
3751
+ userSelect: "none"
3752
+ });
3753
+ document.body.appendChild(button);
3754
+ updateIframePosition(iframe, right, bottom, windowWidth, windowHeight);
3755
+ let isDragging = false;
3756
+ let startX = 0;
3757
+ let startY = 0;
3758
+ let initialRight = 0;
3759
+ let initialBottom = 0;
3760
+ let dragStartTime = 0;
3761
+ button.addEventListener("mousedown", (e) => {
3762
+ isDragging = true;
3763
+ startX = e.clientX;
3764
+ startY = e.clientY;
3765
+ initialRight = parseInt(button.style.right);
3766
+ initialBottom = parseInt(button.style.bottom);
3767
+ dragStartTime = Date.now();
3768
+ button.style.cursor = "grabbing";
3769
+ button.style.transition = "none";
3770
+ e.preventDefault();
3771
+ });
3772
+ document.addEventListener("mousemove", (e) => {
3773
+ if (!isDragging)
3774
+ return;
3775
+ const deltaX = startX - e.clientX;
3776
+ const deltaY = startY - e.clientY;
3777
+ let newRight = initialRight + deltaX;
3778
+ let newBottom = initialBottom + deltaY;
3779
+ newRight = Math.min(Math.max(20, newRight), windowWidth - 80);
3780
+ newBottom = Math.min(Math.max(20, newBottom), windowHeight - 80);
3781
+ const snapThreshold = 20;
3782
+ const buttonLeft = windowWidth - newRight - 60;
3783
+ if (buttonLeft < snapThreshold) {
3784
+ newRight = windowWidth - 80;
3785
+ } else if (buttonLeft > windowWidth - snapThreshold - 60) {
3786
+ newRight = 20;
3787
+ }
3788
+ if (newBottom < snapThreshold) {
3789
+ newBottom = 20;
3790
+ } else if (newBottom > windowHeight - snapThreshold - 60) {
3791
+ newBottom = windowHeight - 80;
3792
+ }
3793
+ button.style.right = `${newRight}px`;
3794
+ button.style.bottom = `${newBottom}px`;
3795
+ updateIframePosition(iframe, newRight, newBottom, windowWidth, windowHeight);
3796
+ });
3797
+ document.addEventListener("mouseup", () => {
3798
+ if (!isDragging)
3799
+ return;
3800
+ const dragEndTime = Date.now();
3801
+ const isDragEvent = dragEndTime - dragStartTime > 200;
3802
+ isDragging = false;
3803
+ button.style.cursor = "grab";
3804
+ button.style.transition = "transform 0.15s ease";
3805
+ localStorage.setItem(
3806
+ "btc-wallet-button-position",
3807
+ JSON.stringify({
3808
+ right: button.style.right,
3809
+ bottom: button.style.bottom
3810
+ })
3811
+ );
3812
+ if (!isDragEvent) {
3813
+ handleButtonClick();
3814
+ }
3815
+ });
3816
+ const handleButtonClick = () => {
3817
+ const isCurrentlyVisible = iframe.style.display === "block";
3818
+ button.style.transform = "scale(0.8)";
3819
+ setTimeout(() => {
3820
+ button.style.transform = "scale(1)";
3821
+ }, 150);
3822
+ const newVisibleState = !isCurrentlyVisible;
3823
+ iframe.style.display = newVisibleState ? "block" : "none";
3824
+ button.src = newVisibleState ? closeImageUrl : openImageUrl;
3825
+ localStorage.setItem("btc-wallet-iframe-visible", String(newVisibleState));
3826
+ setTimeout(() => {
3827
+ if (newVisibleState) {
3828
+ iframe.focus();
3829
+ }
3830
+ }, 0);
3831
+ };
3832
+ button.onclick = null;
3833
+ return button;
3834
+ }
3835
+ function createIframe({
3836
+ iframeUrl,
3837
+ iframeStyle = {}
3838
+ }) {
3839
+ const iframe = document.createElement("iframe");
3840
+ iframe.id = "satoshi-wallet-iframe";
3841
+ iframe.allow = "clipboard-read; clipboard-write";
3842
+ iframe.src = iframeUrl;
3843
+ const isVisible = localStorage.getItem("btc-wallet-iframe-visible") === "true";
3844
+ Object.assign(iframe.style, __spreadValues({
3845
+ position: "fixed",
3846
+ bottom: "90px",
3847
+ right: "20px",
3848
+ zIndex: "100000",
3849
+ boxShadow: "0 0 10px rgba(0, 0, 0, 0.1)",
3850
+ borderRadius: "10px",
3851
+ display: isVisible ? "block" : "none",
3852
+ border: "none"
3853
+ }, iframeStyle));
3854
+ document.body.appendChild(iframe);
3855
+ return iframe;
3856
+ }
3857
+ function setupButtonClickHandler(button, iframe, wallet, originalWallet) {
3858
+ return __async(this, null, function* () {
3859
+ var _a;
3860
+ const accountId = (_a = yield wallet == null ? void 0 : wallet.getAccounts()) == null ? void 0 : _a[0].accountId;
3861
+ const originalAccountId = originalWallet.account;
3862
+ const originalPublicKey = yield originalWallet.getPublicKey();
3863
+ console.log({ accountId, originalAccountId, originalPublicKey });
3864
+ const iframeSrc = new URL(iframe.src);
3865
+ iframeSrc.searchParams.set("origin", window.location.origin);
3866
+ accountId && iframeSrc.searchParams.set("accountId", accountId);
3867
+ originalAccountId && iframeSrc.searchParams.set("originalAccountId", originalAccountId);
3868
+ originalPublicKey && iframeSrc.searchParams.set("originalPublicKey", originalPublicKey);
3869
+ iframe.src = iframeSrc.toString();
3870
+ const actions = {
3871
+ signAndSendTransaction: wallet.signAndSendTransaction,
3872
+ executeBTCDepositAndAction
3873
+ };
3874
+ window.addEventListener("message", (event) => __async(this, null, function* () {
3875
+ var _a2, _b;
3876
+ if (event.origin !== iframeSrc.origin)
3877
+ return;
3878
+ const { action, requestId, data } = event.data;
3879
+ try {
3880
+ const actionFn = actions[action];
3881
+ if (!actionFn)
3882
+ return;
3883
+ console.log("handleWalletAction", action, event.data);
3884
+ const result = yield actionFn(data);
3885
+ console.log("handleWalletAction result", action, result);
3886
+ (_a2 = event.source) == null ? void 0 : _a2.postMessage(
3887
+ {
3888
+ requestId,
3889
+ data,
3890
+ success: true
3891
+ },
3892
+ { targetOrigin: event.origin }
3893
+ );
3894
+ } catch (error) {
3895
+ console.error("handleWalletAction error", action, error);
3896
+ (_b = event.source) == null ? void 0 : _b.postMessage(
3897
+ {
3898
+ requestId,
3899
+ error: error.message,
3900
+ success: false
3901
+ },
3902
+ { targetOrigin: event.origin }
3903
+ );
3904
+ }
3905
+ }));
3906
+ });
3907
+ }
3908
+ function removeWalletButton() {
3909
+ const button = document.getElementById("satoshi-wallet-button");
3910
+ button == null ? void 0 : button.remove();
3911
+ const iframe = document.getElementById("satoshi-wallet-iframe");
3912
+ iframe == null ? void 0 : iframe.remove();
3913
+ }
3914
+ function updateIframePosition(iframe, buttonRight, buttonBottom, windowWidth, windowHeight) {
3915
+ const iframeWidth = parseInt(iframe.style.width);
3916
+ const iframeHeight = parseInt(iframe.style.height);
3917
+ let iframeRight = buttonRight;
3918
+ let iframeBottom = buttonBottom + 70;
3919
+ if (iframeRight + iframeWidth > windowWidth - 20) {
3920
+ iframeRight = Math.max(20, windowWidth - iframeWidth - 20);
3921
+ }
3922
+ if (iframeBottom + iframeHeight > windowHeight - 20) {
3923
+ iframeBottom = Math.max(20, buttonBottom - iframeHeight - 10);
3924
+ }
3925
+ iframe.style.right = `${iframeRight}px`;
3926
+ iframe.style.bottom = `${iframeBottom}px`;
3927
+ }
3928
+
3928
3929
  // src/core/setupBTCWallet.ts
3930
+ var import_big2 = __toESM(require("big.js"), 1);
3929
3931
  var { transfer, functionCall } = import_transactions.actionCreators;
3930
3932
  var STORAGE_KEYS = {
3931
3933
  ACCOUNT: "btc-wallet-account",
@@ -4323,17 +4325,58 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4323
4325
  }
4324
4326
  function calculateGasStrategy(gasTokenBalance, transactions2) {
4325
4327
  return __async(this, null, function* () {
4328
+ var _a;
4326
4329
  const accountId = state.getAccount();
4330
+ const nearAccount = yield provider.query({
4331
+ request_type: "view_account",
4332
+ account_id: accountId,
4333
+ finality: "final"
4334
+ });
4335
+ const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
4336
+ console.log("available near balance:", availableBalance);
4337
+ console.log("available gas token balance:", gasTokenBalance);
4327
4338
  const convertTx = yield Promise.all(
4328
4339
  transactions2.map((transaction, index) => convertTransactionToTxHex(transaction, index))
4329
4340
  );
4330
- const adjustedGas = yield getPredictedGasAmount(
4331
- currentConfig.accountContractId,
4332
- currentConfig.token,
4333
- convertTx.map((t) => t.txHex)
4334
- );
4335
- const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4336
- return recalculateGasWithTransfer(transferTx, convertTx, false);
4341
+ if (availableBalance > 0.5) {
4342
+ console.log("near balance is enough, get the protocol fee of each transaction");
4343
+ const gasTokens = yield nearCall2(
4344
+ currentConfig.accountContractId,
4345
+ "list_gas_token",
4346
+ { token_ids: [currentConfig.token] }
4347
+ );
4348
+ console.log("list_gas_token gas tokens:", gasTokens);
4349
+ const perTxFee = Math.max(
4350
+ Number(((_a = gasTokens[currentConfig.token]) == null ? void 0 : _a.per_tx_protocol_fee) || 0),
4351
+ 100
4352
+ );
4353
+ console.log("perTxFee:", perTxFee);
4354
+ const protocolFee = new import_big2.default(perTxFee || "0").mul(convertTx.length).toFixed(0);
4355
+ console.log("protocolFee:", protocolFee);
4356
+ if (new import_big2.default(gasTokenBalance).gte(protocolFee)) {
4357
+ console.log("use near pay gas and enough gas token balance");
4358
+ return { useNearPayGas: true, gasLimit: protocolFee };
4359
+ } else {
4360
+ console.log("use near pay gas and not enough gas token balance");
4361
+ const transferTx = yield createGasTokenTransfer(accountId, protocolFee);
4362
+ return recalculateGasWithTransfer(transferTx, convertTx, true, perTxFee.toString());
4363
+ }
4364
+ } else {
4365
+ console.log("near balance is not enough, predict the gas token amount required");
4366
+ const adjustedGas = yield getPredictedGasAmount(
4367
+ currentConfig.accountContractId,
4368
+ currentConfig.token,
4369
+ convertTx.map((t) => t.txHex)
4370
+ );
4371
+ if (new import_big2.default(gasTokenBalance).gte(adjustedGas)) {
4372
+ console.log("use gas token and gas token balance is enough");
4373
+ return { useNearPayGas: false, gasLimit: adjustedGas };
4374
+ } else {
4375
+ console.log("use gas token and gas token balance is not enough, need to transfer");
4376
+ const transferTx = yield createGasTokenTransfer(accountId, adjustedGas);
4377
+ return recalculateGasWithTransfer(transferTx, convertTx, false);
4378
+ }
4379
+ }
4337
4380
  });
4338
4381
  }
4339
4382
  function convertTransactionToTxHex(transaction, index = 0) {
@@ -4438,7 +4481,7 @@ function setupBTCWallet({
4438
4481
 
4439
4482
  // src/index.ts
4440
4483
  var getVersion = () => {
4441
- return "0.5.2-beta";
4484
+ return "0.5.4-beta";
4442
4485
  };
4443
4486
  if (typeof window !== "undefined") {
4444
4487
  window.__BTC_WALLET_VERSION = getVersion();