Skip to content

Commit

Permalink
Write tests for new methods in PufferVaultHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
9inpachi committed May 28, 2024
1 parent 14c140c commit c045375
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 32 deletions.
84 changes: 73 additions & 11 deletions lib/contracts/handlers/puffer-vault-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ 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(
Chain.Anvil,
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,
Expand All @@ -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);
});
});
42 changes: 21 additions & 21 deletions lib/contracts/handlers/puffer-vault-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -108,7 +88,7 @@ export class PufferVaultHandler {
* `estimate: () => Promise<bigint>` - 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,
Expand All @@ -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.
*
Expand Down

0 comments on commit c045375

Please sign in to comment.