Skip to content

Commit

Permalink
Fix getModulesPaginated interface
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardotc committed Apr 26, 2024
1 parent 7ed02ce commit 9ca8c6f
Show file tree
Hide file tree
Showing 144 changed files with 21,000 additions and 24 deletions.
5 changes: 3 additions & 2 deletions packages/protocol-kit/src/Safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
SafeConfigProps,
SigningMethod,
SigningMethodType,
SwapOwnerTxParams
SwapOwnerTxParams,
SafeModulesPaginated
} from './types'
import {
EthSafeSignature,
Expand Down Expand Up @@ -406,7 +407,7 @@ class Safe {
* @param pageSize - The size of the page. It will be the max length of the returning array. Must be greater then 0.
* @returns The list of addresses of all the enabled Safe modules
*/
async getModulesPaginated(start: string, pageSize: number = 10): Promise<string[]> {
async getModulesPaginated(start: string, pageSize: number = 10): Promise<SafeModulesPaginated> {
return this.#moduleManager.getModulesPaginated(start, pageSize)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class SafeContract_v1_0_0
return toTxResult(txResponse, options)
}

async getModulesPaginated(start: string, pageSize: bigint): Promise<string[]> {
async getModulesPaginated([start, pageSize]: [string, bigint]): Promise<string[]> {
if (pageSize <= 0) throw new Error('Invalid page size for fetching paginated modules')

const [array] = await this.getModules()
Expand Down
21 changes: 10 additions & 11 deletions packages/protocol-kit/src/managers/moduleManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { isRestrictedAddress, sameString } from '@safe-global/protocol-kit/utils/address'
import { SENTINEL_ADDRESS } from '@safe-global/protocol-kit/utils/constants'
import { SafeContractImplementationType } from '@safe-global/protocol-kit/types'
import {
SafeContractImplementationType,
SafeModulesPaginated
} from '@safe-global/protocol-kit/types'
import SafeProvider from '../SafeProvider'

class ModuleManager {
Expand Down Expand Up @@ -46,17 +49,13 @@ class ModuleManager {
return [...modules]
}

//TODO: Implement getModulesPaginated in the new code
async getModulesPaginated(start: string, pageSize: number): Promise<string[]> {
console.log('getModulesPaginated', start, pageSize)
return []
// if (!this.#safeContract) {
// throw new Error('Safe is not deployed')
// }

// const [modules] = await this.#safeContract.getModulesPaginated(start, pageSize)
async getModulesPaginated(start: string, pageSize: number): Promise<SafeModulesPaginated> {
if (!this.#safeContract) {
throw new Error('Safe is not deployed')
}

// return [...modules]
const [modules, next] = await this.#safeContract.getModulesPaginated([start, BigInt(pageSize)])
return { modules: modules as string[], next }
}

async isModuleEnabled(moduleAddress: string): Promise<boolean> {
Expand Down
5 changes: 5 additions & 0 deletions packages/protocol-kit/src/types/safeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ export type SafeProviderTransaction = {
maxFeePerGas?: number | string
maxPriorityFeePerGas?: number | string
}

export type SafeModulesPaginated = {
modules: string[]
next: string
}
41 changes: 31 additions & 10 deletions packages/protocol-kit/tests/e2e/moduleManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ describe('Safe modules manager', () => {
safeAddress,
contractNetworks
})
chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).length).to.be.eq(0)

const emptyModuleList = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)
const tx = await safeSdk.createEnableModuleTx(await dailyLimitModule.getAddress())
const txResponse = await safeSdk.executeTransaction(tx)
await waitSafeTxReceipt(txResponse)
chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).length).to.be.eq(1)
const moduleList = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)

chai.expect(emptyModuleList.modules.length).to.be.eq(0)
chai.expect(emptyModuleList.next).to.be.eq(SENTINEL_ADDRESS)
chai.expect(moduleList.modules.length).to.be.eq(1)
chai.expect(emptyModuleList.next).to.be.eq(SENTINEL_ADDRESS)
})

it('should constraint returned modules by pageSize', async () => {
Expand All @@ -127,17 +133,28 @@ describe('Safe modules manager', () => {
contractNetworks
})

chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).length).to.be.eq(0)
chai
.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).modules.length)
.to.be.eq(0)
const txDailyLimits = await safeSdk.createEnableModuleTx(dailyLimitsAddress)
const dailyLimitsResponse = await safeSdk.executeTransaction(txDailyLimits)
await waitSafeTxReceipt(dailyLimitsResponse)
const txSocialRecovery = await safeSdk.createEnableModuleTx(socialRecoveryAddress)
const soecialRecoveryResponse = await safeSdk.executeTransaction(txSocialRecovery)
await waitSafeTxReceipt(soecialRecoveryResponse)

chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).length).to.be.eq(2)
chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 1)).length).to.be.eq(1)
chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 1)).length).to.be.eq(1)
const modules1 = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)
const modules2 = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 1)
const modules3 = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 2)

chai.expect(modules1.modules.length).to.be.eq(2)
chai.expect(modules1.next).to.be.eq(SENTINEL_ADDRESS)

chai.expect(modules2.modules.length).to.be.eq(1)
chai.expect(modules2.next).to.be.eq(socialRecoveryAddress)

chai.expect(modules3.modules.length).to.be.eq(2)
chai.expect(modules3.next).to.be.eq(SENTINEL_ADDRESS)
})

it('should offset the returned modules', async () => {
Expand All @@ -159,11 +176,15 @@ describe('Safe modules manager', () => {
const soecialRecoveryResponse = await safeSdk.executeTransaction(txSocialRecovery)
await waitSafeTxReceipt(soecialRecoveryResponse)

const [firstModule, secondModule] = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)
const {
modules: [firstModule, secondModule]
} = await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)

chai.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).length).to.be.eq(2)
chai.expect((await safeSdk.getModulesPaginated(firstModule, 10)).length).to.be.eq(1)
chai.expect((await safeSdk.getModulesPaginated(secondModule, 10)).length).to.be.eq(0)
chai
.expect((await safeSdk.getModulesPaginated(SENTINEL_ADDRESS, 10)).modules.length)
.to.be.eq(2)
chai.expect((await safeSdk.getModulesPaginated(firstModule, 10)).modules.length).to.be.eq(1)
chai.expect((await safeSdk.getModulesPaginated(secondModule, 10)).modules.length).to.be.eq(0)
})

it('should fail if pageSize is invalid', async () => {
Expand Down
Loading

0 comments on commit 9ca8c6f

Please sign in to comment.