btc-wallet 0.3.4 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/config.d.ts +2 -1
- package/dist/core/btcUtils.d.ts +14 -5
- package/dist/core/btcWalletSelectorContext.d.ts +2 -2
- package/dist/index.js +224 -140
- package/dist/index.js.map +4 -4
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/nearUtils.d.ts +7 -0
- package/esm/index.js +223 -139
- 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
@@ -3,14 +3,23 @@ export declare function getBtcBalance(): Promise<{
|
|
3
3
|
rawBalance: any;
|
4
4
|
balance: number;
|
5
5
|
}>;
|
6
|
-
export declare function sendBitcoin(address: string, amount:
|
7
|
-
interface
|
8
|
-
|
9
|
-
|
6
|
+
export declare function sendBitcoin(address: string, amount: number, feeRate: number): Promise<string>;
|
7
|
+
interface ExecuteBTCDepositAndActionParams {
|
8
|
+
action: {
|
9
|
+
receiver_id: string;
|
10
|
+
amount: string;
|
11
|
+
msg: string;
|
12
|
+
};
|
10
13
|
/** fee rate, if not provided, will use the recommended fee rate from the btc node */
|
11
14
|
feeRate?: number;
|
12
15
|
/** is dev environment */
|
13
16
|
isDev?: boolean;
|
14
17
|
}
|
15
|
-
export declare function
|
18
|
+
export declare function executeBTCDepositAndAction({ action, feeRate, isDev, }: ExecuteBTCDepositAndActionParams): Promise<{
|
19
|
+
result: string;
|
20
|
+
error?: undefined;
|
21
|
+
} | {
|
22
|
+
result: string;
|
23
|
+
error: any;
|
24
|
+
}>;
|
16
25
|
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,7 @@ __export(src_exports, {
|
|
90
90
|
UnisatConnector: () => UnisatConnector,
|
91
91
|
WizzConnector: () => WizzConnector,
|
92
92
|
XverseConnector: () => XverseConnector,
|
93
|
-
|
93
|
+
executeBTCDepositAndAction: () => executeBTCDepositAndAction,
|
94
94
|
getBtcBalance: () => getBtcBalance,
|
95
95
|
getBtcGasPrice: () => getBtcGasPrice,
|
96
96
|
getVersion: () => getVersion,
|
@@ -523,7 +523,7 @@ var _network2, _event2;
|
|
523
523
|
var MagicEdenConnector = class extends BaseConnector {
|
524
524
|
constructor() {
|
525
525
|
super();
|
526
|
-
__privateAdd(this, _network2, "
|
526
|
+
__privateAdd(this, _network2, "Mainnet");
|
527
527
|
__privateAdd(this, _event2, new import_events2.default());
|
528
528
|
this.metadata = {
|
529
529
|
id: "magicEden",
|
@@ -1362,6 +1362,27 @@ var checkBTCVersion = (accountContracts, accountContractKey, version) => {
|
|
1362
1362
|
return accountContracts[accountContractKey].some((item) => item.version === version);
|
1363
1363
|
};
|
1364
1364
|
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
1365
|
+
function retryOperation(_0, _1) {
|
1366
|
+
return __async(this, arguments, function* (operation, shouldStop, {
|
1367
|
+
maxRetries = 3,
|
1368
|
+
delayMs = 1e3
|
1369
|
+
} = {}) {
|
1370
|
+
let retries = 0;
|
1371
|
+
while (retries <= maxRetries) {
|
1372
|
+
const result = yield operation();
|
1373
|
+
if (shouldStop(result)) {
|
1374
|
+
return result;
|
1375
|
+
}
|
1376
|
+
if (retries === maxRetries) {
|
1377
|
+
console.warn("Max retries reached");
|
1378
|
+
return result;
|
1379
|
+
}
|
1380
|
+
retries++;
|
1381
|
+
yield delay(delayMs);
|
1382
|
+
}
|
1383
|
+
throw new Error("Unexpected execution path");
|
1384
|
+
});
|
1385
|
+
}
|
1365
1386
|
|
1366
1387
|
// src/utils/ethereumUtils.ts
|
1367
1388
|
var import_util2 = require("@ethereumjs/util");
|
@@ -2476,7 +2497,7 @@ function useBtcWalletSelector() {
|
|
2476
2497
|
publicKey.current = res;
|
2477
2498
|
});
|
2478
2499
|
}
|
2479
|
-
}, [provider]);
|
2500
|
+
}, [getPublicKey, provider]);
|
2480
2501
|
(0, import_react11.useEffect)(() => {
|
2481
2502
|
signMessageFn.current = signMessage;
|
2482
2503
|
}, [signMessage]);
|
@@ -2498,18 +2519,15 @@ function useBtcWalletSelector() {
|
|
2498
2519
|
connector.removeListener("accountsChanged", fn);
|
2499
2520
|
}
|
2500
2521
|
};
|
2501
|
-
}, [connector]);
|
2522
|
+
}, [connector, context, getPublicKey]);
|
2502
2523
|
const hook = (0, import_react11.useMemo)(() => {
|
2503
2524
|
return {
|
2504
2525
|
login: () => __async(this, null, function* () {
|
2505
|
-
const account = accounts
|
2506
|
-
if (account) {
|
2507
|
-
|
2508
|
-
}
|
2509
|
-
if (openConnectModal) {
|
2510
|
-
yield openConnectModal();
|
2526
|
+
const account = accounts == null ? void 0 : accounts[0];
|
2527
|
+
if (!account) {
|
2528
|
+
openConnectModal == null ? void 0 : openConnectModal();
|
2511
2529
|
}
|
2512
|
-
return
|
2530
|
+
return account;
|
2513
2531
|
}),
|
2514
2532
|
autoConnect: () => __async(this, null, function* () {
|
2515
2533
|
requestDirectAccount(connectorRef.current).catch((e) => {
|
@@ -2517,13 +2535,13 @@ function useBtcWalletSelector() {
|
|
2517
2535
|
});
|
2518
2536
|
}),
|
2519
2537
|
logout: () => {
|
2520
|
-
const accountId = accounts
|
2538
|
+
const accountId = accounts == null ? void 0 : accounts[0];
|
2521
2539
|
if (!accountId)
|
2522
2540
|
return;
|
2523
2541
|
disconnect == null ? void 0 : disconnect();
|
2524
2542
|
context.emit("btcLogOut");
|
2525
2543
|
},
|
2526
|
-
account: accounts
|
2544
|
+
account: accounts == null ? void 0 : accounts[0],
|
2527
2545
|
getPublicKey: () => {
|
2528
2546
|
return publicKey.current;
|
2529
2547
|
},
|
@@ -2549,7 +2567,7 @@ function useBtcWalletSelector() {
|
|
2549
2567
|
}
|
2550
2568
|
|
2551
2569
|
// src/core/setupBTCWallet.ts
|
2552
|
-
var
|
2570
|
+
var import_near_api_js2 = require("near-api-js");
|
2553
2571
|
var import_transactions = require("@near-js/transactions");
|
2554
2572
|
var import_key_pair = require("near-api-js/lib/utils/key_pair");
|
2555
2573
|
var import_transaction = require("near-api-js/lib/transaction");
|
@@ -2704,20 +2722,23 @@ function removeWalletButton() {
|
|
2704
2722
|
var walletConfig = {
|
2705
2723
|
dev: {
|
2706
2724
|
base_url: "https://api.dev.satoshibridge.top",
|
2707
|
-
token: "
|
2708
|
-
|
2725
|
+
token: "nbtc-dev.testnet",
|
2726
|
+
accountContractId: "acc-dev.testnet",
|
2727
|
+
bridgeContractId: "brg-dev.testnet",
|
2709
2728
|
walletUrl: "https://wallet-dev.satoshibridge.top"
|
2710
2729
|
},
|
2711
2730
|
testnet: {
|
2712
2731
|
base_url: "https://api.testnet.satoshibridge.top",
|
2713
2732
|
token: "nbtc2-nsp.testnet",
|
2714
|
-
|
2733
|
+
accountContractId: "dev2-nsp.testnet",
|
2734
|
+
bridgeContractId: "brg2-nsp.testnet",
|
2715
2735
|
walletUrl: "https://wallet-test.satoshibridge.top"
|
2716
2736
|
},
|
2717
2737
|
mainnet: {
|
2718
2738
|
base_url: "https://api.mainnet.satoshibridge.top",
|
2719
2739
|
token: "",
|
2720
|
-
|
2740
|
+
accountContractId: "",
|
2741
|
+
bridgeContractId: "",
|
2721
2742
|
walletUrl: "https://wallet.satoshibridge.top"
|
2722
2743
|
}
|
2723
2744
|
};
|
@@ -2729,8 +2750,8 @@ var nearRpcUrls = {
|
|
2729
2750
|
"https://near.drpc.org"
|
2730
2751
|
],
|
2731
2752
|
testnet: [
|
2732
|
-
"https://near-testnet.lava.build",
|
2733
2753
|
"https://rpc.testnet.near.org",
|
2754
|
+
"https://near-testnet.lava.build",
|
2734
2755
|
"https://near-testnet.drpc.org"
|
2735
2756
|
]
|
2736
2757
|
};
|
@@ -2768,7 +2789,7 @@ function request(url, options) {
|
|
2768
2789
|
body,
|
2769
2790
|
method
|
2770
2791
|
});
|
2771
|
-
const
|
2792
|
+
const retryCount = (_a = options == null ? void 0 : options.retryCount) != null ? _a : 1;
|
2772
2793
|
const controller = new AbortController();
|
2773
2794
|
const timeout = (options == null ? void 0 : options.timeout) || 2e4;
|
2774
2795
|
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
@@ -2791,16 +2812,16 @@ function request(url, options) {
|
|
2791
2812
|
return data;
|
2792
2813
|
} catch (err) {
|
2793
2814
|
console.error(err);
|
2794
|
-
if (
|
2795
|
-
console.log(`Retrying... attempts left: ${
|
2796
|
-
return request(url, __spreadProps(__spreadValues({}, options), { retryCount:
|
2815
|
+
if (retryCount > 0) {
|
2816
|
+
console.log(`Retrying... attempts left: ${retryCount}`);
|
2817
|
+
return request(url, __spreadProps(__spreadValues({}, options), { retryCount: retryCount - 1 }));
|
2797
2818
|
} else if ((options == null ? void 0 : options.pollingInterval) && (options == null ? void 0 : options.maxPollingAttempts)) {
|
2798
2819
|
if (options.maxPollingAttempts > 0) {
|
2799
2820
|
console.log(`Polling... attempts left: ${options.maxPollingAttempts}`);
|
2800
2821
|
yield new Promise((resolve) => setTimeout(resolve, options.pollingInterval));
|
2801
2822
|
return request(url, __spreadProps(__spreadValues({}, options), {
|
2802
2823
|
maxPollingAttempts: options.maxPollingAttempts - 1,
|
2803
|
-
retryCount
|
2824
|
+
retryCount
|
2804
2825
|
}));
|
2805
2826
|
}
|
2806
2827
|
}
|
@@ -2809,6 +2830,58 @@ function request(url, options) {
|
|
2809
2830
|
});
|
2810
2831
|
}
|
2811
2832
|
|
2833
|
+
// src/utils/nearUtils.ts
|
2834
|
+
var import_near_api_js = require("near-api-js");
|
2835
|
+
function nearCallFunction(contractId, methodName, args, options) {
|
2836
|
+
return __async(this, null, function* () {
|
2837
|
+
const nearProvider = (options == null ? void 0 : options.provider) || new import_near_api_js.providers.FailoverRpcProvider(
|
2838
|
+
nearRpcUrls[options == null ? void 0 : options.network].map(
|
2839
|
+
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2840
|
+
)
|
2841
|
+
);
|
2842
|
+
const res = yield nearProvider.query({
|
2843
|
+
request_type: "call_function",
|
2844
|
+
account_id: contractId,
|
2845
|
+
method_name: methodName,
|
2846
|
+
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2847
|
+
finality: "final"
|
2848
|
+
});
|
2849
|
+
return JSON.parse(Buffer.from(res.result).toString());
|
2850
|
+
});
|
2851
|
+
}
|
2852
|
+
function pollTransactionStatuses(network, hashes) {
|
2853
|
+
return __async(this, null, function* () {
|
2854
|
+
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
2855
|
+
Object.values(nearRpcUrls[network]).map(
|
2856
|
+
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
2857
|
+
)
|
2858
|
+
);
|
2859
|
+
const maxAttempts = 3;
|
2860
|
+
const pollStatus = (hash) => __async(this, null, function* () {
|
2861
|
+
let attempt = 0;
|
2862
|
+
while (attempt < maxAttempts) {
|
2863
|
+
attempt++;
|
2864
|
+
try {
|
2865
|
+
const result = yield provider.txStatus(hash, "unused", "FINAL");
|
2866
|
+
if (result && result.status) {
|
2867
|
+
console.log(`Transaction ${hash} result:`, result);
|
2868
|
+
return result;
|
2869
|
+
}
|
2870
|
+
} catch (error) {
|
2871
|
+
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
2872
|
+
}
|
2873
|
+
if (attempt === maxAttempts) {
|
2874
|
+
throw new Error(`Transaction not found after max attempts: ${hash}`);
|
2875
|
+
}
|
2876
|
+
yield delay(1e4);
|
2877
|
+
console.log(`RPC request failed for ${hash}, retrying ${maxAttempts - attempt} more times`);
|
2878
|
+
}
|
2879
|
+
});
|
2880
|
+
const results = yield Promise.all(hashes.map((hash) => pollStatus(hash)));
|
2881
|
+
return results;
|
2882
|
+
});
|
2883
|
+
}
|
2884
|
+
|
2812
2885
|
// src/core/setupBTCWallet.ts
|
2813
2886
|
var { transfer, functionCall } = import_transactions.actionCreators;
|
2814
2887
|
var state = {
|
@@ -2906,28 +2979,23 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2906
2979
|
}
|
2907
2980
|
}), 500);
|
2908
2981
|
}
|
2909
|
-
function
|
2910
|
-
return __async(this,
|
2911
|
-
|
2912
|
-
request_type: "call_function",
|
2913
|
-
account_id: currentConfig.contractId,
|
2914
|
-
method_name: method,
|
2915
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
2916
|
-
finality: "optimistic"
|
2917
|
-
});
|
2918
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
2982
|
+
function nearCall2(contractId, methodName, args) {
|
2983
|
+
return __async(this, null, function* () {
|
2984
|
+
return nearCallFunction(contractId, methodName, args, { provider });
|
2919
2985
|
});
|
2920
2986
|
}
|
2921
2987
|
function getNearAccountByBtcPublicKey(btcPublicKey) {
|
2922
2988
|
return __async(this, null, function* () {
|
2923
|
-
const nearTempAddress = yield
|
2924
|
-
|
2925
|
-
|
2926
|
-
|
2927
|
-
|
2928
|
-
|
2929
|
-
|
2930
|
-
|
2989
|
+
const nearTempAddress = yield nearCall2(
|
2990
|
+
currentConfig.accountContractId,
|
2991
|
+
"get_chain_signature_near_account_id",
|
2992
|
+
{ btc_public_key: btcPublicKey }
|
2993
|
+
);
|
2994
|
+
const nearTempPublicKey = yield nearCall2(
|
2995
|
+
currentConfig.accountContractId,
|
2996
|
+
"get_chain_signature_near_account_public_key",
|
2997
|
+
{ btc_public_key: btcPublicKey }
|
2998
|
+
);
|
2931
2999
|
state.saveAccount(nearTempAddress);
|
2932
3000
|
state.savePublicKey(nearTempPublicKey);
|
2933
3001
|
state.saveBtcPublicKey(btcPublicKey);
|
@@ -2951,8 +3019,15 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
2951
3019
|
}
|
2952
3020
|
];
|
2953
3021
|
}
|
2954
|
-
|
2955
|
-
const btcPublicKey = yield btcContext.getPublicKey()
|
3022
|
+
yield btcContext.login();
|
3023
|
+
const btcPublicKey = yield retryOperation(btcContext.getPublicKey, (res) => !!res, {
|
3024
|
+
maxRetries: 40,
|
3025
|
+
delayMs: 3e3
|
3026
|
+
});
|
3027
|
+
console.log("btcPublicKey:", btcPublicKey);
|
3028
|
+
if (!btcPublicKey) {
|
3029
|
+
throw new Error("No connected BTC wallet, please connect your BTC wallet first.");
|
3030
|
+
}
|
2956
3031
|
const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
|
2957
3032
|
return [
|
2958
3033
|
{
|
@@ -3020,11 +3095,14 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3020
3095
|
nonce: BigInt(rawAccessKey.nonce || 0)
|
3021
3096
|
});
|
3022
3097
|
const publicKeyFormat = import_key_pair.PublicKey.from(publicKey);
|
3023
|
-
const
|
3098
|
+
const { result_data: nearNonceFromApi } = yield getNearNonceFromApi(
|
3099
|
+
currentConfig.base_url,
|
3100
|
+
accountId
|
3101
|
+
);
|
3024
3102
|
const newTransactions = params.transactions.map((transaction, index) => {
|
3025
3103
|
let nearNonceNumber = accessKey.nonce + BigInt(1);
|
3026
|
-
if (
|
3027
|
-
nearNonceNumber = BigInt(
|
3104
|
+
if (nearNonceFromApi) {
|
3105
|
+
nearNonceNumber = BigInt(nearNonceFromApi) > nearNonceNumber ? BigInt(nearNonceFromApi) : nearNonceNumber;
|
3028
3106
|
}
|
3029
3107
|
const newActions = transaction.actions.map((action) => {
|
3030
3108
|
switch (action.type) {
|
@@ -3039,7 +3117,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3039
3117
|
return transfer(BigInt(action.params.deposit));
|
3040
3118
|
}
|
3041
3119
|
}).filter(Boolean);
|
3042
|
-
const _transaction =
|
3120
|
+
const _transaction = import_near_api_js2.transactions.createTransaction(
|
3043
3121
|
accountId,
|
3044
3122
|
publicKeyFormat,
|
3045
3123
|
transaction.receiverId,
|
@@ -3056,20 +3134,34 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
|
|
3056
3134
|
const hash = import_bs58.default.encode(new Uint8Array(import_js_sha256.sha256.array(txBytes)));
|
3057
3135
|
return { txBytes, txHex, hash };
|
3058
3136
|
});
|
3059
|
-
const accountInfo = yield
|
3060
|
-
|
3061
|
-
|
3062
|
-
|
3063
|
-
|
3064
|
-
const
|
3137
|
+
const accountInfo = yield nearCall2(
|
3138
|
+
currentConfig.accountContractId,
|
3139
|
+
"get_account",
|
3140
|
+
{ account_id: accountId }
|
3141
|
+
);
|
3142
|
+
const { result_data: nonceFromApi } = yield getNonceFromApi(
|
3143
|
+
currentConfig.base_url,
|
3144
|
+
accountId
|
3145
|
+
);
|
3146
|
+
const nonce = Number(nonceFromApi) > Number(accountInfo.nonce) ? String(nonceFromApi) : String(accountInfo.nonce);
|
3065
3147
|
const intention = {
|
3066
3148
|
chain_id: "397",
|
3067
3149
|
csna: accountId,
|
3068
3150
|
near_transactions: newTransactions.map((t) => t.txHex),
|
3069
3151
|
gas_token: currentConfig.token,
|
3070
3152
|
gas_limit: "3000",
|
3153
|
+
use_near_pay_gas: false,
|
3071
3154
|
nonce
|
3072
3155
|
};
|
3156
|
+
const nearAccount = yield provider.query({
|
3157
|
+
request_type: "view_account",
|
3158
|
+
account_id: accountId,
|
3159
|
+
finality: "final"
|
3160
|
+
});
|
3161
|
+
const availableBalance = parseFloat(nearAccount.amount) / __pow(10, 24);
|
3162
|
+
if (availableBalance > 0.2) {
|
3163
|
+
intention.use_near_pay_gas = true;
|
3164
|
+
}
|
3073
3165
|
const strIntention = JSON.stringify(intention);
|
3074
3166
|
const signature = yield btcContext.signMessage(strIntention);
|
3075
3167
|
const result = yield uploadBTCTx(currentConfig.base_url, {
|
@@ -3154,41 +3246,8 @@ function toHex(originalString) {
|
|
3154
3246
|
hexString = hexString.replace(/(^0+)/g, "");
|
3155
3247
|
return hexString;
|
3156
3248
|
}
|
3157
|
-
function pollTransactionStatuses(network, hashes) {
|
3158
|
-
return __async(this, null, function* () {
|
3159
|
-
const provider = new import_near_api_js.providers.FailoverRpcProvider(
|
3160
|
-
Object.values(nearRpcUrls[network]).map(
|
3161
|
-
(url) => new import_near_api_js.providers.JsonRpcProvider({ url })
|
3162
|
-
)
|
3163
|
-
);
|
3164
|
-
const maxAttempts = 3;
|
3165
|
-
const pollStatus = (hash) => __async(this, null, function* () {
|
3166
|
-
let attempt = 0;
|
3167
|
-
while (attempt < maxAttempts) {
|
3168
|
-
attempt++;
|
3169
|
-
try {
|
3170
|
-
const result = yield provider.txStatus(hash, "unused", "FINAL");
|
3171
|
-
if (result && result.status) {
|
3172
|
-
console.log(`Transaction ${hash} result:`, result);
|
3173
|
-
return result;
|
3174
|
-
}
|
3175
|
-
} catch (error) {
|
3176
|
-
console.error(`Failed to fetch transaction status for ${hash}: ${error.message}`);
|
3177
|
-
}
|
3178
|
-
if (attempt === maxAttempts) {
|
3179
|
-
throw new Error(`Transaction not found after max attempts: ${hash}`);
|
3180
|
-
}
|
3181
|
-
yield delay(1e4);
|
3182
|
-
console.log(`RPC request failed for ${hash}, retrying ${maxAttempts - attempt} more times`);
|
3183
|
-
}
|
3184
|
-
});
|
3185
|
-
const results = yield Promise.all(hashes.map((hash) => pollStatus(hash)));
|
3186
|
-
return results;
|
3187
|
-
});
|
3188
|
-
}
|
3189
3249
|
|
3190
3250
|
// src/core/btcUtils.ts
|
3191
|
-
var import_near_api_js2 = require("near-api-js");
|
3192
3251
|
var import_big = __toESM(require("big.js"), 1);
|
3193
3252
|
function getBtcProvider() {
|
3194
3253
|
if (typeof window === "undefined" || !window.btcContext) {
|
@@ -3209,55 +3268,39 @@ function getBtcRpcUrl() {
|
|
3209
3268
|
return btcRpcUrls[network];
|
3210
3269
|
});
|
3211
3270
|
}
|
3212
|
-
function
|
3271
|
+
function nearCall(contractId, methodName, args) {
|
3213
3272
|
return __async(this, null, function* () {
|
3214
3273
|
const network = yield getNetwork();
|
3215
|
-
|
3216
|
-
nearRpcUrls[network].map(
|
3217
|
-
(url) => new import_near_api_js2.providers.JsonRpcProvider({ url })
|
3218
|
-
)
|
3219
|
-
);
|
3220
|
-
const res = yield nearProvider.query({
|
3221
|
-
request_type: "call_function",
|
3222
|
-
account_id: contractId,
|
3223
|
-
method_name: methodName,
|
3224
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString("base64"),
|
3225
|
-
finality: "optimistic"
|
3226
|
-
});
|
3227
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
3228
|
-
});
|
3229
|
-
}
|
3230
|
-
function getDepositAddress(btcPublicKey, contractId) {
|
3231
|
-
return __async(this, null, function* () {
|
3232
|
-
const res = yield nearViewMethod(contractId, "get_user_dapp_deposit_address", {
|
3233
|
-
deposit_type: {
|
3234
|
-
BtcPublicKey: { btc_public_key: btcPublicKey, dapp_operation: "Burrowland->Supply" }
|
3235
|
-
}
|
3236
|
-
});
|
3237
|
-
return res;
|
3274
|
+
return nearCallFunction(contractId, methodName, args, { network });
|
3238
3275
|
});
|
3239
3276
|
}
|
3240
3277
|
function receiveDepositMsg(_0, _1) {
|
3241
3278
|
return __async(this, arguments, function* (baseUrl, {
|
3242
3279
|
btcPublicKey,
|
3243
3280
|
txHash,
|
3244
|
-
depositType = 1
|
3281
|
+
depositType = 1,
|
3282
|
+
postActions,
|
3283
|
+
extraMsg
|
3245
3284
|
}) {
|
3246
3285
|
const res = yield request(`${baseUrl}/v1/receiveDepositMsg`, {
|
3247
3286
|
method: "POST",
|
3248
|
-
body: { btcPublicKey, txHash, depositType }
|
3287
|
+
body: { btcPublicKey, txHash, depositType, postActions, extraMsg }
|
3249
3288
|
});
|
3289
|
+
console.log("receiveDepositMsg resp:", res);
|
3250
3290
|
return res;
|
3251
3291
|
});
|
3252
3292
|
}
|
3253
3293
|
function checkTransactionStatus(baseUrl, txHash) {
|
3254
3294
|
return __async(this, null, function* () {
|
3255
|
-
const res = yield request(
|
3256
|
-
|
3257
|
-
|
3258
|
-
|
3259
|
-
|
3260
|
-
|
3295
|
+
const res = yield request(
|
3296
|
+
`${baseUrl}/v1/bridgeFromTx?fromTxHash=${txHash}&fromChainId=1`,
|
3297
|
+
{
|
3298
|
+
timeout: 6e4,
|
3299
|
+
pollingInterval: 5e3,
|
3300
|
+
maxPollingAttempts: 10,
|
3301
|
+
shouldStopPolling: (res2) => res2.result_code === 0
|
3302
|
+
}
|
3303
|
+
);
|
3261
3304
|
return res;
|
3262
3305
|
});
|
3263
3306
|
}
|
@@ -3274,38 +3317,30 @@ function getBtcGasPrice() {
|
|
3274
3317
|
}
|
3275
3318
|
});
|
3276
3319
|
}
|
3277
|
-
var retryCount = 0;
|
3278
3320
|
function getBtcBalance() {
|
3279
3321
|
return __async(this, null, function* () {
|
3280
|
-
const { account } = getBtcProvider();
|
3322
|
+
const { account } = yield retryOperation(getBtcProvider, (res2) => !!res2.account);
|
3281
3323
|
if (!account) {
|
3282
|
-
|
3283
|
-
|
3284
|
-
throw new Error("BTC Account is not available.");
|
3285
|
-
}
|
3286
|
-
yield delay(1e3);
|
3287
|
-
return getBtcBalance();
|
3324
|
+
console.error("BTC Account is not available.");
|
3325
|
+
return { rawBalance: 0, balance: 0 };
|
3288
3326
|
}
|
3289
|
-
retryCount = 0;
|
3290
3327
|
const btcRpcUrl = yield getBtcRpcUrl();
|
3291
3328
|
const res = yield fetch(`${btcRpcUrl}/address/${account}/utxo`).then((res2) => res2.json());
|
3292
|
-
const rawBalance = res.reduce((acc, cur) => acc + cur.value, 0);
|
3329
|
+
const rawBalance = res == null ? void 0 : res.reduce((acc, cur) => acc + cur.value, 0);
|
3293
3330
|
const balance = rawBalance / __pow(10, 8);
|
3294
|
-
console.log("btc balance:", balance);
|
3295
3331
|
return { rawBalance, balance };
|
3296
3332
|
});
|
3297
3333
|
}
|
3298
3334
|
function sendBitcoin(address, amount, feeRate) {
|
3299
3335
|
return __async(this, null, function* () {
|
3300
3336
|
const { sendBitcoin: sendBitcoin2 } = getBtcProvider();
|
3301
|
-
const
|
3302
|
-
const txHash = yield sendBitcoin2(address, satoshis, { feeRate });
|
3337
|
+
const txHash = yield sendBitcoin2(address, amount, { feeRate });
|
3303
3338
|
return txHash;
|
3304
3339
|
});
|
3305
3340
|
}
|
3306
|
-
function
|
3341
|
+
function executeBTCDepositAndAction(_0) {
|
3307
3342
|
return __async(this, arguments, function* ({
|
3308
|
-
|
3343
|
+
action,
|
3309
3344
|
feeRate,
|
3310
3345
|
isDev = false
|
3311
3346
|
}) {
|
@@ -3314,26 +3349,75 @@ function executeBurrowSupply(_0) {
|
|
3314
3349
|
const network = yield getNetwork();
|
3315
3350
|
const config = walletConfig[isDev ? "dev" : network];
|
3316
3351
|
const btcPublicKey = yield getPublicKey();
|
3352
|
+
const _action = Object.assign({}, action);
|
3317
3353
|
if (!btcPublicKey) {
|
3318
3354
|
throw new Error("BTC Public Key is not available.");
|
3319
3355
|
}
|
3320
|
-
|
3356
|
+
if (!_action.receiver_id) {
|
3357
|
+
throw new Error("action.receiver_id is required");
|
3358
|
+
}
|
3359
|
+
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");
|
3361
|
+
}
|
3362
|
+
const csna = yield nearCall(
|
3363
|
+
config.accountContractId,
|
3364
|
+
"get_chain_signature_near_account_id",
|
3365
|
+
{
|
3366
|
+
btc_public_key: btcPublicKey
|
3367
|
+
}
|
3368
|
+
);
|
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
|
+
const depositMsg = {
|
3372
|
+
recipient_id: csna,
|
3373
|
+
post_actions: [_action]
|
3374
|
+
};
|
3375
|
+
const registerRes = yield nearCall(action.receiver_id, "storage_balance_of", {
|
3376
|
+
account_id: csna
|
3377
|
+
});
|
3378
|
+
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
|
3386
|
+
};
|
3387
|
+
depositMsg.extra_msg = JSON.stringify(storageDepositMsg);
|
3388
|
+
}
|
3389
|
+
console.log("depositMsg", depositMsg);
|
3390
|
+
const userDepositAddress = yield nearCall(
|
3391
|
+
config.bridgeContractId,
|
3392
|
+
"get_user_deposit_address",
|
3393
|
+
{ deposit_msg: depositMsg }
|
3394
|
+
);
|
3395
|
+
console.log("userDepositAddress", userDepositAddress);
|
3321
3396
|
const _feeRate = feeRate || (yield getBtcGasPrice());
|
3322
|
-
|
3323
|
-
|
3324
|
-
|
3325
|
-
|
3397
|
+
const txHash = yield sendBitcoin(
|
3398
|
+
userDepositAddress,
|
3399
|
+
new import_big.default(action.amount).toNumber(),
|
3400
|
+
_feeRate
|
3401
|
+
);
|
3402
|
+
yield receiveDepositMsg(config.base_url, {
|
3403
|
+
btcPublicKey,
|
3404
|
+
txHash,
|
3405
|
+
postActions: JSON.stringify(depositMsg.post_actions),
|
3406
|
+
extraMsg: depositMsg.extra_msg || ""
|
3407
|
+
});
|
3326
3408
|
const checkTransactionStatusRes = yield checkTransactionStatus(config.base_url, txHash);
|
3327
3409
|
console.log("checkTransactionStatus resp:", checkTransactionStatusRes);
|
3410
|
+
return checkTransactionStatusRes.result_code === 0 ? { result: "success" } : { result: "failed", error: checkTransactionStatusRes.result_message };
|
3328
3411
|
} catch (error) {
|
3329
3412
|
console.error("Error executing Bridge+BurrowSupply:", error);
|
3413
|
+
return { result: "failed", error: error.message };
|
3330
3414
|
}
|
3331
3415
|
});
|
3332
3416
|
}
|
3333
3417
|
|
3334
3418
|
// src/index.ts
|
3335
3419
|
var getVersion = () => {
|
3336
|
-
return "0.3.
|
3420
|
+
return "0.3.6";
|
3337
3421
|
};
|
3338
3422
|
if (typeof window !== "undefined") {
|
3339
3423
|
window.__PARTICLE_BTC_CONNECT_VERSION = getVersion();
|