@@ -30,6 +30,23 @@ export function TestWasm () {
3030 } ) ) ;
3131}
3232
33+ // Test the spend helper.
34+ function TestSend ( amount = 3000n , fee = 12000n ) {
35+ return Fn . Name ( `Spend ${ amount } for ${ fee } ` , testSend ) ;
36+ async function testSend ( chain : Btc ) {
37+ const debug = chain . debug || console . debug ;
38+ const from = chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ;
39+ const to = chain . P2WPKH ( keypair2 . publicKey ( ) ) . address ;
40+ const utxo = await chain . getUtxo ( from ) ;
41+ return Object . assign ( chain , await SimplicityHL . Spend ( )
42+ . asset ( utxo . asset )
43+ . input ( utxo , keypair1 )
44+ . output ( to , amount )
45+ . fee ( fee ) . broadcast ( chain ) ) ;
46+ }
47+ }
48+
49+ // Self-explanatory.
3350export function TestOnTestnet ( ) {
3451 // Tests that touch Liquid Testnet using Esplora
3552 return Test ( 'liquidtestnet' , ( ) => LiquidTestnet ( ) ,
@@ -38,6 +55,7 @@ export function TestOnTestnet () {
3855 ) ;
3956}
4057
58+ // Self-explanatory.
4159export function TestOnLocalnet ( ) {
4260 // Tests that run on temporary localnet:
4361 return ElementsRegtest . Test ( { } ,
@@ -93,61 +111,6 @@ export function TestOnLocalnet () {
93111 ) ;
94112}
95113
96- function TestSend ( amount = 3000n , fee = 12000n ) {
97- return Fn . Name ( `Spend ${ amount } for ${ fee } ` , testSend ) ;
98- async function testSend ( chain : Btc ) {
99- const debug = chain . debug || console . debug ;
100- const from = chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ;
101- const to = chain . P2WPKH ( keypair2 . publicKey ( ) ) . address ;
102- const utxo = await findUtxo ( from ) ;
103- return Object . assign ( chain , await SimplicityHL . Spend ( )
104- . asset ( utxo . asset )
105- . input ( utxo , keypair1 )
106- . output ( to , amount )
107- . fee ( fee ) . broadcast ( chain ) ) ;
108-
109- async function sendSignedTransaction ( hex : string ) {
110- debug ( 'Broadcasting signed transaction:' , signed ) ;
111- if ( rpc && rest ) {
112- const id = await rpc ! . sendrawtransaction ( hex ) ;
113- await rpc ! . rescanblockchain ( ) ;
114- const tx = await rest ! . tx ( id ) ;
115- return tx ;
116- } else if ( esplora ) {
117- const id = await esplora . postTx ( hex ) ;
118- while ( true ) {
119- const mempool = await esplora . getMempoolTxids ( ) . then ( JSON . parse ) ;
120- if ( mempool . includes ( id ) ) {
121- debug ( 'TX still in mempool:' , id ) ;
122- await sleep ( 1000 ) ;
123- } else {
124- return esplora . getTxInfo ( id ) ;
125- }
126- }
127- } else {
128- throw new Error ( 'need { rpc, rest } or { esplora } to broadcast signed transaction' ) ;
129- }
130- }
131-
132- async function findUtxo ( address : string ) : { asset , txid , vout , amount , address } {
133- const { rpc, esplora } = chain ;
134- if ( rpc ) {
135- const unspent = await rpc . listunspent ( 0 , 9999999 , [ address ] ) ; // TODO filter
136- if ( ! unspent [ 0 ] ) throw new Error ( `no UTXOs for ${ address } ` ) ;
137- const { txid, vout, amount, asset } = unspent [ 0 ] ;
138- return { asset, txid, vout, address, amount } ;
139- } else if ( esplora ) {
140- const unspent = await esplora . getAddressUtxos ( address ) ;
141- if ( ! unspent [ 0 ] ) throw new Error ( `no UTXOs for ${ address } ` )
142- const { txid, vout, value, asset } = unspent [ 0 ] ;
143- return { asset, txid, vout, address, amount : BigInt ( value ) } ;
144- } else {
145- throw new Error ( 'need { rpc } or { esplora } to find unspent output' ) ;
146- }
147- }
148- }
149- }
150-
151114interface TestProgram extends Pick < Btc , 'rpc' | 'rest' | 'esplora' > {
152115 ID ,
153116 ASSETS ,
@@ -193,32 +156,14 @@ function TestProgram (name: string, src: string, {
193156 // Fund program from deployer:
194157 // TODO: Use sendSigned
195158 const commitAmount = 1_00000000n ;
196- const commitSource = await findUtxo ( chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ) ;
159+ const commitSource = await chain . getUtxo ( chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ) ;
197160 const commitTxid = await SimplicityHL . Spend ( )
198161 . asset ( commitSource . asset )
199162 . input ( commitSource , keypair1 )
200163 . output ( p2tr , commitAmount )
201164 . fee ( fee )
202165 . broadcast ( chain ) ;
203166
204- async function findUtxo ( address : string ) : { asset , txid , vout , amount , address } {
205- const { rpc, esplora } = chain ;
206- if ( rpc ) {
207- const unspent = await rpc . listunspent ( 0 , 9999999 , [ address ] ) ; // TODO filter
208- if ( ! unspent [ 0 ] ) throw new Error ( `no UTXOs for ${ address } ` ) ;
209- const { txid, vout, amount, asset } = unspent [ 0 ] ;
210- return { asset, txid, vout, address, amount } ;
211- } else if ( esplora ) {
212- const unspent = await esplora . getAddressUtxos ( address ) ;
213- if ( ! unspent [ 0 ] ) throw new Error ( `no UTXOs for ${ address } ` )
214- const { txid, vout, value, asset } = unspent [ 0 ] ;
215- return { asset, txid, vout, address, amount : BigInt ( value ) } ;
216- } else {
217- throw new Error ( 'need { rpc } or { esplora } to find unspent output' ) ;
218- }
219- }
220-
221-
222167 // Create local spender wallet and import it to RPC:
223168 const recipient = chain . P2WPKH ( keypair1 . publicKey ( ) ) . address ;
224169 await rpc . importaddress ( recipient ) ;
0 commit comments