Skip to content

Commit

Permalink
List actions (#81)
Browse files Browse the repository at this point in the history
* [list requests] add requests

* [list actions] move list actions to sdk

* [list actions] Update readme

* [list actions] Update readme
  • Loading branch information
mike-diamond authored Mar 20, 2024
1 parent c19bf14 commit c8cd54b
Show file tree
Hide file tree
Showing 15 changed files with 477 additions and 95 deletions.
283 changes: 188 additions & 95 deletions README.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/methods/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import getExitQueuePositions from './requests/getExitQueuePositions'
import { deposit } from './transactions/deposit'
import { withdraw } from './transactions/withdraw'
import { claimExitQueue } from './transactions/claimExitQueue'
import { updateWhitelist } from './transactions/updateWhitelist'
import { updateBlocklist } from './transactions/updateBlocklist'


export default {
Expand All @@ -37,5 +39,7 @@ export default {
deposit,
withdraw,
claimExitQueue,
updateWhitelist,
updateBlocklist,
},
} as const
43 changes: 43 additions & 0 deletions src/methods/vault/transactions/updateBlocklist/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { UpdateBlocklistInput } from './types'
import { validateArgs } from '../../../../utils'
import { vaultMulticall } from '../../../../contracts'


const validateList = (blocklist: UpdateBlocklistInput['blocklist']) => {
const isValid = blocklist.every((blocklistItem) => (
blocklistItem
&& typeof blocklistItem === 'object'
&& typeof blocklistItem.address === 'string'
&& typeof blocklistItem.isNew === 'boolean'
))

if (!isValid) {
throw new Error('The "blocklist" argument must be an array of objects with "address" and "isNew" properties')
}
}

export const commonLogic = (values: UpdateBlocklistInput) => {
const { options, contracts, userAddress, vaultAddress, blocklist } = values

validateArgs.array({ blocklist })
validateArgs.address({ vaultAddress, userAddress })
validateList(blocklist)

const multicallCommonArgs: Omit<Parameters<typeof vaultMulticall>[0], 'request'> = {
vaultContract: contracts.helpers.createBlocklistVault(vaultAddress),
keeperContract: contracts.base.keeper,
vaultAddress,
userAddress,
options,
}

const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = blocklist.map(({ address, isNew }) => ({
method: 'updateBlocklist',
args: [ address, isNew ],
}))

return {
params,
multicallCommonArgs,
}
}
1 change: 1 addition & 0 deletions src/methods/vault/transactions/updateBlocklist/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as updateBlocklist } from './updateBlocklist'
21 changes: 21 additions & 0 deletions src/methods/vault/transactions/updateBlocklist/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import updateBlocklistGas from './updateBlocklistGas'
import updateBlocklistEncode from './updateBlocklistEncode'


export type UpdateBlocklistInput = {
blocklist: Array<{
address: string
isNew: boolean
}>
userAddress: string
vaultAddress: string
options: StakeWise.Options
provider: StakeWise.Provider
contracts: StakeWise.Contracts
}

export interface UpdateBlocklist {
(values: UpdateBlocklistInput): Promise<StakeWise.TransactionHash>
estimateGas: typeof updateBlocklistGas
encode: typeof updateBlocklistEncode
}
25 changes: 25 additions & 0 deletions src/methods/vault/transactions/updateBlocklist/updateBlocklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UpdateBlocklist } from './types'
import { commonLogic } from './common'
import updateBlocklistGas from './updateBlocklistGas'
import updateBlocklistEncode from './updateBlocklistEncode'
import { vaultMulticall } from '../../../../contracts'


const updateBlocklist: UpdateBlocklist = async (values) => {
const { params, multicallCommonArgs } = commonLogic(values)

const result = await vaultMulticall<{ hash: string }>({
...multicallCommonArgs,
request: {
params,
},
})

return result.hash
}

updateBlocklist.encode = updateBlocklistEncode
updateBlocklist.estimateGas = updateBlocklistGas


export default updateBlocklist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { commonLogic } from './common'
import { UpdateBlocklistInput } from './types'
import { vaultMulticall } from '../../../../contracts'


const updateBlocklistEncode = async (values: UpdateBlocklistInput): Promise<StakeWise.TransactionData> => {
const { params, multicallCommonArgs } = commonLogic(values)

const rx = await vaultMulticall<{ data: string, to: string }>({
...multicallCommonArgs,
request: {
params,
transactionData: true,
},
})

return {
data: rx.data,
to: rx.to,
}
}


export default updateBlocklistEncode
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { commonLogic } from './common'
import { UpdateBlocklistInput } from './types'
import { getGas } from '../../../../utils'
import { vaultMulticall } from '../../../../contracts'


const updateBlocklistGas = async (values: UpdateBlocklistInput) => {
const { params, multicallCommonArgs } = commonLogic(values)

const estimatedGas = await vaultMulticall<bigint>({
...multicallCommonArgs,
request: {
estimateGas: true,
params,
},
})

const gas = await getGas({ estimatedGas, provider: values.provider })

return gas
}


export default updateBlocklistGas
43 changes: 43 additions & 0 deletions src/methods/vault/transactions/updateWhitelist/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { UpdateWhitelistInput } from './types'
import { validateArgs } from '../../../../utils'
import { vaultMulticall } from '../../../../contracts'


const validateList = (whitelist: UpdateWhitelistInput['whitelist']) => {
const isValid = whitelist.every((whitelistItem) => (
whitelistItem
&& typeof whitelistItem === 'object'
&& typeof whitelistItem.address === 'string'
&& typeof whitelistItem.isNew === 'boolean'
))

if (!isValid) {
throw new Error('The "whitelist" argument must be an array of objects with "address" and "isNew" properties')
}
}

export const commonLogic = (values: UpdateWhitelistInput) => {
const { options, contracts, userAddress, vaultAddress, whitelist } = values

validateArgs.array({ whitelist })
validateArgs.address({ vaultAddress, userAddress })
validateList(whitelist)

const multicallCommonArgs: Omit<Parameters<typeof vaultMulticall>[0], 'request'> = {
vaultContract: contracts.helpers.createPrivateVault(vaultAddress),
keeperContract: contracts.base.keeper,
vaultAddress,
userAddress,
options,
}

const params: Parameters<typeof vaultMulticall>[0]['request']['params'] = whitelist.map(({ address, isNew }) => ({
method: 'updateWhitelist',
args: [ address, isNew ],
}))

return {
params,
multicallCommonArgs,
}
}
1 change: 1 addition & 0 deletions src/methods/vault/transactions/updateWhitelist/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as updateWhitelist } from './updateWhitelist'
21 changes: 21 additions & 0 deletions src/methods/vault/transactions/updateWhitelist/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import updateWhitelistGas from './updateWhitelistGas'
import updateWhitelistEncode from './updateWhitelistEncode'


export type UpdateWhitelistInput = {
whitelist: Array<{
address: string
isNew: boolean
}>
userAddress: string
vaultAddress: string
options: StakeWise.Options
provider: StakeWise.Provider
contracts: StakeWise.Contracts
}

export interface UpdateWhitelist {
(values: UpdateWhitelistInput): Promise<StakeWise.TransactionHash>
estimateGas: typeof updateWhitelistGas
encode: typeof updateWhitelistEncode
}
25 changes: 25 additions & 0 deletions src/methods/vault/transactions/updateWhitelist/updateWhitelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UpdateWhitelist } from './types'
import { commonLogic } from './common'
import updateWhitelistGas from './updateWhitelistGas'
import updateWhitelistEncode from './updateWhitelistEncode'
import { vaultMulticall } from '../../../../contracts'


const updateWhitelist: UpdateWhitelist = async (values) => {
const { params, multicallCommonArgs } = commonLogic(values)

const result = await vaultMulticall<{ hash: string }>({
...multicallCommonArgs,
request: {
params,
},
})

return result.hash
}

updateWhitelist.encode = updateWhitelistEncode
updateWhitelist.estimateGas = updateWhitelistGas


export default updateWhitelist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { commonLogic } from './common'
import { UpdateWhitelistInput } from './types'
import { vaultMulticall } from '../../../../contracts'


const updateWhitelistEncode = async (values: UpdateWhitelistInput): Promise<StakeWise.TransactionData> => {
const { params, multicallCommonArgs } = commonLogic(values)

const rx = await vaultMulticall<{ data: string, to: string }>({
...multicallCommonArgs,
request: {
params,
transactionData: true,
},
})

return {
data: rx.data,
to: rx.to,
}
}


export default updateWhitelistEncode
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { commonLogic } from './common'
import { UpdateWhitelistInput } from './types'
import { getGas } from '../../../../utils'
import { vaultMulticall } from '../../../../contracts'


const updateWhitelistGas = async (values: UpdateWhitelistInput) => {
const { params, multicallCommonArgs } = commonLogic(values)

const estimatedGas = await vaultMulticall<bigint>({
...multicallCommonArgs,
request: {
estimateGas: true,
params,
},
})

const gas = await getGas({ estimatedGas, provider: values.provider })

return gas
}


export default updateWhitelistGas
9 changes: 9 additions & 0 deletions src/utils/validateArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ const number = (values: Record<string, number>) => {
})
}

const array = (values: Record<string, any[]>) => {
Object.keys(values).forEach((key) => {
if (!Array.isArray(values[key])) {
throw new Error(`The "${key}" argument must be an array`)
}
})
}


export default {
array,
bigint,
string,
number,
Expand Down

0 comments on commit c8cd54b

Please sign in to comment.