@gauntlet-xyz/sdk 1.0.5 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/constants.d.ts +16 -0
  2. package/dist/constants.d.ts.map +1 -0
  3. package/dist/constants.js +28 -0
  4. package/dist/constants.js.map +1 -0
  5. package/dist/errors.d.ts +18 -3
  6. package/dist/errors.d.ts.map +1 -1
  7. package/dist/errors.js +20 -2
  8. package/dist/errors.js.map +1 -1
  9. package/dist/evm/abis/priceAndFeeCalculatorV2.d.ts +233 -0
  10. package/dist/evm/abis/priceAndFeeCalculatorV2.d.ts.map +1 -0
  11. package/dist/evm/abis/priceAndFeeCalculatorV2.js +119 -0
  12. package/dist/evm/abis/priceAndFeeCalculatorV2.js.map +1 -0
  13. package/dist/evm/abis/provisionerV2.d.ts +460 -0
  14. package/dist/evm/abis/provisionerV2.d.ts.map +1 -0
  15. package/dist/evm/abis/provisionerV2.js +203 -0
  16. package/dist/evm/abis/provisionerV2.js.map +1 -0
  17. package/dist/evm/adapters/aera.d.ts +3 -3
  18. package/dist/evm/adapters/aera.d.ts.map +1 -1
  19. package/dist/evm/adapters/aera.js +179 -103
  20. package/dist/evm/adapters/aera.js.map +1 -1
  21. package/dist/evm/adapters/types.d.ts +12 -1
  22. package/dist/evm/adapters/types.d.ts.map +1 -1
  23. package/dist/evm/aeraContracts/priceAndFeeCalculator.d.ts +41 -0
  24. package/dist/evm/aeraContracts/priceAndFeeCalculator.d.ts.map +1 -0
  25. package/dist/evm/aeraContracts/priceAndFeeCalculator.js +140 -0
  26. package/dist/evm/aeraContracts/priceAndFeeCalculator.js.map +1 -0
  27. package/dist/evm/aeraContracts/v1.d.ts +2317 -0
  28. package/dist/evm/aeraContracts/v1.d.ts.map +1 -0
  29. package/dist/evm/aeraContracts/v1.js +59 -0
  30. package/dist/evm/aeraContracts/v1.js.map +1 -0
  31. package/dist/evm/aeraContracts/v2.d.ts +2802 -0
  32. package/dist/evm/aeraContracts/v2.d.ts.map +1 -0
  33. package/dist/evm/aeraContracts/v2.js +217 -0
  34. package/dist/evm/aeraContracts/v2.js.map +1 -0
  35. package/dist/evm/aeraContracts.d.ts +23 -2316
  36. package/dist/evm/aeraContracts.d.ts.map +1 -1
  37. package/dist/evm/aeraContracts.js +117 -24
  38. package/dist/evm/aeraContracts.js.map +1 -1
  39. package/dist/evm/deposit.d.ts +5 -1
  40. package/dist/evm/deposit.d.ts.map +1 -1
  41. package/dist/evm/deposit.js +28 -14
  42. package/dist/evm/deposit.js.map +1 -1
  43. package/dist/evm/depositReceiverApproval.d.ts +17 -0
  44. package/dist/evm/depositReceiverApproval.d.ts.map +1 -0
  45. package/dist/evm/depositReceiverApproval.js +32 -0
  46. package/dist/evm/depositReceiverApproval.js.map +1 -0
  47. package/dist/evm/index.d.ts +8 -1
  48. package/dist/evm/index.d.ts.map +1 -1
  49. package/dist/evm/index.js +4 -0
  50. package/dist/evm/index.js.map +1 -1
  51. package/dist/evm/operationMode.d.ts +9 -0
  52. package/dist/evm/operationMode.d.ts.map +1 -0
  53. package/dist/evm/operationMode.js +41 -0
  54. package/dist/evm/operationMode.js.map +1 -0
  55. package/dist/evm/types.d.ts +7 -3
  56. package/dist/evm/types.d.ts.map +1 -1
  57. package/dist/evm/types.js +4 -1
  58. package/dist/evm/types.js.map +1 -1
  59. package/dist/evm/userCurrentBalance.d.ts.map +1 -1
  60. package/dist/evm/userCurrentBalance.js +68 -58
  61. package/dist/evm/userCurrentBalance.js.map +1 -1
  62. package/dist/evm/withdraw.d.ts +5 -1
  63. package/dist/evm/withdraw.d.ts.map +1 -1
  64. package/dist/evm/withdraw.js +25 -13
  65. package/dist/evm/withdraw.js.map +1 -1
  66. package/dist/index.d.ts +8 -1
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +5 -1
  69. package/dist/index.js.map +1 -1
  70. package/manifest/vaults.json +31 -139
  71. package/package.json +1 -1
  72. package/src/constants.ts +32 -0
  73. package/src/errors.ts +44 -5
  74. package/src/evm/abis/priceAndFeeCalculatorV2.ts +118 -0
  75. package/src/evm/abis/provisionerV2.ts +202 -0
  76. package/src/evm/adapters/aera.ts +419 -137
  77. package/src/evm/adapters/types.ts +19 -1
  78. package/src/evm/aeraContracts/priceAndFeeCalculator.ts +223 -0
  79. package/src/evm/aeraContracts/v1.ts +157 -0
  80. package/src/evm/aeraContracts/v2.ts +466 -0
  81. package/src/evm/aeraContracts.ts +173 -52
  82. package/src/evm/deposit.ts +48 -16
  83. package/src/evm/depositReceiverApproval.ts +51 -0
  84. package/src/evm/index.ts +23 -1
  85. package/src/evm/operationMode.ts +60 -0
  86. package/src/evm/types.ts +9 -3
  87. package/src/evm/userCurrentBalance.ts +80 -55
  88. package/src/evm/withdraw.ts +42 -15
  89. package/src/index.ts +19 -0
@@ -0,0 +1,223 @@
1
+ import type { Address, PublicClient } from 'viem';
2
+ import { UnsupportedFeatureError } from '../../errors';
3
+ import { priceAndFeeCalculatorAbi } from '../abis/priceAndFeeCalculator';
4
+ import { priceAndFeeCalculatorV2Abi } from '../abis/priceAndFeeCalculatorV2';
5
+ import { ContractVersion } from '../types';
6
+
7
+ const ROUNDING_FLOOR = 0;
8
+ const ROUNDING_CEIL = 1;
9
+
10
+ export interface NormalizedVaultPriceState {
11
+ paused: boolean;
12
+ unitPrice: bigint;
13
+ timestamp: number;
14
+ }
15
+
16
+ function toNumber(value: bigint | number): number {
17
+ return typeof value === 'bigint' ? Number(value) : value;
18
+ }
19
+
20
+ function normalizeVaultPriceState<
21
+ TState extends { paused: boolean } & (
22
+ | { unitPrice: bigint; timestamp: bigint | number }
23
+ | {
24
+ anchorPrice: bigint;
25
+ driftPrice: bigint;
26
+ anchorTimestamp: bigint | number;
27
+ driftTimestamp: bigint | number;
28
+ }
29
+ ),
30
+ >(priceState: TState): TState & NormalizedVaultPriceState {
31
+ if ('unitPrice' in priceState) {
32
+ return {
33
+ ...priceState,
34
+ unitPrice: priceState.unitPrice,
35
+ timestamp: toNumber(priceState.timestamp),
36
+ } as TState & NormalizedVaultPriceState;
37
+ }
38
+
39
+ const anchorTimestamp = toNumber(priceState.anchorTimestamp);
40
+ const driftTimestamp = toNumber(priceState.driftTimestamp);
41
+ const useDriftPrice = driftTimestamp >= anchorTimestamp;
42
+
43
+ return {
44
+ ...priceState,
45
+ unitPrice: useDriftPrice ? priceState.driftPrice : priceState.anchorPrice,
46
+ timestamp: useDriftPrice ? driftTimestamp : anchorTimestamp,
47
+ } as TState & NormalizedVaultPriceState;
48
+ }
49
+
50
+ export async function convertTokenToUnitsIfActive(
51
+ client: PublicClient,
52
+ feeCalculator: Address,
53
+ feeCalculatorVersion: ContractVersion,
54
+ vault: Address,
55
+ token: Address,
56
+ tokenAmount: bigint,
57
+ rounding: typeof ROUNDING_FLOOR | typeof ROUNDING_CEIL
58
+ ): Promise<bigint> {
59
+ if (feeCalculatorVersion === ContractVersion.V2) {
60
+ return client.readContract({
61
+ address: feeCalculator,
62
+ abi: priceAndFeeCalculatorV2Abi,
63
+ functionName: 'convertTokenToUnitsIfActive',
64
+ args: [vault, token, tokenAmount, rounding],
65
+ });
66
+ }
67
+
68
+ return client.readContract({
69
+ address: feeCalculator,
70
+ abi: priceAndFeeCalculatorAbi,
71
+ functionName: 'convertTokenToUnitsIfActive',
72
+ args: [vault, token, tokenAmount, rounding],
73
+ });
74
+ }
75
+
76
+ export async function convertUnitsToTokenIfActive(
77
+ client: PublicClient,
78
+ feeCalculator: Address,
79
+ feeCalculatorVersion: ContractVersion,
80
+ vault: Address,
81
+ token: Address,
82
+ unitsAmount: bigint,
83
+ rounding: typeof ROUNDING_FLOOR | typeof ROUNDING_CEIL
84
+ ): Promise<bigint> {
85
+ if (feeCalculatorVersion === ContractVersion.V2) {
86
+ return client.readContract({
87
+ address: feeCalculator,
88
+ abi: priceAndFeeCalculatorV2Abi,
89
+ functionName: 'convertUnitsToTokenIfActive',
90
+ args: [vault, token, unitsAmount, rounding],
91
+ });
92
+ }
93
+
94
+ return client.readContract({
95
+ address: feeCalculator,
96
+ abi: priceAndFeeCalculatorAbi,
97
+ functionName: 'convertUnitsToTokenIfActive',
98
+ args: [vault, token, unitsAmount, rounding],
99
+ });
100
+ }
101
+
102
+ export async function convertTokenToUnits(
103
+ client: PublicClient,
104
+ feeCalculator: Address,
105
+ feeCalculatorVersion: ContractVersion,
106
+ vault: Address,
107
+ token: Address,
108
+ tokenAmount: bigint
109
+ ): Promise<bigint> {
110
+ if (feeCalculatorVersion === ContractVersion.V2) {
111
+ return client.readContract({
112
+ address: feeCalculator,
113
+ abi: priceAndFeeCalculatorV2Abi,
114
+ functionName: 'convertTokenToUnits',
115
+ args: [vault, token, tokenAmount],
116
+ });
117
+ }
118
+
119
+ return client.readContract({
120
+ address: feeCalculator,
121
+ abi: priceAndFeeCalculatorAbi,
122
+ functionName: 'convertTokenToUnits',
123
+ args: [vault, token, tokenAmount],
124
+ });
125
+ }
126
+
127
+ export async function convertUnitsToToken(
128
+ client: PublicClient,
129
+ feeCalculator: Address,
130
+ feeCalculatorVersion: ContractVersion,
131
+ vault: Address,
132
+ token: Address,
133
+ unitsAmount: bigint
134
+ ): Promise<bigint> {
135
+ if (feeCalculatorVersion === ContractVersion.V2) {
136
+ return client.readContract({
137
+ address: feeCalculator,
138
+ abi: priceAndFeeCalculatorV2Abi,
139
+ functionName: 'convertUnitsToToken',
140
+ args: [vault, token, unitsAmount],
141
+ });
142
+ }
143
+
144
+ return client.readContract({
145
+ address: feeCalculator,
146
+ abi: priceAndFeeCalculatorAbi,
147
+ functionName: 'convertUnitsToToken',
148
+ args: [vault, token, unitsAmount],
149
+ });
150
+ }
151
+
152
+ export async function getAnchorTimestamp(
153
+ client: PublicClient,
154
+ feeCalculator: Address,
155
+ feeCalculatorVersion: ContractVersion,
156
+ vault: Address
157
+ ): Promise<number> {
158
+ if (feeCalculatorVersion !== ContractVersion.V2) {
159
+ throw new UnsupportedFeatureError('Aera: sync redeem requires V2 price and fee calculator');
160
+ }
161
+
162
+ return client.readContract({
163
+ address: feeCalculator,
164
+ abi: priceAndFeeCalculatorV2Abi,
165
+ functionName: 'getAnchorTimestamp',
166
+ args: [vault],
167
+ });
168
+ }
169
+
170
+ export async function getVaultState(
171
+ client: PublicClient,
172
+ feeCalculator: Address,
173
+ feeCalculatorVersion: ContractVersion,
174
+ vault: Address
175
+ ) {
176
+ if (feeCalculatorVersion === ContractVersion.V2) {
177
+ const [priceState, accruals] = await client.readContract({
178
+ address: feeCalculator,
179
+ abi: priceAndFeeCalculatorV2Abi,
180
+ functionName: 'getVaultState',
181
+ args: [vault],
182
+ });
183
+
184
+ return [normalizeVaultPriceState(priceState), accruals] as const;
185
+ }
186
+
187
+ const [priceState, accruals] = await client.readContract({
188
+ address: feeCalculator,
189
+ abi: priceAndFeeCalculatorAbi,
190
+ functionName: 'getVaultState',
191
+ args: [vault],
192
+ });
193
+
194
+ return [normalizeVaultPriceState(priceState), accruals] as const;
195
+ }
196
+
197
+ export async function isVaultPaused(
198
+ client: PublicClient,
199
+ feeCalculator: Address,
200
+ feeCalculatorVersion: ContractVersion,
201
+ vault: Address
202
+ ): Promise<boolean> {
203
+ if (feeCalculatorVersion === ContractVersion.V2) {
204
+ return client.readContract({
205
+ address: feeCalculator,
206
+ abi: priceAndFeeCalculatorV2Abi,
207
+ functionName: 'isVaultPaused',
208
+ args: [vault],
209
+ });
210
+ }
211
+
212
+ return client.readContract({
213
+ address: feeCalculator,
214
+ abi: priceAndFeeCalculatorAbi,
215
+ functionName: 'isVaultPaused',
216
+ args: [vault],
217
+ });
218
+ }
219
+
220
+ export const Rounding = {
221
+ Floor: ROUNDING_FLOOR,
222
+ Ceil: ROUNDING_CEIL,
223
+ } as const;
@@ -0,0 +1,157 @@
1
+ import {
2
+ type Address,
3
+ type Client,
4
+ type GetContractReturnType,
5
+ type PublicClient,
6
+ getContract,
7
+ } from 'viem';
8
+ import { priceAndFeeCalculatorAbi } from '../abis/priceAndFeeCalculator';
9
+ import { provisionerAbi } from '../abis/provisioner';
10
+ import {
11
+ Rounding,
12
+ convertTokenToUnitsIfActive,
13
+ convertUnitsToTokenIfActive,
14
+ } from './priceAndFeeCalculator';
15
+ import { MAX_BPS } from '../../constants';
16
+ import type { ContractVersion } from '../types';
17
+
18
+ export type PriceAndFeeCalculatorContract<T extends Client> = GetContractReturnType<
19
+ typeof priceAndFeeCalculatorAbi,
20
+ T,
21
+ Address
22
+ >;
23
+
24
+ export function getPriceAndFeeCalculator<T extends Client>(
25
+ client: T,
26
+ address: Address
27
+ ): PriceAndFeeCalculatorContract<T> {
28
+ return getContract({
29
+ address,
30
+ abi: priceAndFeeCalculatorAbi,
31
+ client,
32
+ });
33
+ }
34
+
35
+ function getProvisioner<T extends Client>(client: T, address: Address) {
36
+ return getContract({
37
+ address,
38
+ abi: provisionerAbi,
39
+ client,
40
+ });
41
+ }
42
+
43
+ async function getTokenDetails(client: PublicClient, provisioner: Address, token: Address) {
44
+ return getProvisioner(client, provisioner).read.tokensDetails([token]);
45
+ }
46
+
47
+ export async function getAsyncDepositUnitsOut(
48
+ client: PublicClient,
49
+ provisioner: Address,
50
+ feeCalculator: Address,
51
+ feeCalculatorVersion: ContractVersion,
52
+ vault: Address,
53
+ token: Address,
54
+ tokensIn: bigint
55
+ ): Promise<bigint> {
56
+ const tokenDetails = await getTokenDetails(client, provisioner, token);
57
+ const depositMultiplier = BigInt(tokenDetails[3]);
58
+ const adjustedTokensIn = (tokensIn * depositMultiplier) / MAX_BPS;
59
+
60
+ return convertTokenToUnitsIfActive(
61
+ client,
62
+ feeCalculator,
63
+ feeCalculatorVersion,
64
+ vault,
65
+ token,
66
+ adjustedTokensIn,
67
+ Rounding.Floor
68
+ );
69
+ }
70
+
71
+ export async function getAsyncRedeemTokenOut(
72
+ client: PublicClient,
73
+ provisioner: Address,
74
+ feeCalculator: Address,
75
+ feeCalculatorVersion: ContractVersion,
76
+ vault: Address,
77
+ token: Address,
78
+ unitsIn: bigint
79
+ ): Promise<bigint> {
80
+ const tokenDetails = await getTokenDetails(client, provisioner, token);
81
+ const redeemMultiplier = BigInt(tokenDetails[4]);
82
+ const tokensOut = await convertUnitsToTokenIfActive(
83
+ client,
84
+ feeCalculator,
85
+ feeCalculatorVersion,
86
+ vault,
87
+ token,
88
+ unitsIn,
89
+ Rounding.Floor
90
+ );
91
+
92
+ return (tokensOut * redeemMultiplier) / MAX_BPS;
93
+ }
94
+
95
+ export async function getAsyncWithdrawUnitsIn(
96
+ client: PublicClient,
97
+ provisioner: Address,
98
+ feeCalculator: Address,
99
+ feeCalculatorVersion: ContractVersion,
100
+ vault: Address,
101
+ token: Address,
102
+ tokensOut: bigint
103
+ ): Promise<bigint> {
104
+ const tokenDetails = await getTokenDetails(client, provisioner, token);
105
+ const redeemMultiplier = BigInt(tokenDetails[4]);
106
+ const preMultiplierTokensOut = (tokensOut * MAX_BPS + redeemMultiplier - 1n) / redeemMultiplier;
107
+
108
+ return convertTokenToUnitsIfActive(
109
+ client,
110
+ feeCalculator,
111
+ feeCalculatorVersion,
112
+ vault,
113
+ token,
114
+ preMultiplierTokensOut,
115
+ Rounding.Ceil
116
+ );
117
+ }
118
+
119
+ export function requestDepositTxRequest(
120
+ provisioner: Address,
121
+ token: Address,
122
+ tokensIn: bigint,
123
+ minUnitsOut: bigint,
124
+ solverTip: bigint,
125
+ deadline: bigint,
126
+ maxPriceAge: bigint,
127
+ isFixedPrice: boolean,
128
+ account: Address
129
+ ) {
130
+ return {
131
+ address: provisioner,
132
+ abi: provisionerAbi,
133
+ functionName: 'requestDeposit' as const,
134
+ args: [token, tokensIn, minUnitsOut, solverTip, deadline, maxPriceAge, isFixedPrice] as const,
135
+ account,
136
+ };
137
+ }
138
+
139
+ export function requestRedeemTxRequest(
140
+ provisioner: Address,
141
+ token: Address,
142
+ unitsIn: bigint,
143
+ minTokenOut: bigint,
144
+ solverTip: bigint,
145
+ deadline: bigint,
146
+ maxPriceAge: bigint,
147
+ isFixedPrice: boolean,
148
+ account: Address
149
+ ) {
150
+ return {
151
+ address: provisioner,
152
+ abi: provisionerAbi,
153
+ functionName: 'requestRedeem' as const,
154
+ args: [token, unitsIn, minTokenOut, solverTip, deadline, maxPriceAge, isFixedPrice] as const,
155
+ account,
156
+ };
157
+ }