Skip to content

Commit

Permalink
gnosis
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding committed Feb 16, 2024
1 parent f0392c5 commit 4af8425
Show file tree
Hide file tree
Showing 4 changed files with 431 additions and 33 deletions.
4 changes: 4 additions & 0 deletions packages/devtools-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"test": "jest --ci"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk": "^2.0.0",
"@gnosis.pm/safe-core-sdk-types": "^1.0.0",
"@gnosis.pm/safe-ethers-lib": "^1.7.0",
"@gnosis.pm/safe-service-client": "1.1.1",
"p-memoize": "~4.0.4"
},
"devDependencies": {
Expand Down
78 changes: 78 additions & 0 deletions packages/devtools-evm/src/signer/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { TransactionReceipt, TransactionRequest } from '@ethersproject/abstract-provider'
import type { Signer } from '@ethersproject/abstract-signer'
import Safe, { SafeConfig } from '@gnosis.pm/safe-core-sdk'
import { SafeTransactionDataPartial } from '@gnosis.pm/safe-core-sdk-types'
import EthersAdapter from '@gnosis.pm/safe-ethers-lib'
import SafeServiceClient from '@gnosis.pm/safe-service-client'
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import {
formatEid,
Expand All @@ -10,6 +14,8 @@ import {
} from '@layerzerolabs/devtools'
import assert from 'assert'

import * as ethers from 'ethers'

/**
* Implements an OmniSigner interface for EVM-compatible chains
*/
Expand Down Expand Up @@ -75,3 +81,75 @@ export class OmniSignerEVM implements OmniSigner {
}
}
}

/**
* Implements an OmniSigner interface for EVM-compatible chains using Gnosis Safe.
*
* TODO: upgrade to @safeglobal dependencies once codebase utilizes Ethers v6
*/
export class GnosisOmniSignerEVM implements OmniSigner {
protected safeSdk: Safe | undefined
protected safeService: SafeServiceClient | undefined

constructor(
public readonly eid: EndpointId,
public readonly signer: Signer,
protected readonly safeUrl: string,
protected readonly safeConfig: SafeConfig
) {}

async sign(_transaction: OmniTransaction): Promise<string> {
throw new Error('Method not implemented.')
}

async signAndSend(transaction: OmniTransaction): Promise<OmniTransactionResponse> {
const { safeSdk, safeService } = await this.#initSafe()
const safeTransaction = await safeSdk.createTransaction([this.#serializeTransaction(transaction)])
const safeTxHash = await safeSdk.getTransactionHash(safeTransaction)
const safeAddress = safeSdk.getAddress()
const senderAddress = await this.signer.getAddress()
await safeService.proposeTransaction({
safeAddress,
safeTransaction,
safeTxHash,
senderAddress,
})
return {
transactionHash: safeTxHash,
wait: async (_confirmations?: number) => {
return {
transactionHash: safeTxHash,
}
},
}
}

#serializeTransaction(transaction: OmniTransaction): SafeTransactionDataPartial {
return {
to: transaction.point.address,
data: transaction.data,
value: '0',
}
}

async #initSafe() {
if (this.safeConfig && !this.safeSdk) {
const ethAdapter = new EthersAdapter({
ethers,
signerOrProvider: this.signer,
})
this.safeService = new SafeServiceClient(this.safeUrl)

const contractNetworks = this.safeConfig.contractNetworks
this.safeSdk = await Safe.create({
ethAdapter,
safeAddress: this.safeConfig.safeAddress,
...(!!contractNetworks && { contractNetworks }),
})
}
if (!this.safeSdk) {
throw new Error('Safe SDK not initialized')
}
return { safeSdk: this.safeSdk, safeService: this.safeService! }
}
}
2 changes: 1 addition & 1 deletion packages/devtools-evm/test/signer/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { endpointArbitrary, pointArbitrary } from '@layerzerolabs/test-devtools'
import { Signer } from '@ethersproject/abstract-signer'
import { OmniSignerEVM } from '@/signer'

describe('signer/sdk', () => {
describe('signer/ethers', () => {
const transactionHashArbitrary = fc.hexaString()
const signedTransactionArbitrary = fc.hexaString()
const transactionArbitrary = fc.record({
Expand Down
Loading

0 comments on commit 4af8425

Please sign in to comment.