Skip to content

Commit c0740d4

Browse files
committed
SDK vault methods (#84)
* [sdk vault methods] update transactions * [sdk vault methods] update check access * [sdk vault methods] update methods * [sdk vault methods] add operate multicall * [sdk vault methods] update sdk multicall * [sdk vault methods] update sdk multicall * [sdk vault methods] update readme * [sdk vault methods] update sdk * [sdk vault methods] update sdk * [sdk vault methods] fix types * [sdk vault methods] update readme * [sdk vault methods] update deps * [sdk vault methods] update validation * [sdk vault methods] update operate contract * [sdk vault methods] update v3 sdk * [sdk vault methods] update v3 sdk * [sdk vault methods] update v3 sdk
1 parent 3c6e12b commit c0740d4

21 files changed

+371
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { CheckInput } from './types'
2+
3+
4+
const checkAdminAccess = async ({ userAddress, vaultAddress, contracts }: CheckInput) => {
5+
try {
6+
const vaultContract = await contracts.helpers.createVault(vaultAddress)
7+
const admin = await vaultContract.admin()
8+
const hasAccess = admin.toLowerCase() === userAddress.toLowerCase()
9+
10+
if (!hasAccess) {
11+
return Promise.reject('User must be the vault admin to perform this action')
12+
}
13+
}
14+
catch {}
15+
}
16+
17+
18+
export default checkAdminAccess
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { CheckInput } from './types'
2+
3+
4+
const checkBlocklistManagerAccess = async ({ userAddress, vaultAddress, contracts }: CheckInput) => {
5+
try {
6+
const vaultContract = await contracts.helpers.createBlocklistedVault(vaultAddress)
7+
const blocklistManager = await vaultContract.blocklistManager()
8+
const hasAccess = blocklistManager.toLowerCase() === userAddress.toLowerCase()
9+
10+
if (!hasAccess) {
11+
return Promise.reject('User must be the vault blocklist manager to perform this action')
12+
}
13+
}
14+
catch {}
15+
}
16+
17+
18+
export default checkBlocklistManagerAccess
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { CheckInput } from './types'
2+
3+
4+
const checkKeysManagerAccess = async ({ userAddress, vaultAddress, contracts }: CheckInput) => {
5+
try {
6+
const vaultContract = await contracts.helpers.createVault(vaultAddress)
7+
const keysManager = await vaultContract.keysManager()
8+
const hasAccess = keysManager.toLowerCase() === userAddress.toLowerCase()
9+
10+
if (!hasAccess) {
11+
return Promise.reject('User must be the vault keys manager to perform this action')
12+
}
13+
}
14+
catch {}
15+
}
16+
17+
18+
export default checkKeysManagerAccess
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { CheckInput } from './types'
2+
3+
4+
const checkWhitelisterAccess = async ({ userAddress, vaultAddress, contracts }: CheckInput) => {
5+
try {
6+
const vaultContract = await contracts.helpers.createPrivateVault(vaultAddress)
7+
const whitelister = await vaultContract.whitelister()
8+
const hasAccess = whitelister.toLowerCase() === userAddress.toLowerCase()
9+
10+
if (!hasAccess) {
11+
return Promise.reject('User must be the vault whitelister to perform this action')
12+
}
13+
}
14+
catch {}
15+
}
16+
17+
18+
export default checkWhitelisterAccess
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as checkAdminAccess } from './checkAdminAccess'
2+
export { default as checkKeysManagerAccess } from './checkKeysManagerAccess'
3+
export { default as checkWhitelisterAccess } from './checkWhitelisterAccess'
4+
export { default as checkBlocklistManagerAccess } from './checkBlocklistManagerAccess'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type CheckInput = {
2+
userAddress: string
3+
vaultAddress: string
4+
contracts: StakeWise.Contracts
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './check'
2+
export * from './params'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type SetBlocklistManagerParams = {
6+
blocklistManager: string
7+
}
8+
9+
const getBlocklistManagerParams = (values: SetBlocklistManagerParams) => {
10+
const { blocklistManager } = values
11+
12+
validateArgs.address({ blocklistManager })
13+
14+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = [
15+
{
16+
method: 'setBlocklistManager', args: [ blocklistManager ],
17+
},
18+
]
19+
20+
return params
21+
}
22+
23+
24+
export default getBlocklistManagerParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import validateList from './validateList'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type UpdateBlocklistParams = {
6+
blocklist: Array<{
7+
address: string
8+
isNew: boolean
9+
}>
10+
}
11+
12+
const getBlocklistParams = (values: UpdateBlocklistParams) => {
13+
const { blocklist } = values
14+
15+
validateList({ blocklist })
16+
17+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = blocklist.map(({ address, isNew }) => ({
18+
method: 'updateBlocklist',
19+
args: [ address, isNew ],
20+
}))
21+
22+
return params
23+
}
24+
25+
26+
export default getBlocklistParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type SetFeeRecipientParams = {
6+
feeRecipient: string
7+
}
8+
9+
const getFeeRecipientParams = (values: SetFeeRecipientParams) => {
10+
const { feeRecipient } = values
11+
12+
validateArgs.address({ feeRecipient })
13+
14+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = [
15+
{
16+
method: 'setFeeRecipient', args: [ feeRecipient ],
17+
},
18+
]
19+
20+
return params
21+
}
22+
23+
24+
export default getFeeRecipientParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type SetKeysManagerParams = {
6+
keysManager: string
7+
}
8+
9+
const getKeysManagerParams = (values: SetKeysManagerParams) => {
10+
const { keysManager } = values
11+
12+
validateArgs.address({ keysManager })
13+
14+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = [
15+
{
16+
method: 'setKeysManager', args: [ keysManager ],
17+
},
18+
]
19+
20+
return params
21+
}
22+
23+
24+
export default getKeysManagerParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type SetMetadataParams = {
6+
metadataIpfsHash: string
7+
}
8+
9+
const getMetadataParams = (values: SetMetadataParams) => {
10+
const { metadataIpfsHash } = values
11+
12+
validateArgs.string({ metadataIpfsHash })
13+
14+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = [
15+
{
16+
method: 'setMetadata', args: [ metadataIpfsHash ],
17+
},
18+
]
19+
20+
return params
21+
}
22+
23+
24+
export default getMetadataParams
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type SetValidatorsRootParams = {
6+
validatorsRoot: string
7+
}
8+
9+
const getValidatorsRootParams = (values: SetValidatorsRootParams) => {
10+
const { validatorsRoot } = values
11+
12+
validateArgs.string({ validatorsRoot })
13+
14+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = [
15+
{
16+
method: 'setValidatorsRoot', args: [ validatorsRoot ],
17+
},
18+
]
19+
20+
return params
21+
}
22+
23+
24+
export default getValidatorsRootParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import validateList from './validateList'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type UpdateWhitelistParams = {
6+
whitelist: Array<{
7+
address: string
8+
isNew: boolean
9+
}>
10+
}
11+
12+
const getWhitelistParams = (values: UpdateWhitelistParams) => {
13+
const { whitelist } = values
14+
15+
validateList({ whitelist })
16+
17+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = whitelist.map(({ address, isNew }) => ({
18+
method: 'updateWhitelist',
19+
args: [ address, isNew ],
20+
}))
21+
22+
return params
23+
}
24+
25+
26+
export default getWhitelistParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
import { vaultMulticall } from '../../../../../../contracts'
3+
4+
5+
export type SetWhitelisterParams = {
6+
whitelister: string
7+
}
8+
9+
const getWhitelisterParams = (values: SetWhitelisterParams) => {
10+
const { whitelister } = values
11+
12+
validateArgs.address({ whitelister })
13+
14+
const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = [
15+
{
16+
method: 'setWhitelister', args: [ whitelister ],
17+
},
18+
]
19+
20+
return params
21+
}
22+
23+
24+
export default getWhitelisterParams
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { default as getMetadataParams } from './getMetadataParams'
2+
export { default as getBlocklistParams } from './getBlocklistParams'
3+
export { default as getWhitelistParams } from './getWhitelistParams'
4+
export { default as getKeysManagerParams } from './getKeysManagerParams'
5+
export { default as getWhitelisterParams } from './getWhitelisterParams'
6+
export { default as getFeeRecipientParams } from './getFeeRecipientParams'
7+
export { default as getValidatorsRootParams } from './getValidatorsRootParams'
8+
export { default as getBlocklistManagerParams } from './getBlocklistManagerParams'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { validateArgs } from '../../../../../../utils'
2+
3+
4+
type ListItem = {
5+
address: string
6+
isNew: boolean
7+
}
8+
9+
const validateList = (values: Record<string, ListItem[]>) => {
10+
Object.keys(values).forEach((key) => {
11+
validateArgs.array({ [key]: values[key] })
12+
13+
const isValid = values[key].every((listItem) => (
14+
listItem
15+
&& typeof listItem === 'object'
16+
&& typeof listItem.address === 'string'
17+
&& typeof listItem.isNew === 'boolean'
18+
))
19+
20+
if (!isValid) {
21+
throw new Error(`The "${key}" argument must be an array of objects with "address" and "isNew" properties`)
22+
}
23+
})
24+
}
25+
26+
27+
export default validateList
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { vaultMulticall } from '../../../contracts'
2+
3+
4+
type Input = Parameters<typeof vaultMulticall>[0]
5+
6+
const getMulticallEncode = async (values: Input): Promise<StakeWise.TransactionData> => {
7+
const rx = await vaultMulticall<{ data: string, to: string }>({
8+
...values,
9+
request: {
10+
...values.request,
11+
transactionData: true,
12+
},
13+
})
14+
15+
return {
16+
data: rx.data,
17+
to: rx.to,
18+
}
19+
}
20+
21+
22+
export default getMulticallEncode
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getGas } from '../../../utils'
2+
import { vaultMulticall } from '../../../contracts'
3+
4+
5+
type Input = Parameters<typeof vaultMulticall>[0] & {
6+
provider: StakeWise.Provider
7+
}
8+
9+
const getMulticallGas = async (values: Input) => {
10+
const { provider } = values
11+
12+
const estimatedGas = await vaultMulticall<bigint>({
13+
...values,
14+
request: {
15+
...values.request,
16+
estimateGas: true,
17+
},
18+
})
19+
20+
return getGas({ estimatedGas, provider })
21+
}
22+
23+
24+
export default getMulticallGas

src/methods/vault/utils/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as getMulticallGas } from './getMulticallGas'
2+
export { default as getMulticallEncode } from './getMulticallEncode'
3+
export type { BaseInput } from './types'

src/methods/vault/utils/types.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type BaseInput = {
2+
userAddress: string
3+
vaultAddress: string
4+
options: StakeWise.Options
5+
contracts: StakeWise.Contracts
6+
provider: StakeWise.Provider
7+
}

0 commit comments

Comments
 (0)