@@ -8,19 +8,20 @@ import { StellarDerivator } from "../derivation/stellar/index.ts";
88import type { StellarNetworkId } from "../derivation/stellar/stellar-network-id.ts" ;
99import {
1010 type ChannelInvokeMethods ,
11- type ChannelReadMethods ,
11+ ChannelReadMethods ,
1212 ChannelSpec ,
1313} from "./constants.ts" ;
1414import type { ChannelInvoke , ChannelRead } from "./types.ts" ;
1515import type { xdr } from "@stellar/stellar-sdk" ;
1616import * as E from "./error.ts" ;
17+ import type { UTXOPublicKey } from "../core/utxo-keypair-base/types.ts" ;
18+ import { Buffer } from "buffer" ;
1719
1820export class PrivacyChannel {
1921 private _client : Contract ;
2022 private _authId : ContractId ;
2123 private _assetId : ContractId ;
2224 private _networkConfig : NetworkConfig ;
23- private _derivator : StellarDerivator ;
2425
2526 public constructor (
2627 networkConfig : NetworkConfig ,
@@ -38,11 +39,6 @@ export class PrivacyChannel {
3839 this . _authId = authId ;
3940
4041 this . _assetId = assetId ;
41-
42- this . _derivator = new StellarDerivator ( ) . withNetworkAndContract (
43- networkConfig . networkPassphrase as StellarNetworkId ,
44- channelId as ContractId ,
45- ) ;
4642 }
4743
4844 //==========================================
@@ -61,11 +57,10 @@ export class PrivacyChannel {
6157 private require ( arg : "_client" ) : Contract ;
6258 private require ( arg : "_authId" ) : ContractId ;
6359 private require ( arg : "_networkConfig" ) : NetworkConfig ;
64- private require ( arg : "_derivator" ) : StellarDerivator ;
6560 private require ( arg : "_assetId" ) : ContractId ;
6661 private require (
67- arg : "_client" | "_authId" | "_networkConfig" | "_derivator" | " _assetId",
68- ) : Contract | ContractId | NetworkConfig | StellarDerivator {
62+ arg : "_client" | "_authId" | "_networkConfig" | "_assetId" ,
63+ ) : Contract | ContractId | NetworkConfig {
6964 if ( this [ arg ] ) return this [ arg ] ;
7065 throw new E . PROPERTY_NOT_SET ( arg ) ;
7166 }
@@ -125,10 +120,13 @@ export class PrivacyChannel {
125120 *
126121 * @params None
127122 * @returns {StellarDerivator } The StellarDerivator instance.
128- * @throws {Error } If the StellarDerivator instance is not set.
123+ * @throws {Error } If any of the underlying properties are not set.
129124 */
130125 public getDerivator ( ) : StellarDerivator {
131- return this . require ( "_derivator" ) ;
126+ return new StellarDerivator ( ) . withNetworkAndContract (
127+ this . getNetworkConfig ( ) . networkPassphrase as StellarNetworkId ,
128+ this . getChannelId ( ) as ContractId ,
129+ ) ;
132130 }
133131
134132 /**
@@ -142,8 +140,28 @@ export class PrivacyChannel {
142140 return this . getClient ( ) . getContractId ( ) ;
143141 }
144142
143+ /**
144+ * Returns a function that fetches balances for given UTXO public keys.
145+ *
146+ * @returns {(publicKeys: Uint8Array[]) => Promise<bigint[]> }
147+ */
148+ public getBalancesFetcher ( ) : (
149+ publicKeys : UTXOPublicKey [ ] ,
150+ ) => Promise < bigint [ ] > {
151+ const fetchBalances = async (
152+ publicKeys : UTXOPublicKey [ ] ,
153+ ) : Promise < bigint [ ] > => {
154+ return await this . read ( {
155+ method : ChannelReadMethods . utxo_balances ,
156+ methodArgs : { utxos : publicKeys . map ( ( pk ) => Buffer . from ( pk ) ) } ,
157+ } ) ;
158+ } ;
159+
160+ return fetchBalances ;
161+ }
162+
145163 //==========================================
146- // Read / Write Methods
164+ // Contract Read / Write Methods
147165 //==========================================
148166 //
149167 //
0 commit comments