1
1
import {
2
- Address ,
3
2
PublicClient ,
4
3
WalletClient ,
5
4
createPublicClient ,
6
5
createWalletClient ,
7
- getContract ,
8
6
http ,
9
7
} 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' ;
14
10
15
11
/**
16
12
* The core class and the main entry point of the Puffer SDK.
17
13
*/
18
14
export class PufferClient {
19
- private chainAddresses : ValueOf < typeof CHAIN_ADDRESSES > ;
20
- private chainAbis : ValueOf < typeof CHAIN_ABIS > ;
21
-
22
- private viemChain : ViemChain ;
23
15
private walletClient : WalletClient ;
24
16
private publicClient : PublicClient ;
25
17
18
+ // Contract Handlers
19
+ public vault : PufferVaultHandler ;
20
+
26
21
/**
27
22
* Create the Puffer Client.
23
+ *
28
24
* @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.
31
29
*/
32
30
constructor (
33
31
chain : Chain ,
34
32
walletClient ?: WalletClient ,
35
33
publicClient ?: PublicClient ,
36
34
) {
37
- this . chainAddresses = CHAIN_ADDRESSES [ chain ] ;
38
- this . chainAbis = CHAIN_ABIS [ chain ] ;
39
- this . viemChain = VIEM_CHAINS [ chain ] ;
35
+ const viemChain = VIEM_CHAINS [ chain ] ;
40
36
41
37
this . walletClient =
42
38
walletClient ??
43
39
createWalletClient ( {
44
- chain : this . viemChain ,
40
+ chain : viemChain ,
45
41
transport : http ( ) ,
46
42
} ) ;
47
43
this . publicClient =
48
44
publicClient ??
49
45
createPublicClient ( {
50
- chain : this . viemChain ,
46
+ chain : viemChain ,
51
47
transport : http ( ) ,
52
48
} ) ;
49
+
50
+ this . vault = new PufferVaultHandler (
51
+ chain ,
52
+ this . walletClient ,
53
+ this . publicClient ,
54
+ ) ;
53
55
}
54
56
55
57
/**
@@ -60,98 +62,4 @@ export class PufferClient {
60
62
public async requestAddresses ( ) {
61
63
return await this . walletClient . requestAddresses ( ) ;
62
64
}
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
- }
157
65
}
0 commit comments