Skip to content

Commit 800450d

Browse files
committed
Add a separate handler for PufferVaultV2 to separate concerns and make code modular
1 parent d3938be commit 800450d

File tree

4 files changed

+162
-178
lines changed

4 files changed

+162
-178
lines changed

lib/api/puffer-client.ts

+19-111
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
11
import {
2-
Address,
32
PublicClient,
43
WalletClient,
54
createPublicClient,
65
createWalletClient,
7-
getContract,
86
http,
97
} from 'viem';
10-
import { Chain, VIEM_CHAINS, ViemChain } from '../chains/constants';
11-
import { CHAIN_ADDRESSES } from '../contracts/addresses';
12-
import { ValueOf } from '../utils/types';
13-
import { CHAIN_ABIS } from '../contracts/abis/abis';
8+
import { Chain, VIEM_CHAINS } from '../chains/constants';
9+
import { PufferVaultHandler } from '../contracts/puffer-vault-handler';
1410

1511
/**
1612
* The core class and the main entry point of the Puffer SDK.
1713
*/
1814
export class PufferClient {
19-
private chainAddresses: ValueOf<typeof CHAIN_ADDRESSES>;
20-
private chainAbis: ValueOf<typeof CHAIN_ABIS>;
21-
22-
private viemChain: ViemChain;
2315
private walletClient: WalletClient;
2416
private publicClient: PublicClient;
2517

18+
// Contract Handlers
19+
public vault: PufferVaultHandler;
20+
2621
/**
2722
* Create the Puffer Client.
23+
*
2824
* @param chain Chain to use for the client.
29-
* @param walletClient The wallet client to use for wallet interactions.
30-
* @param publicClient The public client to use for public interactions.
25+
* @param walletClient The wallet client to use for wallet
26+
* interactions.
27+
* @param publicClient The public client to use for public
28+
* interactions.
3129
*/
3230
constructor(
3331
chain: Chain,
3432
walletClient?: WalletClient,
3533
publicClient?: PublicClient,
3634
) {
37-
this.chainAddresses = CHAIN_ADDRESSES[chain];
38-
this.chainAbis = CHAIN_ABIS[chain];
39-
this.viemChain = VIEM_CHAINS[chain];
35+
const viemChain = VIEM_CHAINS[chain];
4036

4137
this.walletClient =
4238
walletClient ??
4339
createWalletClient({
44-
chain: this.viemChain,
40+
chain: viemChain,
4541
transport: http(),
4642
});
4743
this.publicClient =
4844
publicClient ??
4945
createPublicClient({
50-
chain: this.viemChain,
46+
chain: viemChain,
5147
transport: http(),
5248
});
49+
50+
this.vault = new PufferVaultHandler(
51+
chain,
52+
this.walletClient,
53+
this.publicClient,
54+
);
5355
}
5456

5557
/**
@@ -60,98 +62,4 @@ export class PufferClient {
6062
public async requestAddresses() {
6163
return await this.walletClient.requestAddresses();
6264
}
63-
64-
/**
65-
* Deposit ETH to the given wallet address. This doesn't make the
66-
* transaction but returns two methods namely `transact` and
67-
* `estimate`.
68-
*
69-
* @param walletAddress Wallet address to get the ETH from.
70-
* @returns `transact: (value: bigint) => Promise<Address>` - Used to
71-
* make the transaction with the given value.
72-
*
73-
* `estimate: () => Promise<bigint>` - Gas estimate of the
74-
* transaction.
75-
*/
76-
public depositETH(walletAddress: Address) {
77-
const contract = this.getPufferVaultContract();
78-
79-
const transact = async (value: bigint) =>
80-
await contract.write.depositETH([walletAddress], {
81-
account: walletAddress,
82-
chain: this.viemChain,
83-
value,
84-
});
85-
86-
const estimate = async () =>
87-
await contract.estimateGas.depositETH([walletAddress], {
88-
account: walletAddress,
89-
});
90-
91-
return { transact, estimate };
92-
}
93-
94-
/**
95-
* Check the pufETH balance of the wallet.
96-
*
97-
* @param walletAddress Wallet address to check the balance of.
98-
* @returns pufETH balance in wei.
99-
*/
100-
public async balanceOf(walletAddress: Address) {
101-
const contract = this.getPufferVaultContract();
102-
return await contract.read.balanceOf([walletAddress]);
103-
}
104-
105-
/**
106-
* Get the rate of pufETH compared to ETH.
107-
*
108-
* @returns Rate of 1 pufETH compared to 1 ETH.
109-
*/
110-
public async pufETHRate() {
111-
const oneWei = BigInt(1e18);
112-
const contract = this.getPufferVaultContract();
113-
114-
return await contract.read.previewDeposit([oneWei]);
115-
}
116-
117-
/**
118-
* Deposit stETH to the given wallet address. This doesn't make the
119-
* transaction but returns two methods namely `transact` and
120-
* `estimate`.
121-
*
122-
* @param walletAddress Wallet address to get the ETH from.
123-
* @param value Value in wei of the stETH to deposit.
124-
* @returns `transact: () => Promise<Address>` - Used to make the
125-
* transaction with the given value.
126-
*
127-
* `estimate: () => Promise<bigint>` - Gas estimate of the
128-
* transaction.
129-
*/
130-
public async depositStETH(walletAddress: Address, value: bigint) {
131-
const contract = this.getPufferVaultContract();
132-
133-
const transact = async () =>
134-
await contract.write.depositStETH([value, walletAddress], {
135-
account: walletAddress,
136-
chain: this.viemChain,
137-
});
138-
139-
const estimate = async () =>
140-
await contract.estimateGas.depositStETH([value, walletAddress], {
141-
account: walletAddress,
142-
});
143-
144-
return { transact, estimate };
145-
}
146-
147-
private getPufferVaultContract() {
148-
return getContract({
149-
address: this.chainAddresses.PufferVault,
150-
abi: this.chainAbis.PufferVaultV2,
151-
client: {
152-
wallet: this.walletClient,
153-
public: this.publicClient,
154-
},
155-
});
156-
}
15765
}

lib/contracts/addresses.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { Address } from 'viem';
21
import { Chain } from '../chains/constants';
32

43
// Source of truth:
54
// https://github.com/PufferFinance/Deployments-and-ACL/tree/main/docs/deployments
6-
export const CHAIN_ADDRESSES: { [key in Chain]: { [key: string]: Address } } = {
5+
export const CHAIN_ADDRESSES = {
76
[Chain.Mainnet]: {
87
PufferVault: '0xD9A442856C234a39a81a089C06451EBAa4306a72',
98
},

lib/contracts/puffer-vault-handler.ts

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import {
2+
Address,
3+
Chain as ViemChain,
4+
PublicClient,
5+
WalletClient,
6+
getContract,
7+
} from 'viem';
8+
import { CHAIN_ABIS } from './abis/abis';
9+
import { Chain, VIEM_CHAINS } from '../chains/constants';
10+
import { CHAIN_ADDRESSES } from './addresses';
11+
12+
/**
13+
* Handler for the `PufferVaultV2` contract exposing methods to interact
14+
* with the contract.
15+
*/
16+
export class PufferVaultHandler {
17+
private viemChain: ViemChain;
18+
19+
/**
20+
* Create the handler for the `PufferVaultV2` contract exposing
21+
* methods to interact with the contract.
22+
*
23+
* @param chain Chain to use for the client.
24+
* @param walletClient The wallet client to use for wallet
25+
* interactions.
26+
* @param publicClient The public client to use for public
27+
* interactions.
28+
*/
29+
constructor(
30+
private chain: Chain,
31+
private walletClient: WalletClient,
32+
private publicClient: PublicClient,
33+
) {
34+
this.viemChain = VIEM_CHAINS[chain];
35+
}
36+
37+
// This is a method because the typings are complex and lost when
38+
// trying to make it a member.
39+
private getContract() {
40+
return getContract({
41+
address: CHAIN_ADDRESSES[this.chain].PufferVault as Address,
42+
abi: CHAIN_ABIS[this.chain].PufferVaultV2,
43+
client: {
44+
wallet: this.walletClient,
45+
public: this.publicClient,
46+
},
47+
});
48+
}
49+
50+
/**
51+
* Deposit ETH to the given wallet address. This doesn't make the
52+
* transaction but returns two methods namely `transact` and
53+
* `estimate`.
54+
*
55+
* @param walletAddress Wallet address to get the ETH from.
56+
* @returns `transact: (value: bigint) => Promise<Address>` - Used to
57+
* make the transaction with the given value.
58+
*
59+
* `estimate: () => Promise<bigint>` - Gas estimate of the
60+
* transaction.
61+
*/
62+
public depositETH(walletAddress: Address) {
63+
const transact = async (value: bigint) =>
64+
await this.getContract().write.depositETH([walletAddress], {
65+
account: walletAddress,
66+
chain: this.viemChain,
67+
value,
68+
});
69+
70+
const estimate = async () =>
71+
await this.getContract().estimateGas.depositETH([walletAddress], {
72+
account: walletAddress,
73+
});
74+
75+
return { transact, estimate };
76+
}
77+
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+
98+
/**
99+
* Deposit stETH to the given wallet address. This doesn't make the
100+
* transaction but returns two methods namely `transact` and
101+
* `estimate`.
102+
*
103+
* @param walletAddress Wallet address to get the ETH from.
104+
* @param value Value in wei of the stETH to deposit.
105+
* @returns `transact: () => Promise<Address>` - Used to make the
106+
* transaction with the given value.
107+
*
108+
* `estimate: () => Promise<bigint>` - Gas estimate of the
109+
* transaction.
110+
*/
111+
public async depositStETH(walletAddress: Address, value: bigint) {
112+
const transact = async () =>
113+
await this.getContract().write.depositStETH([value, walletAddress], {
114+
account: walletAddress,
115+
chain: this.viemChain,
116+
});
117+
118+
const estimate = async () =>
119+
await this.getContract().estimateGas.depositStETH(
120+
[value, walletAddress],
121+
{
122+
account: walletAddress,
123+
},
124+
);
125+
126+
return { transact, estimate };
127+
}
128+
129+
/**
130+
* Get the allowance for the given owner and spender.
131+
*
132+
* @param ownerAddress Address of the owner.
133+
* @param spenderAddress Address of the spender.
134+
* @returns Allowance for the given owner and spender.
135+
*/
136+
public async getAllowance(ownerAddress: Address, spenderAddress: Address) {
137+
return await this.getContract().read.allowance([
138+
ownerAddress,
139+
spenderAddress,
140+
]);
141+
}
142+
}

lib/services/contract-handler.ts

-65
This file was deleted.

0 commit comments

Comments
 (0)