btc-wallet 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,19 @@
1
1
  export declare function getBtcGasPrice(): Promise<number>;
2
2
  export declare function getBtcBalance(): Promise<{
3
+ rawBalance: number;
4
+ balance: number;
5
+ maxSpendableBalance: number;
6
+ availableBalance?: undefined;
7
+ } | {
3
8
  rawBalance: any;
4
9
  balance: number;
10
+ availableBalance: number;
11
+ maxSpendableBalance?: undefined;
5
12
  }>;
6
13
  export declare function sendBitcoin(address: string, amount: number, feeRate: number): Promise<string>;
14
+ export declare function estimateDepositAmount(amount: string, option?: {
15
+ isDev: boolean;
16
+ }): Promise<string>;
7
17
  interface ExecuteBTCDepositAndActionParams {
8
18
  action: {
9
19
  receiver_id: string;
package/dist/index.js CHANGED
@@ -90,6 +90,7 @@ __export(src_exports, {
90
90
  UnisatConnector: () => UnisatConnector,
91
91
  WizzConnector: () => WizzConnector,
92
92
  XverseConnector: () => XverseConnector,
93
+ estimateDepositAmount: () => estimateDepositAmount,
93
94
  executeBTCDepositAndAction: () => executeBTCDepositAndAction,
94
95
  getBtcBalance: () => getBtcBalance,
95
96
  getBtcGasPrice: () => getBtcGasPrice,
@@ -3268,6 +3269,12 @@ function getBtcRpcUrl() {
3268
3269
  return btcRpcUrls[network];
3269
3270
  });
3270
3271
  }
3272
+ function getConfig(isDev) {
3273
+ return __async(this, null, function* () {
3274
+ const network = yield getNetwork();
3275
+ return walletConfig[isDev ? "dev" : network];
3276
+ });
3277
+ }
3271
3278
  function nearCall(contractId, methodName, args) {
3272
3279
  return __async(this, null, function* () {
3273
3280
  const network = yield getNetwork();
@@ -3322,13 +3329,20 @@ function getBtcBalance() {
3322
3329
  const { account } = yield retryOperation(getBtcProvider, (res2) => !!res2.account);
3323
3330
  if (!account) {
3324
3331
  console.error("BTC Account is not available.");
3325
- return { rawBalance: 0, balance: 0 };
3332
+ return { rawBalance: 0, balance: 0, maxSpendableBalance: 0 };
3326
3333
  }
3327
3334
  const btcRpcUrl = yield getBtcRpcUrl();
3328
3335
  const res = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res2) => res2.json());
3329
3336
  const rawBalance = res == null ? void 0 : res.reduce((acc, cur) => acc + cur.value, 0);
3330
3337
  const balance = rawBalance / __pow(10, 8);
3331
- return { rawBalance, balance };
3338
+ const feeRate = yield getBtcGasPrice();
3339
+ const maxGasFee = feeRate * 250 / __pow(10, 8);
3340
+ const availableBalance = Math.max(0, balance - maxGasFee);
3341
+ return {
3342
+ rawBalance,
3343
+ balance,
3344
+ availableBalance
3345
+ };
3332
3346
  });
3333
3347
  }
3334
3348
  function sendBitcoin(address, amount, feeRate) {
@@ -3338,6 +3352,20 @@ function sendBitcoin(address, amount, feeRate) {
3338
3352
  return txHash;
3339
3353
  });
3340
3354
  }
3355
+ function estimateDepositAmount(amount, option) {
3356
+ return __async(this, null, function* () {
3357
+ const config = yield getConfig((option == null ? void 0 : option.isDev) || false);
3358
+ const {
3359
+ deposit_bridge_fee: { fee_min, fee_rate }
3360
+ } = yield nearCall(
3361
+ config.bridgeContractId,
3362
+ "get_config",
3363
+ {}
3364
+ );
3365
+ const fee = Math.max(Number(fee_min), Number(amount) * fee_rate);
3366
+ return new import_big.default(amount).minus(fee).toFixed(0);
3367
+ });
3368
+ }
3341
3369
  function executeBTCDepositAndAction(_0) {
3342
3370
  return __async(this, arguments, function* ({
3343
3371
  action,
@@ -3346,18 +3374,26 @@ function executeBTCDepositAndAction(_0) {
3346
3374
  }) {
3347
3375
  try {
3348
3376
  const { getPublicKey } = getBtcProvider();
3349
- const network = yield getNetwork();
3350
- const config = walletConfig[isDev ? "dev" : network];
3377
+ const config = yield getConfig(isDev);
3351
3378
  const btcPublicKey = yield getPublicKey();
3352
- const _action = Object.assign({}, action);
3379
+ const _action = Object.assign(
3380
+ {},
3381
+ __spreadProps(__spreadValues({}, action), {
3382
+ gas: new import_big.default(100).mul(__pow(10, 12)).toFixed(0)
3383
+ })
3384
+ );
3353
3385
  if (!btcPublicKey) {
3354
3386
  throw new Error("BTC Public Key is not available.");
3355
3387
  }
3356
3388
  if (!_action.receiver_id) {
3357
3389
  throw new Error("action.receiver_id is required");
3358
3390
  }
3391
+ const amountWithFee = yield estimateDepositAmount(_action.amount, {
3392
+ isDev
3393
+ });
3394
+ _action.amount = amountWithFee;
3359
3395
  if (!_action.amount || !new import_big.default(_action.amount || 0).gt(0)) {
3360
- throw new Error("action.amount is required and must be greater than 0");
3396
+ throw new Error("action.amount is required or deposit amount is not enough");
3361
3397
  }
3362
3398
  const csna = yield nearCall(
3363
3399
  config.accountContractId,
@@ -3366,34 +3402,41 @@ function executeBTCDepositAndAction(_0) {
3366
3402
  btc_public_key: btcPublicKey
3367
3403
  }
3368
3404
  );
3369
- _action.amount = new import_big.default(_action.amount).toString();
3370
- _action.gas = new import_big.default(100).mul(__pow(10, 12)).toFixed(0);
3371
3405
  const depositMsg = {
3372
3406
  recipient_id: csna,
3373
3407
  post_actions: [_action]
3374
3408
  };
3409
+ const storageDepositMsg = {};
3410
+ const accountInfo = yield nearCall(config.accountContractId, "get_account", {
3411
+ account_id: csna
3412
+ });
3413
+ if (!accountInfo.nonce) {
3414
+ storageDepositMsg.btc_public_key = btcPublicKey;
3415
+ }
3375
3416
  const registerRes = yield nearCall(action.receiver_id, "storage_balance_of", {
3376
3417
  account_id: csna
3377
3418
  });
3378
3419
  if (!(registerRes == null ? void 0 : registerRes.available)) {
3379
- const storageDepositMsg = {
3380
- storage_deposit_msg: {
3381
- contract_id: action.receiver_id,
3382
- deposit: new import_big.default(0.25).mul(__pow(10, 24)).toFixed(0),
3383
- registration_only: true
3384
- },
3385
- btc_public_key: btcPublicKey
3420
+ storageDepositMsg.storage_deposit_msg = {
3421
+ contract_id: action.receiver_id,
3422
+ deposit: new import_big.default(0.25).mul(__pow(10, 24)).toFixed(0),
3423
+ registration_only: true
3386
3424
  };
3425
+ }
3426
+ if (Object.keys(storageDepositMsg).length > 0) {
3387
3427
  depositMsg.extra_msg = JSON.stringify(storageDepositMsg);
3388
3428
  }
3389
- console.log("depositMsg", depositMsg);
3429
+ console.log("deposit msg:", depositMsg);
3390
3430
  const userDepositAddress = yield nearCall(
3391
3431
  config.bridgeContractId,
3392
3432
  "get_user_deposit_address",
3393
3433
  { deposit_msg: depositMsg }
3394
3434
  );
3395
- console.log("userDepositAddress", userDepositAddress);
3396
3435
  const _feeRate = feeRate || (yield getBtcGasPrice());
3436
+ 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());
3439
+ console.log("fee rate:", _feeRate);
3397
3440
  const txHash = yield sendBitcoin(
3398
3441
  userDepositAddress,
3399
3442
  new import_big.default(action.amount).toNumber(),
@@ -3417,7 +3460,7 @@ function executeBTCDepositAndAction(_0) {
3417
3460
 
3418
3461
  // src/index.ts
3419
3462
  var getVersion = () => {
3420
- return "0.3.6";
3463
+ return "0.3.7";
3421
3464
  };
3422
3465
  if (typeof window !== "undefined") {
3423
3466
  window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();