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.
- package/dist/core/btcUtils.d.ts +10 -0
- package/dist/index.js +61 -18
- package/dist/index.js.map +2 -2
- package/esm/index.js +61 -18
- package/esm/index.js.map +2 -2
- package/package.json +1 -1
package/esm/index.js
CHANGED
@@ -3229,6 +3229,12 @@ function getBtcRpcUrl() {
|
|
3229
3229
|
return btcRpcUrls[network];
|
3230
3230
|
});
|
3231
3231
|
}
|
3232
|
+
function getConfig(isDev) {
|
3233
|
+
return __async(this, null, function* () {
|
3234
|
+
const network = yield getNetwork();
|
3235
|
+
return walletConfig[isDev ? "dev" : network];
|
3236
|
+
});
|
3237
|
+
}
|
3232
3238
|
function nearCall(contractId, methodName, args) {
|
3233
3239
|
return __async(this, null, function* () {
|
3234
3240
|
const network = yield getNetwork();
|
@@ -3283,13 +3289,20 @@ function getBtcBalance() {
|
|
3283
3289
|
const { account } = yield retryOperation(getBtcProvider, (res2) => !!res2.account);
|
3284
3290
|
if (!account) {
|
3285
3291
|
console.error("BTC Account is not available.");
|
3286
|
-
return { rawBalance: 0, balance: 0 };
|
3292
|
+
return { rawBalance: 0, balance: 0, maxSpendableBalance: 0 };
|
3287
3293
|
}
|
3288
3294
|
const btcRpcUrl = yield getBtcRpcUrl();
|
3289
3295
|
const res = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res2) => res2.json());
|
3290
3296
|
const rawBalance = res == null ? void 0 : res.reduce((acc, cur) => acc + cur.value, 0);
|
3291
3297
|
const balance = rawBalance / __pow(10, 8);
|
3292
|
-
|
3298
|
+
const feeRate = yield getBtcGasPrice();
|
3299
|
+
const maxGasFee = feeRate * 250 / __pow(10, 8);
|
3300
|
+
const availableBalance = Math.max(0, balance - maxGasFee);
|
3301
|
+
return {
|
3302
|
+
rawBalance,
|
3303
|
+
balance,
|
3304
|
+
availableBalance
|
3305
|
+
};
|
3293
3306
|
});
|
3294
3307
|
}
|
3295
3308
|
function sendBitcoin(address, amount, feeRate) {
|
@@ -3299,6 +3312,20 @@ function sendBitcoin(address, amount, feeRate) {
|
|
3299
3312
|
return txHash;
|
3300
3313
|
});
|
3301
3314
|
}
|
3315
|
+
function estimateDepositAmount(amount, option) {
|
3316
|
+
return __async(this, null, function* () {
|
3317
|
+
const config = yield getConfig((option == null ? void 0 : option.isDev) || false);
|
3318
|
+
const {
|
3319
|
+
deposit_bridge_fee: { fee_min, fee_rate }
|
3320
|
+
} = yield nearCall(
|
3321
|
+
config.bridgeContractId,
|
3322
|
+
"get_config",
|
3323
|
+
{}
|
3324
|
+
);
|
3325
|
+
const fee = Math.max(Number(fee_min), Number(amount) * fee_rate);
|
3326
|
+
return new Big(amount).minus(fee).toFixed(0);
|
3327
|
+
});
|
3328
|
+
}
|
3302
3329
|
function executeBTCDepositAndAction(_0) {
|
3303
3330
|
return __async(this, arguments, function* ({
|
3304
3331
|
action,
|
@@ -3307,18 +3334,26 @@ function executeBTCDepositAndAction(_0) {
|
|
3307
3334
|
}) {
|
3308
3335
|
try {
|
3309
3336
|
const { getPublicKey } = getBtcProvider();
|
3310
|
-
const
|
3311
|
-
const config = walletConfig[isDev ? "dev" : network];
|
3337
|
+
const config = yield getConfig(isDev);
|
3312
3338
|
const btcPublicKey = yield getPublicKey();
|
3313
|
-
const _action = Object.assign(
|
3339
|
+
const _action = Object.assign(
|
3340
|
+
{},
|
3341
|
+
__spreadProps(__spreadValues({}, action), {
|
3342
|
+
gas: new Big(100).mul(__pow(10, 12)).toFixed(0)
|
3343
|
+
})
|
3344
|
+
);
|
3314
3345
|
if (!btcPublicKey) {
|
3315
3346
|
throw new Error("BTC Public Key is not available.");
|
3316
3347
|
}
|
3317
3348
|
if (!_action.receiver_id) {
|
3318
3349
|
throw new Error("action.receiver_id is required");
|
3319
3350
|
}
|
3351
|
+
const amountWithFee = yield estimateDepositAmount(_action.amount, {
|
3352
|
+
isDev
|
3353
|
+
});
|
3354
|
+
_action.amount = amountWithFee;
|
3320
3355
|
if (!_action.amount || !new Big(_action.amount || 0).gt(0)) {
|
3321
|
-
throw new Error("action.amount is required
|
3356
|
+
throw new Error("action.amount is required or deposit amount is not enough");
|
3322
3357
|
}
|
3323
3358
|
const csna = yield nearCall(
|
3324
3359
|
config.accountContractId,
|
@@ -3327,34 +3362,41 @@ function executeBTCDepositAndAction(_0) {
|
|
3327
3362
|
btc_public_key: btcPublicKey
|
3328
3363
|
}
|
3329
3364
|
);
|
3330
|
-
_action.amount = new Big(_action.amount).toString();
|
3331
|
-
_action.gas = new Big(100).mul(__pow(10, 12)).toFixed(0);
|
3332
3365
|
const depositMsg = {
|
3333
3366
|
recipient_id: csna,
|
3334
3367
|
post_actions: [_action]
|
3335
3368
|
};
|
3369
|
+
const storageDepositMsg = {};
|
3370
|
+
const accountInfo = yield nearCall(config.accountContractId, "get_account", {
|
3371
|
+
account_id: csna
|
3372
|
+
});
|
3373
|
+
if (!accountInfo.nonce) {
|
3374
|
+
storageDepositMsg.btc_public_key = btcPublicKey;
|
3375
|
+
}
|
3336
3376
|
const registerRes = yield nearCall(action.receiver_id, "storage_balance_of", {
|
3337
3377
|
account_id: csna
|
3338
3378
|
});
|
3339
3379
|
if (!(registerRes == null ? void 0 : registerRes.available)) {
|
3340
|
-
|
3341
|
-
|
3342
|
-
|
3343
|
-
|
3344
|
-
registration_only: true
|
3345
|
-
},
|
3346
|
-
btc_public_key: btcPublicKey
|
3380
|
+
storageDepositMsg.storage_deposit_msg = {
|
3381
|
+
contract_id: action.receiver_id,
|
3382
|
+
deposit: new Big(0.25).mul(__pow(10, 24)).toFixed(0),
|
3383
|
+
registration_only: true
|
3347
3384
|
};
|
3385
|
+
}
|
3386
|
+
if (Object.keys(storageDepositMsg).length > 0) {
|
3348
3387
|
depositMsg.extra_msg = JSON.stringify(storageDepositMsg);
|
3349
3388
|
}
|
3350
|
-
console.log("
|
3389
|
+
console.log("deposit msg:", depositMsg);
|
3351
3390
|
const userDepositAddress = yield nearCall(
|
3352
3391
|
config.bridgeContractId,
|
3353
3392
|
"get_user_deposit_address",
|
3354
3393
|
{ deposit_msg: depositMsg }
|
3355
3394
|
);
|
3356
|
-
console.log("userDepositAddress", userDepositAddress);
|
3357
3395
|
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3396
|
+
console.log("user deposit address:", userDepositAddress);
|
3397
|
+
console.log("deposit amount:", new Big(action.amount).toNumber());
|
3398
|
+
console.log("receive amount:", new Big(_action.amount).toNumber());
|
3399
|
+
console.log("fee rate:", _feeRate);
|
3358
3400
|
const txHash = yield sendBitcoin(
|
3359
3401
|
userDepositAddress,
|
3360
3402
|
new Big(action.amount).toNumber(),
|
@@ -3378,7 +3420,7 @@ function executeBTCDepositAndAction(_0) {
|
|
3378
3420
|
|
3379
3421
|
// src/index.ts
|
3380
3422
|
var getVersion = () => {
|
3381
|
-
return "0.3.
|
3423
|
+
return "0.3.7";
|
3382
3424
|
};
|
3383
3425
|
if (typeof window !== "undefined") {
|
3384
3426
|
window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
|
@@ -3396,6 +3438,7 @@ export {
|
|
3396
3438
|
UnisatConnector,
|
3397
3439
|
WizzConnector,
|
3398
3440
|
XverseConnector,
|
3441
|
+
estimateDepositAmount,
|
3399
3442
|
executeBTCDepositAndAction,
|
3400
3443
|
getBtcBalance,
|
3401
3444
|
getBtcGasPrice,
|