btc-wallet 0.3.5 → 0.3.7
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.
- package/dist/config.d.ts +2 -1
- package/dist/core/btcUtils.d.ts +24 -5
- package/dist/core/btcWalletSelectorContext.d.ts +2 -2
- package/dist/index.js +229 -127
- package/dist/index.js.map +4 -4
- package/dist/utils/nearUtils.d.ts +7 -0
- package/esm/index.js +228 -126
- package/esm/index.js.map +4 -4
- package/package.json +1 -1
package/dist/config.d.ts
CHANGED
package/dist/core/btcUtils.d.ts
CHANGED
@@ -1,16 +1,35 @@
|
|
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
|
-
export declare function sendBitcoin(address: string, amount:
|
7
|
-
|
8
|
-
|
9
|
-
|
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>;
|
17
|
+
interface ExecuteBTCDepositAndActionParams {
|
18
|
+
action: {
|
19
|
+
receiver_id: string;
|
20
|
+
amount: string;
|
21
|
+
msg: string;
|
22
|
+
};
|
10
23
|
/** fee rate, if not provided, will use the recommended fee rate from the btc node */
|
11
24
|
feeRate?: number;
|
12
25
|
/** is dev environment */
|
13
26
|
isDev?: boolean;
|
14
27
|
}
|
15
|
-
export declare function
|
28
|
+
export declare function executeBTCDepositAndAction({ action, feeRate, isDev, }: ExecuteBTCDepositAndActionParams): Promise<{
|
29
|
+
result: string;
|
30
|
+
error?: undefined;
|
31
|
+
} | {
|
32
|
+
result: string;
|
33
|
+
error: any;
|
34
|
+
}>;
|
16
35
|
export {};
|
@@ -4,10 +4,10 @@ export declare function BtcWalletSelectorContextProvider({ children, }: {
|
|
4
4
|
autoConnect?: boolean;
|
5
5
|
}): import("react/jsx-runtime").JSX.Element;
|
6
6
|
export declare function useBtcWalletSelector(): {
|
7
|
-
login: () => Promise<string
|
7
|
+
login: () => Promise<string>;
|
8
8
|
autoConnect: () => Promise<void>;
|
9
9
|
logout: () => void;
|
10
|
-
account: string
|
10
|
+
account: string;
|
11
11
|
getPublicKey: () => any;
|
12
12
|
signMessage: (msg: string) => any;
|
13
13
|
getContext: () => any;
|
package/dist/index.js
CHANGED
@@ -90,7 +90,8 @@ __export(src_exports, {
|
|
90
90
|
UnisatConnector: () => UnisatConnector,
|
91
91
|
WizzConnector: () => WizzConnector,
|
92
92
|
XverseConnector: () => XverseConnector,
|
93
|
-
|
93
|
+
estimateDepositAmount: () => estimateDepositAmount,
|
94
|
+
executeBTCDepositAndAction: () => executeBTCDepositAndAction,
|
94
95
|
getBtcBalance: () => getBtcBalance,
|
95
96
|
getBtcGasPrice: () => getBtcGasPrice,
|
96
97
|
getVersion: () => getVersion,
|
@@ -2497,7 +2498,7 @@ function useBtcWalletSelector() {
|
|
2497
2498
|
publicKey.current = res;
|
2498
2499
|
});
|
2499
2500
|
}
|
2500
|
-
}, [provider]);
|
2501
|
+
}, [getPublicKey, provider]);
|
2501
2502
|
(0, import_react11.useEffect)(() => {
|
2502
2503
|
signMessageFn.current = signMessage;
|
2503
2504
|
}, [signMessage]);
|
@@ -2519,18 +2520,15 @@ function useBtcWalletSelector() {
|
|
2519
2520
|
connector.removeListener("accountsChanged", fn);
|
2520
2521
|
}
|
2521
2522
|
};
|
2522
|
-
}, [connector]);
|
2523
|
+
}, [connector, context, getPublicKey]);
|
2523
2524
|
const hook = (0, import_react11.useMemo)(() => {
|
2524
2525
|
return {
|
2525
2526
|
login: () => __async(this, null, function* () {
|
2526
|
-
const account = accounts
|
2527
|
-
if (account) {
|
2528
|
-
|
2529
|
-
}
|
2530
|
-
if (openConnectModal) {
|
2531
|
-
yield openConnectModal();
|
2527
|
+
const account = accounts == null ? void 0 : accounts[0];
|
2528
|
+
if (!account) {
|
2529
|
+
openConnectModal == null ? void 0 : openConnectModal();
|
2532
2530
|
}
|
2533
|
-
return
|
2531
|
+
return account;
|
2534
2532
|
}),
|
2535
2533
|
autoConnect: () => __async(this, null, function* () {
|
2536
2534
|
requestDirectAccount(connectorRef.current).catch((e) => {
|
@@ -2538,13 +2536,13 @@ function useBtcWalletSelector() {
|
|
2538
2536
|
});
|
2539
2537
|
}),
|
2540
2538
|
logout: () => {
|
2541
|
-
const accountId = accounts
|
2539
|
+
const accountId = accounts == null ? void 0 : accounts[0];
|
2542
2540
|
if (!accountId)
|
2543
2541
|
return;
|
2544
2542
|
disconnect == null ? void 0 : disconnect();
|
2545
2543
|
context.emit("btcLogOut");
|
2546
2544
|
},
|
2547
|
-
account: accounts
|
2545
|
+
account: accounts == null ? void 0 : accounts[0],
|
2548
2546
|
getPublicKey: () => {
|
2549
2547
|
return publicKey.current;
|
2550
2548
|
},
|
@@ -2570,7 +2568,7 @@ function useBtcWalletSelector() {
|
|
2570
2568
|
}
|
2571
2569
|
|
2572
2570
|
// src/core/setupBTCWallet.ts
|
2573
|
-
var
|
2571
|
+
var import_near_api_js2 = require("near-api-js");
|
2574
2572
|
var import_transactions = require("@near-js/transactions");
|
2575
2573
|
var import_key_pair = require("near-api-js/lib/utils/key_pair");
|
2576
2574
|
var import_transaction = require("near-api-js/lib/transaction");
|
@@ -2725,20 +2723,23 @@ function removeWalletButton() {
|
|
2725
2723
|
var walletConfig = {
|
2726
2724
|
dev: {
|
2727
2725
|
base_url: "https://api.dev.satoshibridge.top",
|
2728
|
-
token: "
|
2729
|
-
|
2726
|
+
token: "nbtc-dev.testnet",
|
2727
|
+
accountContractId: "acc-dev.testnet",
|
2728
|
+
bridgeContractId: "brg-dev.testnet",
|
2730
2729
|
walletUrl: "https://wallet-dev.satoshibridge.top"
|
2731
2730
|
},
|
2732
2731
|
testnet: {
|
2733
2732
|
base_url: "https://api.testnet.satoshibridge.top",
|
2734
2733
|
token: "nbtc2-nsp.testnet",
|
2735
|
-
|
2734
|
+
accountContractId: "dev2-nsp.testnet",
|
2735
|
+
bridgeContractId: "brg2-nsp.testnet",
|
2736
2736
|
walletUrl: "https://wallet-test.satoshibridge.top"
|
2737
2737
|
},
|
2738
2738
|
mainnet: {
|
2739
2739
|
base_url: "https://api.mainnet.satoshibridge.top",
|
2740
2740
|
token: "",
|
2741
|
-
|
2741
|
+
accountContractId: "",
|
2742
|
+
bridgeContractId: "",
|
2742
2743
|
walletUrl: "https://wallet.satoshibridge.top"
|
2743
2744
|
}
|
2744
2745
|
};
|
@@ -2750,8 +2751,8 @@ var nearRpcUrls = {
|
|
2750
2751
|
"https://near.drpc.org"
|
2751
2752
|
],
|
2752
2753
|
testnet: [
|
2753
|
-
"https://near-testnet.lava.build",
|
2754
2754
|
"https://rpc.testnet.near.org",
|
2755
|
+
"https://near-testnet.lava.build",
|
2755
2756
|
"https://near-testnet.drpc.org"
|
2756
2757
|
]
|
2757
2758
|
};
|
@@ -2830,6 +2831,58 @@ function request(url, options) {
|
|
2830
2831
|
});
|
2831
2832
|
}
|
2832
2833
|
|
2834
|
+
// src/utils/nearUtils.ts
|
2835
|
+
var import_near_api_js = require("near-api-js");
|
2836
|
+
function nearCallFunction(contractId, methodName, args, options) {
|
2837
|
+
return __async(this, null, function* () {
|
2838
|
+
const nearProvider = (options == null ? void 0 : options.provider) || new import_near_api_js.providers.FailoverRpcProvider(
|
2839
|
+
nearRpcUrls[options == null ? void 0 : options.network].map(
|
2840
|
+
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2841
|
+
)
|
2842
|
+
);
|
2843
|
+
const res = yield nearProvider.query({
|
2844
|
+
request_type: "call_function",
|
2845
|
+
account_id: contractId,
|
2846
|
+
method_name: methodName,
|
2847
|
+
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2848
|
+
finality: "final"
|
2849
|
+
});
|
2850
|
+
return JSON.parse(Buffer.from(res.result).toString());
|
2851
|
+
});
|
2852
|
+
}
|
2853
|
+
function pollTransactionStatuses(network, hashes) {
|
2854
|
+
return __async(this, null, function* () {
|
2855
|
+
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
2856
|
+
Object.values(nearRpcUrls[network]).map(
|
2857
|
+
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2858
|
+
)
|
2859
|
+
);
|
2860
|
+
const maxAttempts = 3;
|
2861
|
+
const pollStatus = (hash) => __async(this, null, function* () {
|
2862
|
+
let attempt = 0;
|
2863
|
+
while (attempt < maxAttempts) {
|
2864
|
+
attempt++;
|
2865
|
+
try {
|
2866
|
+
const result = yield provider.txStatus(hash, "unused", "FINAL");
|
2867
|
+
if (result && result.status) {
|
2868
|
+
console.log(`Transaction ${hash} result:`, result);
|
2869
|
+
return result;
|
2870
|
+
}
|
2871
|
+
} catch (error) {
|
2872
|
+
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
2873
|
+
}
|
2874
|
+
if (attempt === maxAttempts) {
|
2875
|
+
throw new Error(`Transaction not found after max attempts: ${hash}`);
|
2876
|
+
}
|
2877
|
+
yield delay(1e4);
|
2878
|
+
console.log(`RPC request failed for ${hash}, retrying ${maxAttempts - attempt} more times`);
|
2879
|
+
}
|
2880
|
+
});
|
2881
|
+
const results = yield Promise.all(hashes.map((hash) => pollStatus(hash)));
|
2882
|
+
return results;
|
2883
|
+
});
|
2884
|
+
}
|
2885
|
+
|
2833
2886
|
// src/core/setupBTCWallet.ts
|
2834
2887
|
var { transfer, functionCall } = import_transactions.actionCreators;
|
2835
2888
|
var state = {
|
@@ -2927,28 +2980,23 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2927
2980
|
}
|
2928
2981
|
}), 500);
|
2929
2982
|
}
|
2930
|
-
function
|
2931
|
-
return __async(this,
|
2932
|
-
|
2933
|
-
request_type: "call_function",
|
2934
|
-
account_id: currentConfig.contractId,
|
2935
|
-
method_name: method,
|
2936
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2937
|
-
finality: "optimistic"
|
2938
|
-
});
|
2939
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
2983
|
+
function nearCall2(contractId, methodName, args) {
|
2984
|
+
return __async(this, null, function* () {
|
2985
|
+
return nearCallFunction(contractId, methodName, args, { provider });
|
2940
2986
|
});
|
2941
2987
|
}
|
2942
2988
|
function getNearAccountByBtcPublicKey(btcPublicKey) {
|
2943
2989
|
return __async(this, null, function* () {
|
2944
|
-
const nearTempAddress = yield
|
2945
|
-
|
2946
|
-
|
2947
|
-
|
2948
|
-
|
2949
|
-
|
2950
|
-
|
2951
|
-
|
2990
|
+
const nearTempAddress = yield nearCall2(
|
2991
|
+
currentConfig.accountContractId,
|
2992
|
+
"get_chain_signature_near_account_id",
|
2993
|
+
{ btc_public_key: btcPublicKey }
|
2994
|
+
);
|
2995
|
+
const nearTempPublicKey = yield nearCall2(
|
2996
|
+
currentConfig.accountContractId,
|
2997
|
+
"get_chain_signature_near_account_public_key",
|
2998
|
+
{ btc_public_key: btcPublicKey }
|
2999
|
+
);
|
2952
3000
|
state.saveAccount(nearTempAddress);
|
2953
3001
|
state.savePublicKey(nearTempPublicKey);
|
2954
3002
|
state.saveBtcPublicKey(btcPublicKey);
|
@@ -3048,11 +3096,14 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3048
3096
|
nonce: BigInt(rawAccessKey.nonce || 0)
|
3049
3097
|
});
|
3050
3098
|
const publicKeyFormat = import_key_pair.PublicKey.from(publicKey);
|
3051
|
-
const
|
3099
|
+
const { result_data: nearNonceFromApi } = yield getNearNonceFromApi(
|
3100
|
+
currentConfig.base_url,
|
3101
|
+
accountId
|
3102
|
+
);
|
3052
3103
|
const newTransactions = params.transactions.map((transaction, index) => {
|
3053
3104
|
let nearNonceNumber = accessKey.nonce + BigInt(1);
|
3054
|
-
if (
|
3055
|
-
nearNonceNumber = BigInt(
|
3105
|
+
if (nearNonceFromApi) {
|
3106
|
+
nearNonceNumber = BigInt(nearNonceFromApi) > nearNonceNumber ? BigInt(nearNonceFromApi) : nearNonceNumber;
|
3056
3107
|
}
|
3057
3108
|
const newActions = transaction.actions.map((action) => {
|
3058
3109
|
switch (action.type) {
|
@@ -3067,7 +3118,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3067
3118
|
return transfer(BigInt(action.params.deposit));
|
3068
3119
|
}
|
3069
3120
|
}).filter(Boolean);
|
3070
|
-
const _transaction =
|
3121
|
+
const _transaction = import_near_api_js2.transactions.createTransaction(
|
3071
3122
|
accountId,
|
3072
3123
|
publicKeyFormat,
|
3073
3124
|
transaction.receiverId,
|
@@ -3084,20 +3135,34 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3084
3135
|
const hash = import_bs58.default.encode(new Uint8Array(import_js_sha256.sha256.array(txBytes)));
|
3085
3136
|
return { txBytes, txHex, hash };
|
3086
3137
|
});
|
3087
|
-
const accountInfo = yield
|
3088
|
-
|
3089
|
-
|
3090
|
-
|
3091
|
-
|
3092
|
-
const
|
3138
|
+
const accountInfo = yield nearCall2(
|
3139
|
+
currentConfig.accountContractId,
|
3140
|
+
"get_account",
|
3141
|
+
{ account_id: accountId }
|
3142
|
+
);
|
3143
|
+
const { result_data: nonceFromApi } = yield getNonceFromApi(
|
3144
|
+
currentConfig.base_url,
|
3145
|
+
accountId
|
3146
|
+
);
|
3147
|
+
const nonce = Number(nonceFromApi) > Number(accountInfo.nonce) ? String(nonceFromApi) : String(accountInfo.nonce);
|
3093
3148
|
const intention = {
|
3094
3149
|
chain_id: "397",
|
3095
3150
|
csna: accountId,
|
3096
3151
|
near_transactions: newTransactions.map((t) => t.txHex),
|
3097
3152
|
gas_token: currentConfig.token,
|
3098
3153
|
gas_limit: "3000",
|
3154
|
+
use_near_pay_gas: false,
|
3099
3155
|
nonce
|
3100
3156
|
};
|
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
|
+
}
|
3101
3166
|
const strIntention = JSON.stringify(intention);
|
3102
3167
|
const signature = yield btcContext.signMessage(strIntention);
|
3103
3168
|
const result = yield uploadBTCTx(currentConfig.base_url, {
|
@@ -3182,41 +3247,8 @@ function toHex(originalString) {
|
|
3182
3247
|
hexString = hexString.replace(/(^0+)/g, "");
|
3183
3248
|
return hexString;
|
3184
3249
|
}
|
3185
|
-
function pollTransactionStatuses(network, hashes) {
|
3186
|
-
return __async(this, null, function* () {
|
3187
|
-
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
3188
|
-
Object.values(nearRpcUrls[network]).map(
|
3189
|
-
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
3190
|
-
)
|
3191
|
-
);
|
3192
|
-
const maxAttempts = 3;
|
3193
|
-
const pollStatus = (hash) => __async(this, null, function* () {
|
3194
|
-
let attempt = 0;
|
3195
|
-
while (attempt < maxAttempts) {
|
3196
|
-
attempt++;
|
3197
|
-
try {
|
3198
|
-
const result = yield provider.txStatus(hash, "unused", "FINAL");
|
3199
|
-
if (result && result.status) {
|
3200
|
-
console.log(`Transaction ${hash} result:`, result);
|
3201
|
-
return result;
|
3202
|
-
}
|
3203
|
-
} catch (error) {
|
3204
|
-
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
3205
|
-
}
|
3206
|
-
if (attempt === maxAttempts) {
|
3207
|
-
throw new Error(`Transaction not found after max attempts: ${hash}`);
|
3208
|
-
}
|
3209
|
-
yield delay(1e4);
|
3210
|
-
console.log(`RPC request failed for ${hash}, retrying ${maxAttempts - attempt} more times`);
|
3211
|
-
}
|
3212
|
-
});
|
3213
|
-
const results = yield Promise.all(hashes.map((hash) => pollStatus(hash)));
|
3214
|
-
return results;
|
3215
|
-
});
|
3216
|
-
}
|
3217
3250
|
|
3218
3251
|
// src/core/btcUtils.ts
|
3219
|
-
var import_near_api_js2 = require("near-api-js");
|
3220
3252
|
var import_big = __toESM(require("big.js"), 1);
|
3221
3253
|
function getBtcProvider() {
|
3222
3254
|
if (typeof window === "undefined" || !window.btcContext) {
|
@@ -3237,55 +3269,45 @@ function getBtcRpcUrl() {
|
|
3237
3269
|
return btcRpcUrls[network];
|
3238
3270
|
});
|
3239
3271
|
}
|
3240
|
-
function
|
3272
|
+
function getConfig(isDev) {
|
3241
3273
|
return __async(this, null, function* () {
|
3242
3274
|
const network = yield getNetwork();
|
3243
|
-
|
3244
|
-
nearRpcUrls[network].map(
|
3245
|
-
(url) => new import_near_api_js2.providers.JsonRpcProvider({ url })
|
3246
|
-
)
|
3247
|
-
);
|
3248
|
-
const res = yield nearProvider.query({
|
3249
|
-
request_type: "call_function",
|
3250
|
-
account_id: contractId,
|
3251
|
-
method_name: methodName,
|
3252
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
3253
|
-
finality: "optimistic"
|
3254
|
-
});
|
3255
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
3275
|
+
return walletConfig[isDev ? "dev" : network];
|
3256
3276
|
});
|
3257
3277
|
}
|
3258
|
-
function
|
3278
|
+
function nearCall(contractId, methodName, args) {
|
3259
3279
|
return __async(this, null, function* () {
|
3260
|
-
const
|
3261
|
-
|
3262
|
-
BtcPublicKey: { btc_public_key: btcPublicKey, dapp_operation: "Burrowland->Supply" }
|
3263
|
-
}
|
3264
|
-
});
|
3265
|
-
return res;
|
3280
|
+
const network = yield getNetwork();
|
3281
|
+
return nearCallFunction(contractId, methodName, args, { network });
|
3266
3282
|
});
|
3267
3283
|
}
|
3268
3284
|
function receiveDepositMsg(_0, _1) {
|
3269
3285
|
return __async(this, arguments, function* (baseUrl, {
|
3270
3286
|
btcPublicKey,
|
3271
3287
|
txHash,
|
3272
|
-
depositType = 1
|
3288
|
+
depositType = 1,
|
3289
|
+
postActions,
|
3290
|
+
extraMsg
|
3273
3291
|
}) {
|
3274
3292
|
const res = yield request(`${baseUrl}/v1/receiveDepositMsg`, {
|
3275
3293
|
method: "POST",
|
3276
|
-
body: { btcPublicKey, txHash, depositType }
|
3294
|
+
body: { btcPublicKey, txHash, depositType, postActions, extraMsg }
|
3277
3295
|
});
|
3296
|
+
console.log("receiveDepositMsg resp:", res);
|
3278
3297
|
return res;
|
3279
3298
|
});
|
3280
3299
|
}
|
3281
3300
|
function checkTransactionStatus(baseUrl, txHash) {
|
3282
3301
|
return __async(this, null, function* () {
|
3283
|
-
const res = yield request(
|
3284
|
-
|
3285
|
-
|
3286
|
-
|
3287
|
-
|
3288
|
-
|
3302
|
+
const res = yield request(
|
3303
|
+
`${baseUrl}/v1/bridgeFromTx?fromTxHash=${txHash}&fromChainId=1`,
|
3304
|
+
{
|
3305
|
+
timeout: 6e4,
|
3306
|
+
pollingInterval: 5e3,
|
3307
|
+
maxPollingAttempts: 10,
|
3308
|
+
shouldStopPolling: (res2) => res2.result_code === 0
|
3309
|
+
}
|
3310
|
+
);
|
3289
3311
|
return res;
|
3290
3312
|
});
|
3291
3313
|
}
|
@@ -3304,61 +3326,141 @@ function getBtcGasPrice() {
|
|
3304
3326
|
}
|
3305
3327
|
function getBtcBalance() {
|
3306
3328
|
return __async(this, null, function* () {
|
3307
|
-
var _a;
|
3308
3329
|
const { account } = yield retryOperation(getBtcProvider, (res2) => !!res2.account);
|
3309
3330
|
if (!account) {
|
3310
3331
|
console.error("BTC Account is not available.");
|
3311
|
-
return { rawBalance: 0, balance: 0 };
|
3332
|
+
return { rawBalance: 0, balance: 0, maxSpendableBalance: 0 };
|
3312
3333
|
}
|
3313
3334
|
const btcRpcUrl = yield getBtcRpcUrl();
|
3314
3335
|
const res = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res2) => res2.json());
|
3315
|
-
const rawBalance =
|
3316
|
-
var _a2;
|
3317
|
-
return (_a2 = item == null ? void 0 : item.status) == null ? void 0 : _a2.confirmed;
|
3318
|
-
})) == null ? void 0 : _a.reduce((acc, cur) => acc + cur.value, 0);
|
3336
|
+
const rawBalance = res == null ? void 0 : res.reduce((acc, cur) => acc + cur.value, 0);
|
3319
3337
|
const balance = rawBalance / __pow(10, 8);
|
3320
|
-
|
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
|
+
};
|
3321
3346
|
});
|
3322
3347
|
}
|
3323
3348
|
function sendBitcoin(address, amount, feeRate) {
|
3324
3349
|
return __async(this, null, function* () {
|
3325
3350
|
const { sendBitcoin: sendBitcoin2 } = getBtcProvider();
|
3326
|
-
const
|
3327
|
-
const txHash = yield sendBitcoin2(address, satoshis, { feeRate });
|
3351
|
+
const txHash = yield sendBitcoin2(address, amount, { feeRate });
|
3328
3352
|
return txHash;
|
3329
3353
|
});
|
3330
3354
|
}
|
3331
|
-
function
|
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
|
+
}
|
3369
|
+
function executeBTCDepositAndAction(_0) {
|
3332
3370
|
return __async(this, arguments, function* ({
|
3333
|
-
|
3371
|
+
action,
|
3334
3372
|
feeRate,
|
3335
3373
|
isDev = false
|
3336
3374
|
}) {
|
3337
3375
|
try {
|
3338
3376
|
const { getPublicKey } = getBtcProvider();
|
3339
|
-
const
|
3340
|
-
const config = walletConfig[isDev ? "dev" : network];
|
3377
|
+
const config = yield getConfig(isDev);
|
3341
3378
|
const btcPublicKey = yield getPublicKey();
|
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
|
+
);
|
3342
3385
|
if (!btcPublicKey) {
|
3343
3386
|
throw new Error("BTC Public Key is not available.");
|
3344
3387
|
}
|
3345
|
-
|
3388
|
+
if (!_action.receiver_id) {
|
3389
|
+
throw new Error("action.receiver_id is required");
|
3390
|
+
}
|
3391
|
+
const amountWithFee = yield estimateDepositAmount(_action.amount, {
|
3392
|
+
isDev
|
3393
|
+
});
|
3394
|
+
_action.amount = amountWithFee;
|
3395
|
+
if (!_action.amount || !new import_big.default(_action.amount || 0).gt(0)) {
|
3396
|
+
throw new Error("action.amount is required or deposit amount is not enough");
|
3397
|
+
}
|
3398
|
+
const csna = yield nearCall(
|
3399
|
+
config.accountContractId,
|
3400
|
+
"get_chain_signature_near_account_id",
|
3401
|
+
{
|
3402
|
+
btc_public_key: btcPublicKey
|
3403
|
+
}
|
3404
|
+
);
|
3405
|
+
const depositMsg = {
|
3406
|
+
recipient_id: csna,
|
3407
|
+
post_actions: [_action]
|
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
|
+
}
|
3416
|
+
const registerRes = yield nearCall(action.receiver_id, "storage_balance_of", {
|
3417
|
+
account_id: csna
|
3418
|
+
});
|
3419
|
+
if (!(registerRes == null ? void 0 : registerRes.available)) {
|
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
|
3424
|
+
};
|
3425
|
+
}
|
3426
|
+
if (Object.keys(storageDepositMsg).length > 0) {
|
3427
|
+
depositMsg.extra_msg = JSON.stringify(storageDepositMsg);
|
3428
|
+
}
|
3429
|
+
console.log("deposit msg:", depositMsg);
|
3430
|
+
const userDepositAddress = yield nearCall(
|
3431
|
+
config.bridgeContractId,
|
3432
|
+
"get_user_deposit_address",
|
3433
|
+
{ deposit_msg: depositMsg }
|
3434
|
+
);
|
3346
3435
|
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3347
|
-
console.log("
|
3348
|
-
|
3349
|
-
|
3350
|
-
console.log("
|
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);
|
3440
|
+
const txHash = yield sendBitcoin(
|
3441
|
+
userDepositAddress,
|
3442
|
+
new import_big.default(action.amount).toNumber(),
|
3443
|
+
_feeRate
|
3444
|
+
);
|
3445
|
+
yield receiveDepositMsg(config.base_url, {
|
3446
|
+
btcPublicKey,
|
3447
|
+
txHash,
|
3448
|
+
postActions: JSON.stringify(depositMsg.post_actions),
|
3449
|
+
extraMsg: depositMsg.extra_msg || ""
|
3450
|
+
});
|
3351
3451
|
const checkTransactionStatusRes = yield checkTransactionStatus(config.base_url, txHash);
|
3352
3452
|
console.log("checkTransactionStatus resp:", checkTransactionStatusRes);
|
3453
|
+
return checkTransactionStatusRes.result_code === 0 ? { result: "success" } : { result: "failed", error: checkTransactionStatusRes.result_message };
|
3353
3454
|
} catch (error) {
|
3354
3455
|
console.error("Error executing Bridge+BurrowSupply:", error);
|
3456
|
+
return { result: "failed", error: error.message };
|
3355
3457
|
}
|
3356
3458
|
});
|
3357
3459
|
}
|
3358
3460
|
|
3359
3461
|
// src/index.ts
|
3360
3462
|
var getVersion = () => {
|
3361
|
-
return "0.3.
|
3463
|
+
return "0.3.7";
|
3362
3464
|
};
|
3363
3465
|
if (typeof window !== "undefined") {
|
3364
3466
|
window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
|