@@ -228,7 +228,7 @@ describe("MC Payment Contract", () => {
228228 // grant WITHDRAWER_ROLE to userSigner
229229 await payment
230230 . connect ( owner )
231- . grantRole ( await payment . WITHDRAWER_ROLE ( ) , userSigner . getAddress ( ) ) ;
231+ . grantRole ( await payment . WITHDRAWER_ROLE ( ) , await userSigner . getAddress ( ) ) ;
232232
233233 // now userSigner can withdraw owner balance
234234 await expect ( payment . connect ( userSigner ) . ownerWithdraw ( ) ) . to . changeEtherBalance ( userSigner , 10 ) ;
@@ -241,6 +241,24 @@ describe("MC Payment Contract", () => {
241241 payment ,
242242 "WithdrawErrorNoBalance" ,
243243 ) ;
244+ await expect ( payment . connect ( owner ) . ownerWithdraw ( ) ) . to . be . revertedWithCustomError (
245+ payment ,
246+ "WithdrawErrorNoBalance" ,
247+ ) ;
248+ } ) ;
249+
250+ it ( "Calling setAdminRole by owner:" , async ( ) => {
251+ expect ( await payment . hasRole ( await payment . DEFAULT_ADMIN_ROLE ( ) , owner . address ) ) . to . be . false ;
252+ await payment . connect ( owner ) . setAdminRole ( owner . address ) ;
253+ expect ( await payment . hasRole ( await payment . DEFAULT_ADMIN_ROLE ( ) , owner . address ) ) . to . be . true ;
254+ await payment . connect ( owner ) . revokeRole ( await payment . DEFAULT_ADMIN_ROLE ( ) , owner . address ) ;
255+ expect ( await payment . hasRole ( await payment . DEFAULT_ADMIN_ROLE ( ) , owner . address ) ) . to . be . false ;
256+ } ) ;
257+
258+ it ( "Calling setAdminRole by non-owner:" , async ( ) => {
259+ await expect (
260+ payment . connect ( userSigner ) . setAdminRole ( owner . address ) ,
261+ ) . to . be . revertedWithCustomError ( payment , "OwnableUnauthorizedAccount" ) ;
244262 } ) ;
245263
246264 it ( "Update owner percentage:" , async ( ) => {
@@ -391,6 +409,62 @@ describe("MC Payment Contract", () => {
391409 expect ( await payment . getOwnerERC20Balance ( tokenAddress ) ) . to . be . eq ( 0 ) ;
392410 } ) ;
393411
412+ it ( "ERC-20 payment with withdrawer role:" , async ( ) => {
413+ const tokenFactory = await ethers . getContractFactory ( "ERC20Token" , owner ) ;
414+ const token = await tokenFactory . deploy ( 1_000 ) ;
415+ await token . connect ( owner ) . transfer ( await userSigner . getAddress ( ) , 100 ) ;
416+ expect ( await token . balanceOf ( await userSigner . getAddress ( ) ) ) . to . be . eq ( 100 ) ;
417+
418+ await token . connect ( userSigner ) . approve ( await payment . getAddress ( ) , 10 ) ;
419+
420+ const paymentData = {
421+ tokenAddress : await token . getAddress ( ) ,
422+ recipient : issuer1Signer . address ,
423+ amount : 10 ,
424+ expirationDate : Math . round ( new Date ( ) . getTime ( ) / 1000 ) + 60 * 60 , // 1 hour
425+ nonce : 35 ,
426+ metadata : "0x" ,
427+ } ;
428+
429+ const signature = await issuer1Signer . signTypedData ( domainData , erc20types , paymentData ) ;
430+ const erc20PaymentGas = await payment
431+ . connect ( userSigner )
432+ . payERC20 . estimateGas ( paymentData , signature ) ;
433+ console . log ( "ERC-20 Payment Gas: " + erc20PaymentGas ) ;
434+
435+ await expect (
436+ payment . connect ( userSigner ) . payERC20 ( paymentData , signature ) ,
437+ ) . to . changeTokenBalances ( token , [ userSigner , issuer1Signer , payment ] , [ - 10 , 9 , 1 ] ) ;
438+ expect ( await payment . isPaymentDone ( issuer1Signer . address , 35 ) ) . to . be . true ;
439+ // owner ERC-20 withdraw
440+ const tokenAddress = await token . getAddress ( ) ;
441+ const ownerBalance = await payment . getOwnerERC20Balance ( tokenAddress ) ;
442+ expect ( ownerBalance ) . to . be . eq ( 1 ) ;
443+
444+ await expect (
445+ payment . connect ( userSigner ) . ownerERC20Withdraw ( tokenAddress ) ,
446+ ) . to . be . revertedWithCustomError ( payment , "AccessControlUnauthorizedAccount" ) ;
447+
448+ // grant admin role to owner
449+ await payment . connect ( owner ) . setAdminRole ( owner . address ) ;
450+ // grant WITHDRAWER_ROLE to userSigner
451+ await payment . grantRole ( await payment . WITHDRAWER_ROLE ( ) , await userSigner . getAddress ( ) ) ;
452+
453+ await expect (
454+ payment . connect ( userSigner ) . ownerERC20Withdraw ( tokenAddress ) ,
455+ ) . to . changeTokenBalances ( token , [ userSigner , payment ] , [ 1 , - 1 ] ) ;
456+
457+ // second owner or withdrawer withdraw
458+ await expect (
459+ payment . connect ( userSigner ) . ownerERC20Withdraw ( tokenAddress ) ,
460+ ) . to . be . revertedWithCustomError ( payment , "WithdrawErrorNoBalance" ) ;
461+
462+ await expect (
463+ payment . connect ( owner ) . ownerERC20Withdraw ( tokenAddress ) ,
464+ ) . to . be . revertedWithCustomError ( payment , "WithdrawErrorNoBalance" ) ;
465+ expect ( await payment . getOwnerERC20Balance ( tokenAddress ) ) . to . be . eq ( 0 ) ;
466+ } ) ;
467+
394468 it ( "ERC-20 payment with different issuer owner percentage:" , async ( ) => {
395469 const tokenFactory = await ethers . getContractFactory ( "ERC20Token" , owner ) ;
396470 const token = await tokenFactory . deploy ( 1_000 ) ;
@@ -432,6 +506,47 @@ describe("MC Payment Contract", () => {
432506 expect ( await payment . getOwnerERC20Balance ( tokenAddress ) ) . to . be . eq ( 0 ) ;
433507 } ) ;
434508
509+ it ( "ERC-20 payment with different issuer owner percentage 100%:" , async ( ) => {
510+ const tokenFactory = await ethers . getContractFactory ( "ERC20Token" , owner ) ;
511+ const token = await tokenFactory . deploy ( 1_000 ) ;
512+ await token . connect ( owner ) . transfer ( await userSigner . getAddress ( ) , 100 ) ;
513+ expect ( await token . balanceOf ( await userSigner . getAddress ( ) ) ) . to . be . eq ( 100 ) ;
514+
515+ await token . connect ( userSigner ) . approve ( await payment . getAddress ( ) , 10 ) ;
516+
517+ await payment . connect ( owner ) . updateIssuerOwnerPercentage ( issuer1Signer . address , 100 ) ;
518+
519+ const paymentData = {
520+ tokenAddress : await token . getAddress ( ) ,
521+ recipient : issuer1Signer . address ,
522+ amount : 10 ,
523+ expirationDate : Math . round ( new Date ( ) . getTime ( ) / 1000 ) + 60 * 60 , // 1 hour
524+ nonce : 35 ,
525+ metadata : "0x" ,
526+ } ;
527+
528+ const signature = await issuer1Signer . signTypedData ( domainData , erc20types , paymentData ) ;
529+ const erc20PaymentGas = await payment
530+ . connect ( userSigner )
531+ . payERC20 . estimateGas ( paymentData , signature ) ;
532+ console . log ( "ERC-20 Payment Gas: " + erc20PaymentGas ) ;
533+
534+ await expect (
535+ payment . connect ( userSigner ) . payERC20 ( paymentData , signature ) ,
536+ ) . to . changeTokenBalances ( token , [ userSigner , issuer1Signer , payment ] , [ - 10 , 0 , 10 ] ) ;
537+ expect ( await payment . isPaymentDone ( issuer1Signer . address , 35 ) ) . to . be . true ;
538+ // owner ERC-20 withdraw
539+ const tokenAddress = await token . getAddress ( ) ;
540+ const ownerBalance = await payment . getOwnerERC20Balance ( tokenAddress ) ;
541+ expect ( ownerBalance ) . to . be . eq ( 10 ) ;
542+ await expect ( payment . connect ( owner ) . ownerERC20Withdraw ( tokenAddress ) ) . to . changeTokenBalances (
543+ token ,
544+ [ owner , payment ] ,
545+ [ 10 , - 10 ] ,
546+ ) ;
547+ expect ( await payment . getOwnerERC20Balance ( tokenAddress ) ) . to . be . eq ( 0 ) ;
548+ } ) ;
549+
435550 it ( "ERC-20 payment - expired:" , async ( ) => {
436551 const tokenFactory = await ethers . getContractFactory ( "ERC20Token" , owner ) ;
437552 const token = await tokenFactory . deploy ( 1_000 ) ;
0 commit comments