Skip to content

Commit

Permalink
feat(sdk-starter-kit): Improvements (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv authored Sep 16, 2024
1 parent 0d03508 commit cc14332
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
12 changes: 5 additions & 7 deletions packages/sdk-starter-kit/src/SafeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,16 @@ export class SafeClient extends BaseClient {
* @param extendFunc
* @returns
*/
extend<T>(extendFunc: (client: SafeClient) => Promise<T>): Promise<SafeClient & T>
extend<T>(extendFunc: (client: SafeClient) => T): SafeClient & T
extend<T>(extendFunc: (client: this) => Promise<T>): Promise<this & T>
extend<T>(extendFunc: (client: this) => T): this & T

extend<T>(
extendFunc: (client: SafeClient) => T | Promise<T>
): (SafeClient & T) | Promise<SafeClient & T> {
extend<T>(extendFunc: (client: this) => T | Promise<T>): (this & T) | Promise<this & T> {
const result = extendFunc(this)

if (result instanceof Promise) {
return result.then((extensions) => Object.assign(this, extensions) as SafeClient & T)
return result.then((extensions) => Object.assign(this, extensions) as this & T)
} else {
return Object.assign(this, result) as SafeClient & T
return Object.assign(this, result) as this & T
}
}

Expand Down
40 changes: 40 additions & 0 deletions packages/sdk-starter-kit/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createSafeClient, offChainMessages, onChainMessages } from './index'

const RPC_URL = 'https://ethereum-sepolia-rpc.publicnode.com'
const SAFE_ADDRESS = '0x60C4Ab82D06Fd7dFE9517e17736C2Dcc77443EF0'
const SAFE_OWNERS = [
'0x9cCBDE03eDd71074ea9c49e413FA9CDfF16D263B',
'0x56e2C102c664De6DfD7315d12c0178b61D16F171'
]

describe('createSafeClient', () => {
it('should create a Safe client instance', async () => {
const safeClient = await createSafeClient({
provider: RPC_URL,
safeAddress: SAFE_ADDRESS
})

const safeAddress = await safeClient.getAddress()
const owners = await safeClient.getOwners()
const threshold = await safeClient.getThreshold()

expect(safeAddress).toBe('0x60C4Ab82D06Fd7dFE9517e17736C2Dcc77443EF0')
expect(owners).toStrictEqual(SAFE_OWNERS)
expect(threshold).toBe(1)
})

it('should allow to extend the client several times and accumulating methods', async () => {
const safeClient1 = await createSafeClient({
provider: RPC_URL,
safeAddress: SAFE_ADDRESS
})

const safeClient2 = safeClient1.extend(offChainMessages())
const safeClient3 = safeClient2.extend(onChainMessages())

expect(safeClient3).toBeDefined()
expect(safeClient3.send).toBeDefined()
expect(safeClient3.sendOnChainMessage).toBeDefined()
expect(safeClient3.sendOffChainMessage).toBeDefined()
})
})
2 changes: 1 addition & 1 deletion packages/sdk-starter-kit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type PredictedSafeConfig = {

export type SdkStarterKitRootConfig = {
provider: SafeProvider['provider']
signer: SafeProvider['signer']
signer?: SafeProvider['signer']
}

export type SdkStarterKitConfig = SdkStarterKitRootConfig &
Expand Down

0 comments on commit cc14332

Please sign in to comment.