Skip to content

Commit

Permalink
chore: rename safeSdk to protocolKit in onramp-kit (safe-global#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
dasanra authored Jan 19, 2024
1 parent 3edaecb commit ff6c568
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Monerium() {
const safeOwner = await provider.getSigner()
const ethAdapter = new EthersAdapter({ ethers, signerOrProvider: safeOwner })

const safeSdk = await Safe.create({
const protocolKit = await Safe.create({
ethAdapter: ethAdapter,
safeAddress: selectedSafe,
isL1SafeSingleton: true
Expand All @@ -41,7 +41,7 @@ function Monerium() {
})

await pack.init({
safeSdk
protocolKit
})

pack.subscribe(OrderState.pending, (notification) => {
Expand All @@ -66,8 +66,8 @@ function Monerium() {
}, 5000)
})

const threshold = await safeSdk.getThreshold()
const owners = await safeSdk.getOwners()
const threshold = await protocolKit.getThreshold()
const owners = await protocolKit.getOwners()

setSafeThreshold(`${threshold}/${owners.length}`)
setMoneriumPack(pack)
Expand Down
14 changes: 7 additions & 7 deletions packages/onramp-kit/src/packs/monerium/MoneriumPack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ describe('MoneriumPack', () => {
})

it('should initialize the pack', async () => {
const safeSdk = new Safe()
const protocolKit = new Safe()

await moneriumPack.init({ safeSdk })
await moneriumPack.init({ protocolKit })

expect(safeMoneriumClient.SafeMoneriumClient).toHaveBeenCalledWith(
{
clientId: 'monerium-client-id',
environment: 'sandbox',
redirectUrl: 'http://localhost:3000'
},
safeSdk
protocolKit
)
})

Expand All @@ -72,9 +72,9 @@ describe('MoneriumPack', () => {

describe('open()', () => {
beforeEach(async () => {
const safeSdk = new Safe()
const protocolKit = new Safe()

await moneriumPack.init({ safeSdk })
await moneriumPack.init({ protocolKit })
})

it('should call order socket', async () => {
Expand Down Expand Up @@ -128,9 +128,9 @@ describe('MoneriumPack', () => {
safeMoneriumClient.SafeMoneriumClient.prototype,
'revokeAccess'
)
const safeSdk = new Safe()
const protocolKit = new Safe()

await moneriumPack.init({ safeSdk })
await moneriumPack.init({ protocolKit })

await moneriumPack.close()

Expand Down
4 changes: 2 additions & 2 deletions packages/onramp-kit/src/packs/monerium/MoneriumPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class MoneriumPack extends OnRampKitBasePack {
* @throws {Error} If the Monerium client is not initialized
*/
async init(options: MoneriumInitOptions) {
if (!options?.safeSdk) {
if (!options?.protocolKit) {
throw new Error('You need to provide an instance of the protocol kit')
}

Expand All @@ -41,7 +41,7 @@ export class MoneriumPack extends OnRampKitBasePack {
clientId: this.#config.clientId,
redirectUrl: this.#config.redirectUrl
},
options.safeSdk
options.protocolKit
)
}

Expand Down
62 changes: 31 additions & 31 deletions packages/onramp-kit/src/packs/monerium/SafeMoneriumClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { hashMessage } from 'ethers'
import { PaymentStandard } from '@monerium/sdk'
import Safe, * as protocolKit from '@safe-global/protocol-kit'
import Safe, * as protocolKitPackage from '@safe-global/protocol-kit'
import { OperationType } from '@safe-global/safe-core-sdk-types'
import SafeApiKit from '@safe-global/api-kit'

Expand Down Expand Up @@ -35,21 +35,21 @@ jest.mock('@safe-global/protocol-kit')
jest.mock('@safe-global/api-kit')

describe('SafeMoneriumClient', () => {
const safeSdk = new Safe()
const protocolKit = new Safe()
let safeMoneriumClient: SafeMoneriumClient

beforeEach(() => {
jest.clearAllMocks()
safeSdk.getChainId = jest.fn().mockResolvedValue(5)
safeSdk.getEthAdapter = jest.fn().mockReturnValue({
protocolKit.getChainId = jest.fn().mockResolvedValue(5)
protocolKit.getEthAdapter = jest.fn().mockReturnValue({
call: jest.fn().mockImplementation(async () => MAGIC_VALUE),
getSignerAddress: jest.fn().mockResolvedValue('0xSignerAddress')
})

safeSdk.getEthAdapter.call = jest.fn().mockImplementation(async () => MAGIC_VALUE)
protocolKit.getEthAdapter.call = jest.fn().mockImplementation(async () => MAGIC_VALUE)
safeMoneriumClient = new SafeMoneriumClient(
{ environment: 'sandbox', clientId: 'mockClientId', redirectUrl: 'http://mockUrl' },
safeSdk
protocolKit
)
})

Expand All @@ -58,12 +58,12 @@ describe('SafeMoneriumClient', () => {
})

it('should allow to get the Safe address', async () => {
safeSdk.getAddress = jest.fn(() => Promise.resolve('0xSafeAddress'))
protocolKit.getAddress = jest.fn(() => Promise.resolve('0xSafeAddress'))
expect(await safeMoneriumClient.getSafeAddress()).toBe('0xSafeAddress')
})

it('should allow to send tokens from then Safe to any IBAN', async () => {
safeSdk.getAddress = jest.fn(() => Promise.resolve('0xSafeAddress'))
protocolKit.getAddress = jest.fn(() => Promise.resolve('0xSafeAddress'))
const placeOrderSpy = jest.spyOn(safeMoneriumClient, 'placeOrder')
//@ts-expect-error - Not all values are mocked
const signMessageSpy = jest.spyOn(safeMoneriumClient, 'signMessage').mockResolvedValueOnce({
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('SafeMoneriumClient', () => {
})

it('should throw if signing message fails', async () => {
safeSdk.getAddress = jest.fn(() => Promise.resolve('0xSafeAddress'))
protocolKit.getAddress = jest.fn(() => Promise.resolve('0xSafeAddress'))
const placeOrderSpy = jest.spyOn(safeMoneriumClient, 'placeOrder')
const signMessageSpy = jest
.spyOn(safeMoneriumClient, 'signMessage')
Expand All @@ -116,7 +116,7 @@ describe('SafeMoneriumClient', () => {

it('should allow to check if a message is NOT signed in the smart contract if the promise is fulfilled', async () => {
// Promise fullfilled without signature
safeSdk.getEthAdapter().call = jest.fn().mockImplementation(async () => '0x')
protocolKit.getEthAdapter().call = jest.fn().mockImplementation(async () => '0x')

const isMessageSigned = await safeMoneriumClient.isMessageSigned(
'0xSafeAddress',
Expand All @@ -136,7 +136,7 @@ describe('SafeMoneriumClient', () => {
}

// promise is rejected with the signature
safeSdk.getEthAdapter().call = jest
protocolKit.getEthAdapter().call = jest
.fn()
.mockImplementation(() =>
Promise.reject(new EthersError('execution reverted: "Hash not approved"', MAGIC_VALUE))
Expand All @@ -160,7 +160,7 @@ describe('SafeMoneriumClient', () => {
}

// promise is rejected without a signature
safeSdk.getEthAdapter().call = jest
protocolKit.getEthAdapter().call = jest
.fn()
.mockImplementation(() =>
Promise.reject(new EthersError('execution reverted: "Hash not approved"', '0x'))
Expand Down Expand Up @@ -219,20 +219,20 @@ describe('SafeMoneriumClient', () => {
nonce: 0
}

jest.spyOn(protocolKit, 'getSignMessageLibContract').mockResolvedValueOnce({
jest.spyOn(protocolKitPackage, 'getSignMessageLibContract').mockResolvedValueOnce({
encode: jest.fn(),
getAddress: jest.fn(),
getMessageHash: jest.fn(),
signMessage: jest.fn(),
estimateGas: jest.fn()
})

safeSdk.createTransaction = jest.fn().mockResolvedValueOnce({
protocolKit.createTransaction = jest.fn().mockResolvedValueOnce({
data: txData
})

safeSdk.getTransactionHash = jest.fn().mockResolvedValueOnce('0xTransactionHash')
safeSdk.signHash = jest.fn().mockResolvedValueOnce('0xTransactionSignature')
protocolKit.getTransactionHash = jest.fn().mockResolvedValueOnce('0xTransactionHash')
protocolKit.signHash = jest.fn().mockResolvedValueOnce('0xTransactionSignature')

jest.spyOn(SafeApiKit.prototype, 'getTransaction').mockResolvedValueOnce({
confirmationsRequired: 1,
Expand All @@ -241,7 +241,7 @@ describe('SafeMoneriumClient', () => {
})

const proposeTransactionSpy = jest.spyOn(SafeApiKit.prototype, 'proposeTransaction')
safeSdk.executeTransaction = jest.fn()
protocolKit.executeTransaction = jest.fn()
await safeMoneriumClient.signMessage('0xSafeAddress', 'message to sign')

expect(proposeTransactionSpy).toHaveBeenCalledWith(
Expand All @@ -256,36 +256,36 @@ describe('SafeMoneriumClient', () => {
})

it('should map the protocol kit chainId to the Monerium Chain types', async () => {
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(1n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(1n)
expect(await safeMoneriumClient.getChain()).toBe('ethereum')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(5n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(5n)
expect(await safeMoneriumClient.getChain()).toBe('ethereum')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(100n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(100n)
expect(await safeMoneriumClient.getChain()).toBe('gnosis')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(10200n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(10200n)
expect(await safeMoneriumClient.getChain()).toBe('gnosis')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(137n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(137n)
expect(await safeMoneriumClient.getChain()).toBe('polygon')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(80001n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(80001n)
expect(await safeMoneriumClient.getChain()).toBe('polygon')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(300n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(300n)
expect(safeMoneriumClient.getChain()).rejects.toThrowError('Chain not supported: 300')
})

it('should map the protocol kit chainId to the Monerium Network types', async () => {
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(1n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(1n)
expect(await safeMoneriumClient.getNetwork()).toBe('mainnet')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(5n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(5n)
expect(await safeMoneriumClient.getNetwork()).toBe('goerli')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(100n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(100n)
expect(await safeMoneriumClient.getNetwork()).toBe('mainnet')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(10200n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(10200n)
expect(await safeMoneriumClient.getNetwork()).toBe('chiado')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(137n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(137n)
expect(await safeMoneriumClient.getNetwork()).toBe('mainnet')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(80001n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(80001n)
expect(await safeMoneriumClient.getNetwork()).toBe('mumbai')
safeSdk.getChainId = jest.fn().mockResolvedValueOnce(300n)
protocolKit.getChainId = jest.fn().mockResolvedValueOnce(300n)
expect(safeMoneriumClient.getNetwork()).rejects.toThrowError('Network not supported: 300')
})
})
30 changes: 15 additions & 15 deletions packages/onramp-kit/src/packs/monerium/SafeMoneriumClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ import {
import { SafeMoneriumOrder } from './types'

export class SafeMoneriumClient extends MoneriumClient {
#safeSdk: Safe
#protocolKit: Safe
#ethAdapter: EthAdapter

/**
* Constructor where the Monerium environment and the Protocol kit instance are set
* @param moneriumOptions The Monerium options object
* @param safeSdk The Protocol kit instance
* @param protocolKit The Protocol kit instance
*/
constructor(moneriumOptions: ClassOptions, safeSdk: Safe) {
constructor(moneriumOptions: ClassOptions, protocolKit: Safe) {
super(moneriumOptions)

this.#safeSdk = safeSdk
this.#ethAdapter = safeSdk.getEthAdapter()
this.#protocolKit = protocolKit
this.#ethAdapter = protocolKit.getEthAdapter()
}

/**
* We get the Safe address using the Protocol kit instance
* @returns The Safe address
*/
async getSafeAddress(): Promise<string> {
return await this.#safeSdk.getAddress()
return await this.#protocolKit.getAddress()
}

/**
Expand Down Expand Up @@ -94,7 +94,7 @@ export class SafeMoneriumClient extends MoneriumClient {
* @returns A boolean indicating if the message is signed
*/
async isSignMessagePending(safeAddress: string, message: string): Promise<boolean> {
const chainId = await this.#safeSdk.getChainId()
const chainId = await this.#protocolKit.getChainId()

const apiKit = new SafeApiKit({ chainId })

Expand All @@ -120,7 +120,7 @@ export class SafeMoneriumClient extends MoneriumClient {
message: string
): Promise<SafeMultisigTransactionResponse> {
try {
const safeVersion = await this.#safeSdk.getContractVersion()
const safeVersion = await this.#protocolKit.getContractVersion()

const signMessageContract = await getSignMessageLibContract({
ethAdapter: this.#ethAdapter,
Expand All @@ -135,15 +135,15 @@ export class SafeMoneriumClient extends MoneriumClient {
data: txData,
operation: OperationType.DelegateCall
}
const safeTransaction = await this.#safeSdk.createTransaction({
const safeTransaction = await this.#protocolKit.createTransaction({
transactions: [safeTransactionData]
})

const safeTxHash = await this.#safeSdk.getTransactionHash(safeTransaction)
const safeTxHash = await this.#protocolKit.getTransactionHash(safeTransaction)

const senderSignature = await this.#safeSdk.signHash(safeTxHash)
const senderSignature = await this.#protocolKit.signHash(safeTxHash)

const chainId = await this.#safeSdk.getChainId()
const chainId = await this.#protocolKit.getChainId()

const apiKit = new SafeApiKit({ chainId })

Expand All @@ -168,15 +168,15 @@ export class SafeMoneriumClient extends MoneriumClient {
* @returns The Chain Id
*/
async getChainId(): Promise<number> {
return Number(await this.#safeSdk.getChainId())
return Number(await this.#protocolKit.getChainId())
}

/**
* Get the corresponding Monerium SDK Chain from the current chain id
* @returns The Chain
*/
async getChain(): Promise<Chain> {
const chainId = await this.#safeSdk.getChainId()
const chainId = await this.#protocolKit.getChainId()

return getMoneriumChain(Number(chainId))
}
Expand All @@ -186,7 +186,7 @@ export class SafeMoneriumClient extends MoneriumClient {
* @returns The Network
*/
async getNetwork(): Promise<Networks> {
const chainId = await this.#safeSdk.getChainId()
const chainId = await this.#protocolKit.getChainId()

return getMoneriumNetwork(Number(chainId))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/onramp-kit/src/packs/monerium/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface MoneriumProviderConfig {
}

export interface MoneriumInitOptions {
safeSdk: Safe
protocolKit: Safe
}

export interface SafeMoneriumOrder {
Expand Down

0 comments on commit ff6c568

Please sign in to comment.