|
| 1 | +import { createPublicClient, http, type Address, type Hex } from 'viem'; |
| 2 | +import { sepolia } from 'viem/chains'; |
| 3 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 4 | + |
| 5 | +import * as ERC20TransferAmountEnforcer from '../../../src/DelegationFramework/ERC20TransferAmountEnforcer'; |
| 6 | +import { randomAddress, randomBytes } from '../../utils'; |
| 7 | + |
| 8 | +// Helper function to generate random bytes32 |
| 9 | +const randomBytes32 = (): Hex => randomBytes(32); |
| 10 | + |
| 11 | +// Mock the readContract function |
| 12 | +vi.mock('viem/actions', () => ({ |
| 13 | + readContract: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +describe('ERC20TransferAmountEnforcer read functions', () => { |
| 17 | + let client: any; |
| 18 | + let contractAddress: Address; |
| 19 | + let delegationManager: Address; |
| 20 | + let delegationHash: Hex; |
| 21 | + let terms: Hex; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + client = createPublicClient({ |
| 25 | + chain: sepolia, |
| 26 | + transport: http(), |
| 27 | + }); |
| 28 | + contractAddress = randomAddress(); |
| 29 | + delegationManager = randomAddress(); |
| 30 | + delegationHash = randomBytes32(); |
| 31 | + terms = |
| 32 | + '0x1234567890123456789012345678901234567890000000000000000000000000000000000000000000000000000000000001'; |
| 33 | + |
| 34 | + vi.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('getTermsInfo', () => { |
| 38 | + it('should call readContract with correct parameters and return terms info', async () => { |
| 39 | + const mockAllowedContract = randomAddress(); |
| 40 | + const mockMaxTokens = 1000000n; |
| 41 | + |
| 42 | + const { readContract } = await import('viem/actions'); |
| 43 | + vi.mocked(readContract).mockResolvedValue([ |
| 44 | + mockAllowedContract, |
| 45 | + mockMaxTokens, |
| 46 | + ]); |
| 47 | + |
| 48 | + const result = await ERC20TransferAmountEnforcer.read.getTermsInfo({ |
| 49 | + client, |
| 50 | + contractAddress, |
| 51 | + terms, |
| 52 | + }); |
| 53 | + |
| 54 | + expect(readContract).toHaveBeenCalledWith(client, { |
| 55 | + address: contractAddress, |
| 56 | + abi: expect.any(Array), |
| 57 | + functionName: 'getTermsInfo', |
| 58 | + args: [terms], |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result).toEqual({ |
| 62 | + allowedContract: mockAllowedContract, |
| 63 | + maxTokens: mockMaxTokens, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle different terms values', async () => { |
| 68 | + const mockAllowedContract = randomAddress(); |
| 69 | + const mockMaxTokens = 500000n; |
| 70 | + const differentTerms = |
| 71 | + '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd000000000000000000000000000000000000000000000000000000000000002'; |
| 72 | + |
| 73 | + const { readContract } = await import('viem/actions'); |
| 74 | + vi.mocked(readContract).mockResolvedValue([ |
| 75 | + mockAllowedContract, |
| 76 | + mockMaxTokens, |
| 77 | + ]); |
| 78 | + |
| 79 | + const result = await ERC20TransferAmountEnforcer.read.getTermsInfo({ |
| 80 | + client, |
| 81 | + contractAddress, |
| 82 | + terms: differentTerms, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(readContract).toHaveBeenCalledWith(client, { |
| 86 | + address: contractAddress, |
| 87 | + abi: expect.any(Array), |
| 88 | + functionName: 'getTermsInfo', |
| 89 | + args: [differentTerms], |
| 90 | + }); |
| 91 | + |
| 92 | + expect(result).toEqual({ |
| 93 | + allowedContract: mockAllowedContract, |
| 94 | + maxTokens: mockMaxTokens, |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('getSpentAmount', () => { |
| 100 | + it('should call readContract with correct parameters and return spent amount', async () => { |
| 101 | + const mockSpentAmount = 250000n; |
| 102 | + |
| 103 | + const { readContract } = await import('viem/actions'); |
| 104 | + vi.mocked(readContract).mockResolvedValue(mockSpentAmount); |
| 105 | + |
| 106 | + const result = await ERC20TransferAmountEnforcer.read.getSpentAmount({ |
| 107 | + client, |
| 108 | + contractAddress, |
| 109 | + delegationManager, |
| 110 | + delegationHash, |
| 111 | + }); |
| 112 | + |
| 113 | + expect(readContract).toHaveBeenCalledWith(client, { |
| 114 | + address: contractAddress, |
| 115 | + abi: expect.any(Array), |
| 116 | + functionName: 'spentMap', |
| 117 | + args: [delegationManager, delegationHash], |
| 118 | + }); |
| 119 | + |
| 120 | + expect(result).toBe(mockSpentAmount); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle zero spent amount', async () => { |
| 124 | + const mockSpentAmount = 0n; |
| 125 | + |
| 126 | + const { readContract } = await import('viem/actions'); |
| 127 | + vi.mocked(readContract).mockResolvedValue(mockSpentAmount); |
| 128 | + |
| 129 | + const result = await ERC20TransferAmountEnforcer.read.getSpentAmount({ |
| 130 | + client, |
| 131 | + contractAddress, |
| 132 | + delegationManager, |
| 133 | + delegationHash, |
| 134 | + }); |
| 135 | + |
| 136 | + expect(readContract).toHaveBeenCalledWith(client, { |
| 137 | + address: contractAddress, |
| 138 | + abi: expect.any(Array), |
| 139 | + functionName: 'spentMap', |
| 140 | + args: [delegationManager, delegationHash], |
| 141 | + }); |
| 142 | + |
| 143 | + expect(result).toBe(mockSpentAmount); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should handle different delegation parameters', async () => { |
| 147 | + const differentDelegationManager = randomAddress(); |
| 148 | + const differentDelegationHash = randomBytes32(); |
| 149 | + const mockSpentAmount = 750000n; |
| 150 | + |
| 151 | + const { readContract } = await import('viem/actions'); |
| 152 | + vi.mocked(readContract).mockResolvedValue(mockSpentAmount); |
| 153 | + |
| 154 | + const result = await ERC20TransferAmountEnforcer.read.getSpentAmount({ |
| 155 | + client, |
| 156 | + contractAddress, |
| 157 | + delegationManager: differentDelegationManager, |
| 158 | + delegationHash: differentDelegationHash, |
| 159 | + }); |
| 160 | + |
| 161 | + expect(readContract).toHaveBeenCalledWith(client, { |
| 162 | + address: contractAddress, |
| 163 | + abi: expect.any(Array), |
| 164 | + functionName: 'spentMap', |
| 165 | + args: [differentDelegationManager, differentDelegationHash], |
| 166 | + }); |
| 167 | + |
| 168 | + expect(result).toBe(mockSpentAmount); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('API structure', () => { |
| 173 | + it('should export the expected functions', () => { |
| 174 | + expect(ERC20TransferAmountEnforcer.read.getTermsInfo).toBeDefined(); |
| 175 | + expect(ERC20TransferAmountEnforcer.read.getSpentAmount).toBeDefined(); |
| 176 | + expect(typeof ERC20TransferAmountEnforcer.read.getTermsInfo).toBe( |
| 177 | + 'function', |
| 178 | + ); |
| 179 | + expect(typeof ERC20TransferAmountEnforcer.read.getSpentAmount).toBe( |
| 180 | + 'function', |
| 181 | + ); |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments