1
- use candid:: Principal ;
2
- use ic_cdk:: api:: call:: call_with_payment;
3
1
use ic_cdk:: api:: management_canister:: bitcoin:: {
4
- BitcoinNetwork , GetBalanceRequest , GetCurrentFeePercentilesRequest , GetUtxosRequest ,
5
- GetUtxosResponse , MillisatoshiPerByte , Satoshi , SendTransactionRequest ,
2
+ bitcoin_get_balance, bitcoin_get_current_fee_percentiles, bitcoin_get_utxos,
3
+ bitcoin_send_transaction, BitcoinNetwork , GetBalanceRequest , GetCurrentFeePercentilesRequest ,
4
+ GetUtxosRequest , GetUtxosResponse , MillisatoshiPerByte , SendTransactionRequest ,
6
5
} ;
7
6
8
- // The fees for the various bitcoin endpoints.
9
- const GET_BALANCE_COST_CYCLES : u64 = 100_000_000 ;
10
- const GET_UTXOS_COST_CYCLES : u64 = 10_000_000_000 ;
11
- const GET_CURRENT_FEE_PERCENTILES_CYCLES : u64 = 100_000_000 ;
12
- const SEND_TRANSACTION_BASE_CYCLES : u64 = 5_000_000_000 ;
13
- const SEND_TRANSACTION_PER_BYTE_CYCLES : u64 = 20_000_000 ;
14
-
15
7
/// Returns the balance of the given bitcoin address.
16
8
///
17
9
/// Relies on the `bitcoin_get_balance` endpoint.
18
10
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_balance
19
11
pub async fn get_balance ( network : BitcoinNetwork , address : String ) -> u64 {
20
- let balance_res: Result < ( Satoshi , ) , _ > = call_with_payment (
21
- Principal :: management_canister ( ) ,
22
- "bitcoin_get_balance" ,
23
- ( GetBalanceRequest {
24
- address,
25
- network : network. into ( ) ,
26
- min_confirmations : None ,
27
- } , ) ,
28
- GET_BALANCE_COST_CYCLES ,
29
- )
12
+ let min_confirmations = None ;
13
+ let balance_res = bitcoin_get_balance ( GetBalanceRequest {
14
+ address,
15
+ network,
16
+ min_confirmations,
17
+ } )
30
18
. await ;
31
19
32
20
balance_res. unwrap ( ) . 0
@@ -37,16 +25,12 @@ pub async fn get_balance(network: BitcoinNetwork, address: String) -> u64 {
37
25
/// NOTE: Relies on the `bitcoin_get_utxos` endpoint.
38
26
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_utxos
39
27
pub async fn get_utxos ( network : BitcoinNetwork , address : String ) -> GetUtxosResponse {
40
- let utxos_res: Result < ( GetUtxosResponse , ) , _ > = call_with_payment (
41
- Principal :: management_canister ( ) ,
42
- "bitcoin_get_utxos" ,
43
- ( GetUtxosRequest {
44
- address,
45
- network : network. into ( ) ,
46
- filter : None ,
47
- } , ) ,
48
- GET_UTXOS_COST_CYCLES ,
49
- )
28
+ let filter = None ;
29
+ let utxos_res = bitcoin_get_utxos ( GetUtxosRequest {
30
+ address,
31
+ network,
32
+ filter,
33
+ } )
50
34
. await ;
51
35
52
36
utxos_res. unwrap ( ) . 0
@@ -58,15 +42,8 @@ pub async fn get_utxos(network: BitcoinNetwork, address: String) -> GetUtxosResp
58
42
/// Relies on the `bitcoin_get_current_fee_percentiles` endpoint.
59
43
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_current_fee_percentiles
60
44
pub async fn get_current_fee_percentiles ( network : BitcoinNetwork ) -> Vec < MillisatoshiPerByte > {
61
- let res: Result < ( Vec < MillisatoshiPerByte > , ) , _ > = call_with_payment (
62
- Principal :: management_canister ( ) ,
63
- "bitcoin_get_current_fee_percentiles" ,
64
- ( GetCurrentFeePercentilesRequest {
65
- network : network. into ( ) ,
66
- } , ) ,
67
- GET_CURRENT_FEE_PERCENTILES_CYCLES ,
68
- )
69
- . await ;
45
+ let res =
46
+ bitcoin_get_current_fee_percentiles ( GetCurrentFeePercentilesRequest { network } ) . await ;
70
47
71
48
res. unwrap ( ) . 0
72
49
}
@@ -76,18 +53,10 @@ pub async fn get_current_fee_percentiles(network: BitcoinNetwork) -> Vec<Millisa
76
53
/// Relies on the `bitcoin_send_transaction` endpoint.
77
54
/// See https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_send_transaction
78
55
pub async fn send_transaction ( network : BitcoinNetwork , transaction : Vec < u8 > ) {
79
- let transaction_fee = SEND_TRANSACTION_BASE_CYCLES
80
- + ( transaction. len ( ) as u64 ) * SEND_TRANSACTION_PER_BYTE_CYCLES ;
81
-
82
- let res: Result < ( ) , _ > = call_with_payment (
83
- Principal :: management_canister ( ) ,
84
- "bitcoin_send_transaction" ,
85
- ( SendTransactionRequest {
86
- network : network. into ( ) ,
87
- transaction,
88
- } , ) ,
89
- transaction_fee,
90
- )
56
+ let res = bitcoin_send_transaction ( SendTransactionRequest {
57
+ network,
58
+ transaction,
59
+ } )
91
60
. await ;
92
61
93
62
res. unwrap ( ) ;
0 commit comments