Skip to content

Commit c7a571a

Browse files
committed
Write tests for new methods in PufferVaultHandler
1 parent 1c4e541 commit c7a571a

File tree

2 files changed

+94
-32
lines changed

2 files changed

+94
-32
lines changed

lib/contracts/handlers/puffer-vault-handler.test.ts

+73-11
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,32 @@ import { Chain } from '../../chains/constants';
88
import { PufferVaultHandler } from './puffer-vault-handler';
99

1010
describe('PufferVaultHandler', () => {
11-
it('should check pufETH balance', async () => {
12-
const mockAddress = '0x8d37d81e29d11cd7557ceaca25e4e4a4255b1159';
13-
const mockBalance = BigInt(1);
14-
const mockCallHex = toHex(mockBalance, { size: 32 });
11+
it('should deposit ETH', async () => {
12+
const mockAddress = '0xEB77D02f8122B32273444a1b544C147Fb559CB41';
13+
const mockGas = BigInt(1);
1514

16-
const walletClient = setupMockWalletClient();
17-
const publicRequest = mockRpcRequest({ eth_call: mockCallHex });
15+
const walletRequest = mockRpcRequest({
16+
eth_sendTransaction: mockAddress,
17+
});
18+
const walletClient = setupMockWalletClient(walletRequest);
19+
const publicRequest = mockRpcRequest({ eth_estimateGas: mockGas });
1820
const publicClient = setupMockPublicClient(publicRequest);
1921

2022
const handler = new PufferVaultHandler(
2123
Chain.Anvil,
2224
walletClient,
2325
publicClient,
2426
);
25-
const balance = await handler.balanceOf(mockAddress);
27+
const { transact, estimate } = handler.depositETH(mockAddress);
2628

27-
expect(balance).toBe(mockBalance);
29+
expect(await transact(BigInt(1))).toBe(mockAddress);
30+
expect(await estimate()).toBe(mockGas);
2831
});
2932

30-
it('should be able to deposit ETH', async () => {
33+
it('should deposit stETH', async () => {
3134
const mockAddress = '0xEB77D02f8122B32273444a1b544C147Fb559CB41';
3235
const mockGas = BigInt(1);
36+
const mockValue = BigInt(1);
3337

3438
const walletRequest = mockRpcRequest({
3539
eth_sendTransaction: mockAddress,
@@ -43,9 +47,67 @@ describe('PufferVaultHandler', () => {
4347
walletClient,
4448
publicClient,
4549
);
46-
const { transact, estimate } = handler.depositETH(mockAddress);
50+
const { transact, estimate } = handler.depositStETH(mockAddress, mockValue);
4751

48-
expect(await transact(BigInt(1))).toBe(mockAddress);
52+
expect(await transact()).toBe(mockAddress);
4953
expect(await estimate()).toBe(mockGas);
5054
});
55+
56+
it('should check pufETH balance', async () => {
57+
const mockAddress = '0xEB77D02f8122B32273444a1b544C147Fb559CB41';
58+
const mockBalance = BigInt(1);
59+
const mockCallHex = toHex(mockBalance, { size: 32 });
60+
61+
const walletClient = setupMockWalletClient();
62+
const publicRequest = mockRpcRequest({ eth_call: mockCallHex });
63+
const publicClient = setupMockPublicClient(publicRequest);
64+
65+
const handler = new PufferVaultHandler(
66+
Chain.Anvil,
67+
walletClient,
68+
publicClient,
69+
);
70+
const balance = await handler.balanceOf(mockAddress);
71+
72+
expect(balance).toBe(mockBalance);
73+
});
74+
75+
it('should check pufETH rate', async () => {
76+
const mockRate = BigInt(1e18);
77+
const mockCallHex = toHex(mockRate, { size: 32 });
78+
79+
const walletClient = setupMockWalletClient();
80+
const publicRequest = mockRpcRequest({ eth_call: mockCallHex });
81+
const publicClient = setupMockPublicClient(publicRequest);
82+
83+
const handler = new PufferVaultHandler(
84+
Chain.Anvil,
85+
walletClient,
86+
publicClient,
87+
);
88+
const rate = await handler.getPufETHRate();
89+
90+
expect(rate).toBe(mockRate);
91+
});
92+
93+
it('should get allowance', async () => {
94+
const mockAllowance = BigInt(1);
95+
const mockCallHex = toHex(mockAllowance, { size: 32 });
96+
97+
const walletClient = setupMockWalletClient();
98+
const publicRequest = mockRpcRequest({ eth_call: mockCallHex });
99+
const publicClient = setupMockPublicClient(publicRequest);
100+
101+
const handler = new PufferVaultHandler(
102+
Chain.Anvil,
103+
walletClient,
104+
publicClient,
105+
);
106+
const rate = await handler.getAllowance(
107+
'0xEB77D02f8122B32273444a1b544C147Fb559CB41',
108+
'0x92e01fbccae21eed10ab2f278f47905d482df202',
109+
);
110+
111+
expect(rate).toBe(mockAllowance);
112+
});
51113
});

lib/contracts/handlers/puffer-vault-handler.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,6 @@ export class PufferVaultHandler {
7575
return { transact, estimate };
7676
}
7777

78-
/**
79-
* Check the pufETH balance of the wallet.
80-
*
81-
* @param walletAddress Wallet address to check the balance of.
82-
* @returns pufETH balance in wei.
83-
*/
84-
public async balanceOf(walletAddress: Address) {
85-
return await this.getContract().read.balanceOf([walletAddress]);
86-
}
87-
88-
/**
89-
* Get the rate of pufETH compared to ETH.
90-
*
91-
* @returns Rate of 1 pufETH compared to 1 ETH.
92-
*/
93-
public async pufETHRate() {
94-
const oneWei = BigInt(1e18);
95-
return await this.getContract().read.previewDeposit([oneWei]);
96-
}
97-
9878
/**
9979
* Deposit stETH to the given wallet address. This doesn't make the
10080
* transaction but returns two methods namely `transact` and
@@ -108,7 +88,7 @@ export class PufferVaultHandler {
10888
* `estimate: () => Promise<bigint>` - Gas estimate of the
10989
* transaction.
11090
*/
111-
public async depositStETH(walletAddress: Address, value: bigint) {
91+
public depositStETH(walletAddress: Address, value: bigint) {
11292
const transact = async () =>
11393
await this.getContract().write.depositStETH([value, walletAddress], {
11494
account: walletAddress,
@@ -126,6 +106,26 @@ export class PufferVaultHandler {
126106
return { transact, estimate };
127107
}
128108

109+
/**
110+
* Check the pufETH balance of the wallet.
111+
*
112+
* @param walletAddress Wallet address to check the balance of.
113+
* @returns pufETH balance in wei.
114+
*/
115+
public async balanceOf(walletAddress: Address) {
116+
return await this.getContract().read.balanceOf([walletAddress]);
117+
}
118+
119+
/**
120+
* Get the rate of pufETH compared to ETH.
121+
*
122+
* @returns Rate of pufETH compared to 1 ETH.
123+
*/
124+
public async getPufETHRate() {
125+
const oneWei = BigInt(1e18);
126+
return await this.getContract().read.previewDeposit([oneWei]);
127+
}
128+
129129
/**
130130
* Get the allowance for the given owner and spender.
131131
*

0 commit comments

Comments
 (0)