diff --git a/lib/contracts/handlers/puffer-vault-handler.test.ts b/lib/contracts/handlers/puffer-vault-handler.test.ts index cdb5f8a..4fc5248 100644 --- a/lib/contracts/handlers/puffer-vault-handler.test.ts +++ b/lib/contracts/handlers/puffer-vault-handler.test.ts @@ -8,13 +8,15 @@ import { Chain } from '../../chains/constants'; import { PufferVaultHandler } from './puffer-vault-handler'; describe('PufferVaultHandler', () => { - it('should check pufETH balance', async () => { - const mockAddress = '0x8d37d81e29d11cd7557ceaca25e4e4a4255b1159'; - const mockBalance = BigInt(1); - const mockCallHex = toHex(mockBalance, { size: 32 }); + it('should deposit ETH', async () => { + const mockAddress = '0xEB77D02f8122B32273444a1b544C147Fb559CB41'; + const mockGas = BigInt(1); - const walletClient = setupMockWalletClient(); - const publicRequest = mockRpcRequest({ eth_call: mockCallHex }); + const walletRequest = mockRpcRequest({ + eth_sendTransaction: mockAddress, + }); + const walletClient = setupMockWalletClient(walletRequest); + const publicRequest = mockRpcRequest({ eth_estimateGas: mockGas }); const publicClient = setupMockPublicClient(publicRequest); const handler = new PufferVaultHandler( @@ -22,14 +24,16 @@ describe('PufferVaultHandler', () => { walletClient, publicClient, ); - const balance = await handler.balanceOf(mockAddress); + const { transact, estimate } = handler.depositETH(mockAddress); - expect(balance).toBe(mockBalance); + expect(await transact(BigInt(1))).toBe(mockAddress); + expect(await estimate()).toBe(mockGas); }); - it('should be able to deposit ETH', async () => { + it('should deposit stETH', async () => { const mockAddress = '0xEB77D02f8122B32273444a1b544C147Fb559CB41'; const mockGas = BigInt(1); + const mockValue = BigInt(1); const walletRequest = mockRpcRequest({ eth_sendTransaction: mockAddress, @@ -43,9 +47,67 @@ describe('PufferVaultHandler', () => { walletClient, publicClient, ); - const { transact, estimate } = handler.depositETH(mockAddress); + const { transact, estimate } = handler.depositStETH(mockAddress, mockValue); - expect(await transact(BigInt(1))).toBe(mockAddress); + expect(await transact()).toBe(mockAddress); expect(await estimate()).toBe(mockGas); }); + + it('should check pufETH balance', async () => { + const mockAddress = '0xEB77D02f8122B32273444a1b544C147Fb559CB41'; + const mockBalance = BigInt(1); + const mockCallHex = toHex(mockBalance, { size: 32 }); + + const walletClient = setupMockWalletClient(); + const publicRequest = mockRpcRequest({ eth_call: mockCallHex }); + const publicClient = setupMockPublicClient(publicRequest); + + const handler = new PufferVaultHandler( + Chain.Anvil, + walletClient, + publicClient, + ); + const balance = await handler.balanceOf(mockAddress); + + expect(balance).toBe(mockBalance); + }); + + it('should check pufETH rate', async () => { + const mockRate = BigInt(1e18); + const mockCallHex = toHex(mockRate, { size: 32 }); + + const walletClient = setupMockWalletClient(); + const publicRequest = mockRpcRequest({ eth_call: mockCallHex }); + const publicClient = setupMockPublicClient(publicRequest); + + const handler = new PufferVaultHandler( + Chain.Anvil, + walletClient, + publicClient, + ); + const rate = await handler.getPufETHRate(); + + expect(rate).toBe(mockRate); + }); + + it('should get allowance', async () => { + const mockAllowance = BigInt(1); + const mockCallHex = toHex(mockAllowance, { size: 32 }); + + const walletClient = setupMockWalletClient(); + const publicRequest = mockRpcRequest({ eth_call: mockCallHex }); + const publicClient = setupMockPublicClient(publicRequest); + + const handler = new PufferVaultHandler( + Chain.Anvil, + walletClient, + publicClient, + ); + const rate = await handler.getAllowance( + '0xEB77D02f8122B32273444a1b544C147Fb559CB41', + '0x92e01fbccae21eed10ab2f278f47905d482df202', + ); + + expect(rate).toBe(mockAllowance); + }); }); diff --git a/lib/contracts/handlers/puffer-vault-handler.ts b/lib/contracts/handlers/puffer-vault-handler.ts index bbfff94..5ec17b1 100644 --- a/lib/contracts/handlers/puffer-vault-handler.ts +++ b/lib/contracts/handlers/puffer-vault-handler.ts @@ -75,26 +75,6 @@ export class PufferVaultHandler { return { transact, estimate }; } - /** - * Check the pufETH balance of the wallet. - * - * @param walletAddress Wallet address to check the balance of. - * @returns pufETH balance in wei. - */ - public async balanceOf(walletAddress: Address) { - return await this.getContract().read.balanceOf([walletAddress]); - } - - /** - * Get the rate of pufETH compared to ETH. - * - * @returns Rate of 1 pufETH compared to 1 ETH. - */ - public async pufETHRate() { - const oneWei = BigInt(1e18); - return await this.getContract().read.previewDeposit([oneWei]); - } - /** * Deposit stETH to the given wallet address. This doesn't make the * transaction but returns two methods namely `transact` and @@ -108,7 +88,7 @@ export class PufferVaultHandler { * `estimate: () => Promise` - Gas estimate of the * transaction. */ - public async depositStETH(walletAddress: Address, value: bigint) { + public depositStETH(walletAddress: Address, value: bigint) { const transact = async () => await this.getContract().write.depositStETH([value, walletAddress], { account: walletAddress, @@ -126,6 +106,26 @@ export class PufferVaultHandler { return { transact, estimate }; } + /** + * Check the pufETH balance of the wallet. + * + * @param walletAddress Wallet address to check the balance of. + * @returns pufETH balance in wei. + */ + public async balanceOf(walletAddress: Address) { + return await this.getContract().read.balanceOf([walletAddress]); + } + + /** + * Get the rate of pufETH compared to ETH. + * + * @returns Rate of pufETH compared to 1 ETH. + */ + public async getPufETHRate() { + const oneWei = BigInt(1e18); + return await this.getContract().read.previewDeposit([oneWei]); + } + /** * Get the allowance for the given owner and spender. *