@@ -13,7 +13,9 @@ import {
1313 getThreadUtxo ,
1414 validateName ,
1515} from '../util/index.js' ;
16- import type { FetchClaimDomainUtxosParams , CreateClaimDomainTransactionParams } from '../interfaces/index.js' ;
16+ import type { FetchClaimDomainUtxosParams , CreateClaimDomainParams , FetchClaimDomainUtxosResponse } from '../interfaces/index.js' ;
17+ import { InvalidPrevBidderAddressError } from '../errors.js' ;
18+
1719
1820/**
1921 * Fetches UTXOs required for claiming a domain.
@@ -24,15 +26,15 @@ import type { FetchClaimDomainUtxosParams, CreateClaimDomainTransactionParams }
2426 * @param {Contract } params.domainFactoryContract - The domain factory contract instance.
2527 * @param {string } params.name - The name of the domain.
2628 * @param {NetworkProvider } params.networkProvider - The network provider for blockchain interactions.
27- * @returns {Promise<Object > } A promise that resolves to an object containing the necessary UTXOs.
29+ * @returns {Promise<FetchClaimDomainUtxosResponse > } A promise that resolves to an object containing the necessary UTXOs.
2830 */
2931export const fetchClaimDomainUtxos = async ( {
3032 category,
3133 registryContract,
3234 domainFactoryContract,
3335 name,
3436 networkProvider,
35- } : FetchClaimDomainUtxosParams ) : Promise < any > =>
37+ } : FetchClaimDomainUtxosParams ) : Promise < FetchClaimDomainUtxosResponse > =>
3638{
3739 const [ registryUtxos , domainFactoryUtxos ] = await Promise . all ( [
3840 networkProvider . getUtxos ( registryContract . address ) ,
@@ -75,58 +77,50 @@ export const fetchClaimDomainUtxos = async ({
7577 * and contracts. It ensures the domain name is valid, fetches necessary UTXOs if not provided,
7678 * and builds a transaction with multiple inputs and outputs to facilitate the domain claim process.
7779 *
78- * @param {CreateClaimDomainTransactionParams } params - The parameters required to create the claim domain transaction.
80+ * @param {CreateClaimDomainParams } params - The parameters required to create the claim domain transaction.
7981 * @param {string } params.category - The category of the domain.
8082 * @param {Contract } params.registryContract - The registry contract instance.
8183 * @param {Contract } params.domainFactoryContract - The domain factory contract instance.
8284 * @param {number } params.inactivityExpiryTime - The inactivity expiry time for the domain.
8385 * @param {number } params.maxPlatformFeePercentage - The maximum platform fee percentage.
8486 * @param {number } params.minWaitTime - The minimum wait time for the transaction.
8587 * @param {string } params.name - The name of the domain.
86- * @param {NetworkProvider } params.networkProvider - The network provider for blockchain interactions.
87- * @param {any } params.options - Additional options for the domain contract.
88+ * @param {object } params.options - Additional options for the domain contract.
8889 * @param {string } [params.platformFeeAddress] - The address to receive the platform fee, if specified.
89- * @param {any } [params.utxos] - The UTXOs to be used in the transaction, if already available.
90+ * @param {FetchClaimDomainUtxosResponse } [params.utxos] - The UTXOs to be used in the transaction, if already available.
9091 * @returns {Promise<TransactionBuilder> } A promise that resolves to the transaction builder.
9192 * @throws {InvalidNameError } If the domain name is invalid.
92- * @throws {Error } If the previous bidder address is invalid.
93+ * @throws {InvalidPrevBidderAddressError } If the previous bidder address is invalid.
9394 */
94- export const createClaimDomainTransaction = async ( {
95+ export const createClaimDomainTransactionCore = async ( {
9596 category,
9697 registryContract,
9798 domainFactoryContract,
9899 inactivityExpiryTime,
99100 maxPlatformFeePercentage,
100101 minWaitTime,
101102 name,
102- networkProvider,
103103 options,
104104 platformFeeAddress,
105105 utxos,
106- } : CreateClaimDomainTransactionParams ) : Promise < TransactionBuilder > =>
106+ } : CreateClaimDomainParams ) : Promise < TransactionBuilder > =>
107107{
108108 validateName ( name ) ;
109109 const { nameBin } = convertNameToBinaryAndHex ( name ) ;
110110
111- if ( ! utxos )
112- {
113- utxos = await fetchClaimDomainUtxos ( {
114- category,
115- registryContract,
116- domainFactoryContract,
117- name,
118- networkProvider,
119- } ) ;
120- }
121111 const { threadNFTUTXO, authorizedContractUTXO, domainMintingUTXO, runningAuctionUTXO } = utxos ;
122112
123113 const bidderPKH = runningAuctionUTXO . token ?. nft ?. commitment . slice ( 0 , 40 ) ;
114+ if ( ! bidderPKH )
115+ {
116+ throw new InvalidPrevBidderAddressError ( ) ;
117+ }
124118 const bidderLockingBytecode = convertPkhToLockingBytecode ( bidderPKH ) ;
125119 const bidderAddressResult = lockingBytecodeToCashAddress ( { bytecode : bidderLockingBytecode } ) ;
126120
127121 if ( typeof bidderAddressResult !== 'object' || ! bidderAddressResult . address )
128122 {
129- throw new Error ( 'Invalid prev bidder address' ) ;
123+ throw new InvalidPrevBidderAddressError ( ) ;
130124 }
131125
132126 const bidderAddress = bidderAddressResult . address ;
@@ -140,7 +134,7 @@ export const createClaimDomainTransaction = async ({
140134
141135 const registrationId = createRegistrationId ( runningAuctionUTXO ) ;
142136
143- const transaction = await new TransactionBuilder ( { provider : networkProvider } )
137+ const transaction = await new TransactionBuilder ( { provider : options . provider } )
144138 . addInput ( threadNFTUTXO , registryContract . unlock . call ( ) )
145139 . addInput ( authorizedContractUTXO , domainFactoryContract . unlock . call ( ) )
146140 . addInput ( domainMintingUTXO , registryContract . unlock . call ( ) )
@@ -149,11 +143,11 @@ export const createClaimDomainTransaction = async ({
149143 to : registryContract . tokenAddress ,
150144 amount : threadNFTUTXO . satoshis ,
151145 token : {
152- category : threadNFTUTXO . token . category ,
153- amount : threadNFTUTXO . token . amount + runningAuctionUTXO . token . amount ,
146+ category : threadNFTUTXO . token ! . category ,
147+ amount : threadNFTUTXO . token ! . amount + runningAuctionUTXO . token ! . amount ,
154148 nft : {
155- capability : threadNFTUTXO . token . nft . capability ,
156- commitment : threadNFTUTXO . token . nft . commitment ,
149+ capability : threadNFTUTXO . token ! . nft ! . capability ,
150+ commitment : threadNFTUTXO . token ! . nft ! . commitment ,
157151 } ,
158152 } ,
159153 } )
@@ -165,19 +159,19 @@ export const createClaimDomainTransaction = async ({
165159 to : registryContract . tokenAddress ,
166160 amount : domainMintingUTXO . satoshis ,
167161 token : {
168- category : domainMintingUTXO . token . category ,
169- amount : domainMintingUTXO . token . amount ,
162+ category : domainMintingUTXO . token ! . category ,
163+ amount : domainMintingUTXO . token ! . amount ,
170164 nft : {
171- capability : domainMintingUTXO . token . nft . capability ,
172- commitment : domainMintingUTXO . token . nft . commitment ,
165+ capability : domainMintingUTXO . token ! . nft ! . capability ,
166+ commitment : domainMintingUTXO . token ! . nft ! . commitment ,
173167 } ,
174168 } ,
175169 } )
176170 . addOutput ( {
177171 to : domainContract . tokenAddress ,
178172 amount : BigInt ( 1000 ) ,
179173 token : {
180- category : domainMintingUTXO . token . category ,
174+ category : domainMintingUTXO . token ! . category ,
181175 amount : BigInt ( 0 ) ,
182176 nft : {
183177 capability : 'none' ,
@@ -189,7 +183,7 @@ export const createClaimDomainTransaction = async ({
189183 to : domainContract . tokenAddress ,
190184 amount : BigInt ( 1000 ) ,
191185 token : {
192- category : domainMintingUTXO . token . category ,
186+ category : domainMintingUTXO . token ! . category ,
193187 amount : BigInt ( 0 ) ,
194188 nft : {
195189 capability : 'none' ,
@@ -201,7 +195,7 @@ export const createClaimDomainTransaction = async ({
201195 to : convertCashAddressToTokenAddress ( bidderAddress ) ,
202196 amount : BigInt ( 1000 ) ,
203197 token : {
204- category : domainMintingUTXO . token . category ,
198+ category : domainMintingUTXO . token ! . category ,
205199 amount : BigInt ( 0 ) ,
206200 nft : {
207201 capability : 'none' ,
0 commit comments