Skip to content

Commit f4f9403

Browse files
committed
[list actions] move list actions to sdk
1 parent 3aca780 commit f4f9403

14 files changed

+334
-107
lines changed

README.md

+153-60
Large diffs are not rendered by default.

src/methods/vault/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import getExitQueuePositions from './requests/getExitQueuePositions'
1616
import { deposit } from './transactions/deposit'
1717
import { withdraw } from './transactions/withdraw'
1818
import { claimExitQueue } from './transactions/claimExitQueue'
19+
import { updateWhitelist } from './transactions/updateWhitelist'
20+
import { updateBlocklist } from './transactions/updateBlocklist'
1921

2022

2123
export default {
@@ -37,5 +39,7 @@ export default {
3739
deposit,
3840
withdraw,
3941
claimExitQueue,
42+
updateWhitelist,
43+
updateBlocklist,
4044
},
4145
} as const
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { UpdateBlocklistInput } from './types'
2+
import { validateArgs } from '../../../../utils'
3+
import { vaultMulticall } from '../../../../contracts'
4+
5+
6+
const validateList = (blocklist: UpdateBlocklistInput['blocklist']) => {
7+
const isValid = blocklist.every((blocklistItem) => (
8+
blocklistItem
9+
&& typeof blocklistItem === 'object'
10+
&& typeof blocklistItem.address === 'string'
11+
&& typeof blocklistItem.isNew === 'boolean'
12+
))
13+
14+
if (!isValid) {
15+
throw new Error('The "blocklist" argument must be an array of objects with "address" and "isNew" properties')
16+
}
17+
}
18+
19+
export const commonLogic = (values: UpdateBlocklistInput) => {
20+
const { options, contracts, userAddress, vaultAddress, blocklist } = values
21+
22+
validateArgs.array({ blocklist })
23+
validateArgs.address({ vaultAddress, userAddress })
24+
validateList(blocklist)
25+
26+
const multicallCommonArgs: Omit<Parameters<typeof vaultMulticall>[0], 'request'> = {
27+
vaultContract: contracts.helpers.createBlocklistVault(vaultAddress),
28+
keeperContract: contracts.base.keeper,
29+
vaultAddress,
30+
userAddress,
31+
options,
32+
}
33+
34+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = blocklist.map(({ address, isNew }) => ({
35+
method: 'updateBlocklist',
36+
args: [ address, isNew ],
37+
}))
38+
39+
return {
40+
params,
41+
multicallCommonArgs,
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as updateBlocklist } from './updateBlocklist'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import updateBlocklistGas from './updateBlocklistGas'
2+
import updateBlocklistEncode from './updateBlocklistEncode'
3+
4+
5+
export type UpdateBlocklistInput = {
6+
blocklist: Array<{
7+
address: string
8+
isNew: boolean
9+
}>
10+
userAddress: string
11+
vaultAddress: string
12+
options: StakeWise.Options
13+
provider: StakeWise.Provider
14+
contracts: StakeWise.Contracts
15+
}
16+
17+
export interface UpdateBlocklist {
18+
(values: UpdateBlocklistInput): Promise<StakeWise.TransactionHash>
19+
estimateGas: typeof updateBlocklistGas
20+
encode: typeof updateBlocklistEncode
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { UpdateBlocklist } from './types'
2+
import { commonLogic } from './common'
3+
import updateBlocklistGas from './updateBlocklistGas'
4+
import updateBlocklistEncode from './updateBlocklistEncode'
5+
import { vaultMulticall } from '../../../../contracts'
6+
7+
8+
const updateBlocklist: UpdateBlocklist = async (values) => {
9+
const { params, multicallCommonArgs } = commonLogic(values)
10+
11+
const result = await vaultMulticall<{ hash: string }>({
12+
...multicallCommonArgs,
13+
request: {
14+
params,
15+
},
16+
})
17+
18+
return result.hash
19+
}
20+
21+
updateBlocklist.encode = updateBlocklistEncode
22+
updateBlocklist.estimateGas = updateBlocklistGas
23+
24+
25+
export default updateBlocklist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { commonLogic } from './common'
2+
import { UpdateBlocklistInput } from './types'
3+
import { vaultMulticall } from '../../../../contracts'
4+
5+
6+
const updateBlocklistEncode = async (values: UpdateBlocklistInput): Promise<StakeWise.TransactionData> => {
7+
const { params, multicallCommonArgs } = commonLogic(values)
8+
9+
const rx = await vaultMulticall<{ data: string, to: string }>({
10+
...multicallCommonArgs,
11+
request: {
12+
params,
13+
transactionData: true,
14+
},
15+
})
16+
17+
return {
18+
data: rx.data,
19+
to: rx.to,
20+
}
21+
}
22+
23+
24+
export default updateBlocklistEncode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { commonLogic } from './common'
2+
import { UpdateBlocklistInput } from './types'
3+
import { getGas } from '../../../../utils'
4+
import { vaultMulticall } from '../../../../contracts'
5+
6+
7+
const updateBlocklistGas = async (values: UpdateBlocklistInput) => {
8+
const { params, multicallCommonArgs } = commonLogic(values)
9+
10+
const estimatedGas = await vaultMulticall<bigint>({
11+
...multicallCommonArgs,
12+
request: {
13+
estimateGas: true,
14+
params,
15+
},
16+
})
17+
18+
const gas = await getGas({ estimatedGas, provider: values.provider })
19+
20+
return gas
21+
}
22+
23+
24+
export default updateBlocklistGas

src/methods/vault/transactions/updateWhitelist/common.ts

+22-40
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,38 @@ import { validateArgs } from '../../../../utils'
33
import { vaultMulticall } from '../../../../contracts'
44

55

6-
export const commonLogic = async (values: UpdateWhitelistInput) => {
7-
const { options, contracts, assets, vaultAddress, userAddress } = values
6+
const validateList = (whitelist: UpdateWhitelistInput['whitelist']) => {
7+
const isValid = whitelist.every((whitelistItem) => (
8+
whitelistItem
9+
&& typeof whitelistItem === 'object'
10+
&& typeof whitelistItem.address === 'string'
11+
&& typeof whitelistItem.isNew === 'boolean'
12+
))
13+
14+
if (!isValid) {
15+
throw new Error('The "whitelist" argument must be an array of objects with "address" and "isNew" properties')
16+
}
17+
}
818

9-
validateArgs.bigint({ assets })
10-
validateArgs.address({ vaultAddress, userAddress })
19+
export const commonLogic = (values: UpdateWhitelistInput) => {
20+
const { options, contracts, userAddress, vaultAddress, whitelist } = values
1121

12-
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = []
22+
validateArgs.array({ whitelist })
23+
validateArgs.address({ vaultAddress, userAddress })
24+
validateList(whitelist)
1325

1426
const multicallCommonArgs: Omit<Parameters<typeof vaultMulticall>[0], 'request'> = {
15-
vaultContract: contracts.helpers.createVault(vaultAddress),
27+
vaultContract: contracts.helpers.createPrivateVault(vaultAddress),
1628
keeperContract: contracts.base.keeper,
1729
vaultAddress,
1830
userAddress,
1931
options,
2032
}
2133

22-
const isCollateralized = await contracts.base.keeper.isCollateralized(vaultAddress)
23-
24-
if (isCollateralized) {
25-
const result = await vaultMulticall<[ { shares: bigint } ]>({
26-
...multicallCommonArgs,
27-
request: {
28-
params: [ { method: 'convertToShares', args: [ assets ] } ],
29-
callStatic: true,
30-
},
31-
})
32-
33-
const exitQueueShares = result[0].shares
34-
35-
params.push({
36-
method: 'enterExitQueue',
37-
args: [ exitQueueShares, userAddress ],
38-
})
39-
}
40-
else {
41-
const result = await vaultMulticall<[ { shares: bigint } ]>({
42-
...multicallCommonArgs,
43-
request: {
44-
params: [ { method: 'convertToShares', args: [ assets ] } ],
45-
callStatic: true,
46-
},
47-
})
48-
49-
const shares = result[0].shares
50-
51-
params.push({
52-
method: 'redeem',
53-
args: [ shares, userAddress ],
54-
})
55-
}
34+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = whitelist.map(({ address, isNew }) => ({
35+
method: 'updateWhitelist',
36+
args: [ address, isNew ],
37+
}))
5638

5739
return {
5840
params,

src/methods/vault/transactions/updateWhitelist/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type UpdateWhitelistInput = {
77
address: string
88
isNew: boolean
99
}>
10+
userAddress: string
1011
vaultAddress: string
1112
options: StakeWise.Options
1213
provider: StakeWise.Provider

src/methods/vault/transactions/updateWhitelist/updateWhitelist.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { vaultMulticall } from '../../../../contracts'
66

77

88
const updateWhitelist: UpdateWhitelist = async (values) => {
9-
const { params, multicallCommonArgs } = await commonLogic(values)
9+
const { params, multicallCommonArgs } = commonLogic(values)
1010

1111
const result = await vaultMulticall<{ hash: string }>({
1212
...multicallCommonArgs,

src/methods/vault/transactions/updateWhitelist/updateWhitelistEncode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { commonLogic } from './common'
2-
import { WithdrawInput } from './types'
2+
import { UpdateWhitelistInput } from './types'
33
import { vaultMulticall } from '../../../../contracts'
44

55

6-
const updateWhitelistEncode = async (values: WithdrawInput): Promise<StakeWise.TransactionData> => {
7-
const { params, multicallCommonArgs } = await commonLogic(values)
6+
const updateWhitelistEncode = async (values: UpdateWhitelistInput): Promise<StakeWise.TransactionData> => {
7+
const { params, multicallCommonArgs } = commonLogic(values)
88

99
const rx = await vaultMulticall<{ data: string, to: string }>({
1010
...multicallCommonArgs,

src/methods/vault/transactions/updateWhitelist/updateWhitelistGas.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { commonLogic } from './common'
2-
import { WithdrawInput } from './types'
2+
import { UpdateWhitelistInput } from './types'
33
import { getGas } from '../../../../utils'
44
import { vaultMulticall } from '../../../../contracts'
55

66

7-
const updateWhitelistGas = async (values: WithdrawInput) => {
8-
const { params, multicallCommonArgs } = await commonLogic(values)
7+
const updateWhitelistGas = async (values: UpdateWhitelistInput) => {
8+
const { params, multicallCommonArgs } = commonLogic(values)
99

1010
const estimatedGas = await vaultMulticall<bigint>({
1111
...multicallCommonArgs,

src/utils/validateArgs.ts

+9
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ const number = (values: Record<string, number>) => {
3333
})
3434
}
3535

36+
const array = (values: Record<string, any[]>) => {
37+
Object.keys(values).forEach((key) => {
38+
if (!Array.isArray(values[key])) {
39+
throw new Error(`The "${key}" argument must be an array`)
40+
}
41+
})
42+
}
43+
3644

3745
export default {
46+
array,
3847
bigint,
3948
string,
4049
number,

0 commit comments

Comments
 (0)