btc-wallet 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
package/dist/config.d.ts CHANGED
@@ -4,6 +4,7 @@ export declare const walletConfig: Record<string, {
4
4
  accountContractId: string;
5
5
  bridgeContractId: string;
6
6
  walletUrl: string;
7
+ gasTokenLimit: string;
7
8
  }>;
8
9
  export declare const nearRpcUrls: {
9
10
  mainnet: string[];
package/dist/index.js CHANGED
@@ -2726,21 +2726,24 @@ var walletConfig = {
2726
2726
  token: "nbtc-dev.testnet",
2727
2727
  accountContractId: "acc-dev.testnet",
2728
2728
  bridgeContractId: "brg-dev.testnet",
2729
- walletUrl: "https://wallet-dev.satoshibridge.top"
2729
+ walletUrl: "https://wallet-dev.satoshibridge.top",
2730
+ gasTokenLimit: "3000"
2730
2731
  },
2731
2732
  testnet: {
2732
2733
  base_url: "https://api.testnet.satoshibridge.top",
2733
2734
  token: "nbtc2-nsp.testnet",
2734
2735
  accountContractId: "dev2-nsp.testnet",
2735
2736
  bridgeContractId: "brg2-nsp.testnet",
2736
- walletUrl: "https://wallet-test.satoshibridge.top"
2737
+ walletUrl: "https://wallet-test.satoshibridge.top",
2738
+ gasTokenLimit: "3000"
2737
2739
  },
2738
2740
  mainnet: {
2739
2741
  base_url: "https://api.mainnet.satoshibridge.top",
2740
2742
  token: "",
2741
2743
  accountContractId: "",
2742
2744
  bridgeContractId: "",
2743
- walletUrl: "https://wallet.satoshibridge.top"
2745
+ walletUrl: "https://wallet.satoshibridge.top",
2746
+ gasTokenLimit: "3000"
2744
2747
  }
2745
2748
  };
2746
2749
  var nearRpcUrls = {
@@ -2884,6 +2887,7 @@ function pollTransactionStatuses(network, hashes) {
2884
2887
  }
2885
2888
 
2886
2889
  // src/core/setupBTCWallet.ts
2890
+ var import_big = __toESM(require("big.js"), 1);
2887
2891
  var { transfer, functionCall } = import_transactions.actionCreators;
2888
2892
  var state = {
2889
2893
  saveAccount(account) {
@@ -3100,6 +3104,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3100
3104
  currentConfig.base_url,
3101
3105
  accountId
3102
3106
  );
3107
+ const { transferGasTransaction, useNearPayGas } = yield getGasConfig();
3108
+ if (!useNearPayGas && transferGasTransaction) {
3109
+ params.transactions.unshift(transferGasTransaction);
3110
+ }
3103
3111
  const newTransactions = params.transactions.map((transaction, index) => {
3104
3112
  let nearNonceNumber = accessKey.nonce + BigInt(1);
3105
3113
  if (nearNonceFromApi) {
@@ -3150,19 +3158,10 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3150
3158
  csna: accountId,
3151
3159
  near_transactions: newTransactions.map((t) => t.txHex),
3152
3160
  gas_token: currentConfig.token,
3153
- gas_limit: "3000",
3154
- use_near_pay_gas: false,
3161
+ gas_limit: currentConfig.gasTokenLimit,
3162
+ use_near_pay_gas: useNearPayGas,
3155
3163
  nonce
3156
3164
  };
3157
- const nearAccount = yield provider.query({
3158
- request_type: "view_account",
3159
- account_id: accountId,
3160
- finality: "final"
3161
- });
3162
- const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
3163
- if (availableBalance > 0.2) {
3164
- intention.use_near_pay_gas = true;
3165
- }
3166
3165
  const strIntention = JSON.stringify(intention);
3167
3166
  const signature = yield btcContext.signMessage(strIntention);
3168
3167
  const result = yield uploadBTCTx(currentConfig.base_url, {
@@ -3180,6 +3179,48 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3180
3179
  }
3181
3180
  });
3182
3181
  }
3182
+ function getGasConfig() {
3183
+ return __async(this, null, function* () {
3184
+ const accountId = state.getAccount();
3185
+ const nearAccount = yield provider.query({
3186
+ request_type: "view_account",
3187
+ account_id: accountId,
3188
+ finality: "final"
3189
+ });
3190
+ const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
3191
+ if (availableBalance > 0.2) {
3192
+ return { useNearPayGas: true };
3193
+ } else {
3194
+ const gasTokenBalance = yield nearCall2(currentConfig.token, "ft_balance_of", {
3195
+ account_id: accountId
3196
+ });
3197
+ if (new import_big.default(gasTokenBalance).gt(currentConfig.gasTokenLimit)) {
3198
+ const transferGasTransaction = {
3199
+ signerId: accountId,
3200
+ receiverId: currentConfig.token,
3201
+ actions: [
3202
+ {
3203
+ type: "FunctionCall",
3204
+ params: {
3205
+ methodName: "ft_transfer_call",
3206
+ args: {
3207
+ receiver_id: currentConfig.accountContractId,
3208
+ amount: currentConfig.gasTokenLimit,
3209
+ msg: "Deposit"
3210
+ },
3211
+ gas: new import_big.default(50).mul(__pow(10, 12)).toFixed(0),
3212
+ deposit: "1"
3213
+ }
3214
+ }
3215
+ ]
3216
+ };
3217
+ return { transferGasTransaction, useNearPayGas: false };
3218
+ } else {
3219
+ throw new Error("No enough gas token balance");
3220
+ }
3221
+ }
3222
+ });
3223
+ }
3183
3224
  function initWalletButton(network, wallet2) {
3184
3225
  return __async(this, null, function* () {
3185
3226
  const checkAndSetupWalletButton = () => {
@@ -3249,7 +3290,7 @@ function toHex(originalString) {
3249
3290
  }
3250
3291
 
3251
3292
  // src/core/btcUtils.ts
3252
- var import_big = __toESM(require("big.js"), 1);
3293
+ var import_big2 = __toESM(require("big.js"), 1);
3253
3294
  function getBtcProvider() {
3254
3295
  if (typeof window === "undefined" || !window.btcContext) {
3255
3296
  throw new Error("BTC Provider is not initialized.");
@@ -3363,7 +3404,7 @@ function estimateDepositAmount(amount, option) {
3363
3404
  {}
3364
3405
  );
3365
3406
  const fee = Math.max(Number(fee_min), Number(amount) * fee_rate);
3366
- return new import_big.default(amount).minus(fee).toFixed(0);
3407
+ return new import_big2.default(amount).minus(fee).toFixed(0);
3367
3408
  });
3368
3409
  }
3369
3410
  function executeBTCDepositAndAction(_0) {
@@ -3379,7 +3420,7 @@ function executeBTCDepositAndAction(_0) {
3379
3420
  const _action = Object.assign(
3380
3421
  {},
3381
3422
  __spreadProps(__spreadValues({}, action), {
3382
- gas: new import_big.default(100).mul(__pow(10, 12)).toFixed(0)
3423
+ gas: new import_big2.default(100).mul(__pow(10, 12)).toFixed(0)
3383
3424
  })
3384
3425
  );
3385
3426
  if (!btcPublicKey) {
@@ -3392,7 +3433,7 @@ function executeBTCDepositAndAction(_0) {
3392
3433
  isDev
3393
3434
  });
3394
3435
  _action.amount = amountWithFee;
3395
- if (!_action.amount || !new import_big.default(_action.amount || 0).gt(0)) {
3436
+ if (!_action.amount || !new import_big2.default(_action.amount || 0).gt(0)) {
3396
3437
  throw new Error("action.amount is required or deposit amount is not enough");
3397
3438
  }
3398
3439
  const csna = yield nearCall(
@@ -3407,10 +3448,14 @@ function executeBTCDepositAndAction(_0) {
3407
3448
  post_actions: [_action]
3408
3449
  };
3409
3450
  const storageDepositMsg = {};
3410
- const accountInfo = yield nearCall(config.accountContractId, "get_account", {
3411
- account_id: csna
3412
- });
3413
- if (!accountInfo.nonce) {
3451
+ const accountInfo = yield nearCall(
3452
+ config.accountContractId,
3453
+ "get_account",
3454
+ {
3455
+ account_id: csna
3456
+ }
3457
+ );
3458
+ if (!(accountInfo == null ? void 0 : accountInfo.nonce)) {
3414
3459
  storageDepositMsg.btc_public_key = btcPublicKey;
3415
3460
  }
3416
3461
  const registerRes = yield nearCall(action.receiver_id, "storage_balance_of", {
@@ -3419,14 +3464,14 @@ function executeBTCDepositAndAction(_0) {
3419
3464
  if (!(registerRes == null ? void 0 : registerRes.available)) {
3420
3465
  storageDepositMsg.storage_deposit_msg = {
3421
3466
  contract_id: action.receiver_id,
3422
- deposit: new import_big.default(0.25).mul(__pow(10, 24)).toFixed(0),
3467
+ deposit: new import_big2.default(0.25).mul(__pow(10, 24)).toFixed(0),
3423
3468
  registration_only: true
3424
3469
  };
3425
3470
  }
3426
3471
  if (Object.keys(storageDepositMsg).length > 0) {
3427
3472
  depositMsg.extra_msg = JSON.stringify(storageDepositMsg);
3428
3473
  }
3429
- console.log("deposit msg:", depositMsg);
3474
+ console.log("get_user_deposit_address params:", { deposit_msg: depositMsg });
3430
3475
  const userDepositAddress = yield nearCall(
3431
3476
  config.bridgeContractId,
3432
3477
  "get_user_deposit_address",
@@ -3434,12 +3479,12 @@ function executeBTCDepositAndAction(_0) {
3434
3479
  );
3435
3480
  const _feeRate = feeRate || (yield getBtcGasPrice());
3436
3481
  console.log("user deposit address:", userDepositAddress);
3437
- console.log("deposit amount:", new import_big.default(action.amount).toNumber());
3438
- console.log("receive amount:", new import_big.default(_action.amount).toNumber());
3482
+ console.log("deposit amount:", new import_big2.default(action.amount).toNumber());
3483
+ console.log("receive amount:", new import_big2.default(_action.amount).toNumber());
3439
3484
  console.log("fee rate:", _feeRate);
3440
3485
  const txHash = yield sendBitcoin(
3441
3486
  userDepositAddress,
3442
- new import_big.default(action.amount).toNumber(),
3487
+ new import_big2.default(action.amount).toNumber(),
3443
3488
  _feeRate
3444
3489
  );
3445
3490
  yield receiveDepositMsg(config.base_url, {
@@ -3460,7 +3505,7 @@ function executeBTCDepositAndAction(_0) {
3460
3505
 
3461
3506
  // src/index.ts
3462
3507
  var getVersion = () => {
3463
- return "0.3.7";
3508
+ return "0.3.8";
3464
3509
  };
3465
3510
  if (typeof window !== "undefined") {
3466
3511
  window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();