1- import { parseUnits } from "viem" ;
1+ import { TransactionReceipt , parseUnits } from "viem" ;
22import ethereum from "../util/ethereum" ;
33import pocket from "../util/pocket" ;
44import { expect } from "chai" ;
55import { findInvalidMint , findMint } from "../util/mongodb" ;
6- import { MintMemo , Status } from "../types" ;
6+ import { Mint , MintMemo , Status } from "../types" ;
77import { sleep , debug } from "../util/helpers" ;
88
99type MessageSend = {
@@ -17,6 +17,8 @@ type MessageSend = {
1717
1818export const mintFlow = async ( ) => {
1919 it ( "should mint for send tx to vault with valid memo" , async ( ) => {
20+ debug ( "\nTesting -- should mint for send tx to vault with valid memo" ) ;
21+
2022 const signer = await pocket . signerPromise ;
2123 const fromAddress = await pocket . getAddress ( ) ;
2224 const recipientAddress = await ethereum . getAddress ( ) ;
@@ -166,6 +168,10 @@ export const mintFlow = async () => {
166168 } ) ;
167169
168170 it ( "should fail mint to invalidMint for failed send tx to vault" , async ( ) => {
171+ debug (
172+ "\nTesting -- should fail mint to invalidMint for failed send tx to vault"
173+ ) ;
174+
169175 const signer = await pocket . signerPromise ;
170176 const fromAddress = await pocket . getAddress ( ) ;
171177 const toAddress = pocket . VAULT_ADDRESS ;
@@ -204,6 +210,10 @@ export const mintFlow = async () => {
204210 } ) ;
205211
206212 it ( "should return amount for send tx to vault with invalid memo" , async ( ) => {
213+ debug (
214+ "\nTesting -- should return amount for send tx to vault with invalid memo"
215+ ) ;
216+
207217 const signer = await pocket . signerPromise ;
208218 const fromAddress = await pocket . getAddress ( ) ;
209219 const toAddress = pocket . VAULT_ADDRESS ;
@@ -332,4 +342,217 @@ export const mintFlow = async () => {
332342
333343 debug ( "InvalidMint success" ) ;
334344 } ) ;
345+
346+ it ( "should do multiple consecutive mints" , async ( ) => {
347+ debug ( "\nTesting -- should do multiple consecutive mints" ) ;
348+
349+ const signer = await pocket . signerPromise ;
350+ const fromAddress = await pocket . getAddress ( ) ;
351+ const recipientAddress = await ethereum . getAddress ( ) ;
352+ const toAddress = pocket . VAULT_ADDRESS ;
353+ const amounts = [
354+ parseUnits ( "1" , 6 ) ,
355+ parseUnits ( "2" , 6 ) ,
356+ parseUnits ( "3" , 6 ) ,
357+ ] ;
358+ const fee = BigInt ( 10000 ) ;
359+ const startNonce = 2 ;
360+
361+ const memo : MintMemo = {
362+ address : recipientAddress ,
363+ chain_id : ethereum . CHAIN . id . toString ( ) ,
364+ } ;
365+
366+ const fromBeforeBalance = await pocket . getBalance ( fromAddress ) ;
367+ const toBeforeBalance = await pocket . getBalance ( toAddress ) ;
368+
369+ debug ( "Sending transactions..." ) ;
370+ const sendTxs = await Promise . all (
371+ amounts . map ( async ( amount ) =>
372+ pocket . sendPOKT (
373+ signer ,
374+ toAddress ,
375+ amount . toString ( ) ,
376+ JSON . stringify ( memo ) ,
377+ fee . toString ( )
378+ )
379+ )
380+ ) ;
381+
382+ expect ( sendTxs ) . to . not . be . null ;
383+ expect ( sendTxs . length ) . to . equal ( amounts . length ) ;
384+
385+ if ( ! sendTxs ) return ;
386+
387+ sendTxs . forEach ( ( sendTx , i ) => {
388+ expect ( sendTx ) . to . not . be . null ;
389+
390+ if ( ! sendTx ) return ;
391+ debug ( `Transaction ${ i } sent: ` , sendTx . hash ) ;
392+ } ) ;
393+
394+ const fromAfterBalance = await pocket . getBalance ( fromAddress ) ;
395+ const toAfterBalance = await pocket . getBalance ( toAddress ) ;
396+
397+ const totalAmount = amounts . reduce (
398+ ( total , amount ) => total + amount ,
399+ BigInt ( 0 )
400+ ) ;
401+
402+ expect ( fromAfterBalance ) . to . equal (
403+ fromBeforeBalance - totalAmount - BigInt ( amounts . length ) * fee
404+ ) ;
405+ expect ( toAfterBalance ) . to . equal ( toBeforeBalance + totalAmount ) ;
406+
407+ debug ( `Waiting for mints to be created...` ) ;
408+ await sleep ( 2000 ) ;
409+
410+ await Promise . all (
411+ sendTxs . map ( async ( sendTx , i ) => {
412+ if ( ! sendTx ) return ;
413+
414+ const mint = await findMint ( sendTx . hash ) ;
415+
416+ expect ( mint ) . to . not . be . null ;
417+
418+ if ( ! mint ) return ;
419+ debug ( `Mint ${ i } created` ) ;
420+
421+ expect ( mint . height . toString ( ) ) . to . equal ( sendTx . height . toString ( ) ) ;
422+ expect ( mint . sender_address . toLowerCase ( ) ) . to . equal (
423+ fromAddress . toLowerCase ( )
424+ ) ;
425+ expect ( mint . recipient_address . toLowerCase ( ) ) . to . equal (
426+ recipientAddress . toLowerCase ( )
427+ ) ;
428+ const amount = amounts [ i ] ;
429+ expect ( mint . amount . toString ( ) ) . to . equal ( amount . toString ( ) ) ;
430+ expect ( mint . status ) . to . equal ( Status . PENDING ) ;
431+ } )
432+ ) ;
433+
434+ await sleep ( 12000 ) ;
435+
436+ await Promise . all (
437+ sendTxs . map ( async ( sendTx , i ) => {
438+ if ( ! sendTx ) return ;
439+
440+ const mint = await findMint ( sendTx . hash ) ;
441+
442+ expect ( mint ) . to . not . be . null ;
443+
444+ if ( ! mint ) return ;
445+
446+ expect ( mint . status ) . to . be . oneOf ( [ Status . CONFIRMED , Status . SIGNED ] ) ;
447+ debug ( `Mint ${ i } confirmed` ) ;
448+ } )
449+ ) ;
450+
451+ await sleep ( 3000 ) ;
452+
453+ const noncesToSee = sendTxs . map ( ( _ , i ) => ( i + startNonce ) . toString ( ) ) ;
454+
455+ const sortedMints : Array < Mint | null > = [ null , null , null ] ;
456+
457+ for ( let i = 0 ; i < sendTxs . length ; i ++ ) {
458+ const sendTx = sendTxs [ i ] ;
459+
460+ if ( ! sendTx ) return ;
461+
462+ const mint = await findMint ( sendTx . hash ) ;
463+
464+ expect ( mint ) . to . not . be . null ;
465+
466+ if ( ! mint ) return ;
467+
468+ expect ( mint . status ) . to . equal ( Status . SIGNED ) ;
469+ debug ( `Mint ${ i } signed` ) ;
470+
471+ expect ( mint . signers . length ) . to . equal ( 3 ) ;
472+ expect ( mint . signatures . length ) . to . equal ( 3 ) ;
473+ const nonce = mint . nonce . toString ( ) ;
474+ expect ( noncesToSee ) . to . include ( nonce ) ;
475+ noncesToSee . splice ( noncesToSee . indexOf ( nonce ) , 1 ) ;
476+
477+ const sortedIndex = parseInt ( nonce ) - startNonce ;
478+ sortedMints [ sortedIndex ] = mint ;
479+ }
480+
481+ const mintTxs : Array < TransactionReceipt > = [ ] ;
482+
483+ for ( let i = 0 ; i < sortedMints . length ; i ++ ) {
484+ const mint = sortedMints [ i ] ;
485+ expect ( mint ) . to . not . be . null ;
486+
487+ if ( ! mint ) return ;
488+
489+ expect ( mint . data ) . to . not . be . null ;
490+
491+ if ( ! mint . data ) return ;
492+
493+ const beforeWPOKTBalance = await ethereum . getWPOKTBalance (
494+ recipientAddress
495+ ) ;
496+
497+ debug ( `Minting ${ i } WPOKT...` ) ;
498+ const mintTx = await ethereum . mintWPOKT (
499+ await ethereum . walletPromise ,
500+ mint . data ,
501+ mint . signatures
502+ ) ;
503+
504+ expect ( mintTx ) . to . not . be . null ;
505+
506+ if ( ! mintTx ) return ;
507+ debug ( `WPOKT ${ i } minted: ` , mintTx . transactionHash ) ;
508+
509+ expect ( mintTx . transactionHash ) . to . be . a ( "string" ) ;
510+
511+ mintTxs . push ( mintTx ) ;
512+
513+ const mintedEvent = ethereum . findMintedEvent ( mintTx ) ;
514+
515+ expect ( mintedEvent ) . to . not . be . null ;
516+
517+ if ( ! mintedEvent ) return ;
518+
519+ expect ( mintedEvent . nonce . toString ( ) ) . to . equal ( mint . nonce . toString ( ) ) ;
520+ expect ( mintedEvent . recipient . toLowerCase ( ) ) . to . equal (
521+ recipientAddress . toLowerCase ( )
522+ ) ;
523+ expect ( mintedEvent . amount . toString ( ) ) . to . equal ( mint . amount . toString ( ) ) ;
524+
525+ const afterWPOKTBalance = await ethereum . getWPOKTBalance (
526+ recipientAddress
527+ ) ;
528+
529+ expect ( afterWPOKTBalance ) . to . equal (
530+ beforeWPOKTBalance + BigInt ( mint . amount )
531+ ) ;
532+ }
533+
534+ await sleep ( 2000 ) ;
535+
536+ await Promise . all (
537+ sortedMints . map ( async ( oldMint , i ) => {
538+ expect ( oldMint ) . to . not . be . null ;
539+ if ( ! oldMint ) return ;
540+
541+ const mint = await findMint ( oldMint . transaction_hash ) ;
542+
543+ expect ( mint ) . to . not . be . null ;
544+
545+ if ( ! mint ) return ;
546+
547+ expect ( mint . status ) . to . equal ( Status . SUCCESS ) ;
548+
549+ const mintTx = mintTxs [ i ] ;
550+
551+ expect ( mint . mint_tx_hash . toLowerCase ( ) ) . to . equal (
552+ mintTx . transactionHash . toLowerCase ( )
553+ ) ;
554+ debug ( `Mint ${ i } success` ) ;
555+ } )
556+ ) ;
557+ } ) ;
335558} ;
0 commit comments