Skip to content

Commit

Permalink
safe-global
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Goulding <[email protected]>
  • Loading branch information
ryandgoulding committed Feb 20, 2024
1 parent 3da2f1c commit 94f2935
Show file tree
Hide file tree
Showing 5 changed files with 1,275 additions and 172 deletions.
2 changes: 1 addition & 1 deletion packages/devtools-evm-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"test": "jest --ci --forceExit"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk": "3.2.0",
"@layerzerolabs/export-deployments": "~0.0.1",
"@safe-global/protocol-kit": "^1.3.0",
"micro-memoize": "~4.1.2",
"p-memoize": "~4.0.4",
"zod": "^3.22.4"
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools-evm-hardhat/src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'hardhat/types/config'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { ConnectSafeConfig } from '@gnosis.pm/safe-core-sdk'
import { ConnectSafeConfigWithSafeAddress } from '@safe-global/protocol-kit'

declare module 'hardhat/types/config' {
interface HardhatNetworkUserConfig {
Expand Down Expand Up @@ -48,7 +48,7 @@ declare module 'hardhat/types/config' {
*/
safeConfig?: SafeConfig
}
interface SafeConfig extends ConnectSafeConfig {
interface SafeConfig extends ConnectSafeConfigWithSafeAddress {
safeUrl: string
safeAddress: string // override to make ConnectSafeConfig.safeAddress mandatory
}
Expand Down
7 changes: 3 additions & 4 deletions packages/devtools-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@
"test": "jest --ci"
},
"dependencies": {
"@gnosis.pm/safe-core-sdk": "^3.2.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",
"@safe-global/api-kit": "^1.3.0",
"@safe-global/protocol-kit": "^1.3.0",
"ethers": "5.7.2",
"p-memoize": "~4.0.4"
},
Expand All @@ -56,6 +54,7 @@
"@layerzerolabs/io-devtools": "~0.1.3",
"@layerzerolabs/lz-definitions": "~2.1.7",
"@layerzerolabs/test-devtools": "~0.1.6",
"@safe-global/safe-core-sdk-types": "^2.3.0",
"@swc/core": "^1.4.0",
"@swc/jest": "^0.2.36",
"@types/jest": "^29.5.12",
Expand Down
32 changes: 17 additions & 15 deletions packages/devtools-evm/src/signer/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { TransactionReceipt, TransactionRequest } from '@ethersproject/abstract-provider'
import type { Signer } from '@ethersproject/abstract-signer'
import Safe, { ConnectSafeConfig } 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 Safe, { ConnectSafeConfig, EthersAdapter } from '@safe-global/protocol-kit'
import SafeApiKit from '@safe-global/api-kit'
import { MetaTransactionData, OperationType } from '@safe-global/safe-core-sdk-types'
import type { EndpointId } from '@layerzerolabs/lz-definitions'
import {
formatEid,
Expand Down Expand Up @@ -82,7 +81,7 @@ export class GnosisOmniSignerEVM<TSafeConfig extends ConnectSafeConfig> extends
// devtools only supports Ethers v5, and @safeglobal only supports Ethers v6.

protected safeSdk: Safe | undefined
protected safeService: SafeServiceClient | undefined
protected apiKit: SafeApiKit | undefined

constructor(
eid: EndpointId,
Expand All @@ -99,16 +98,18 @@ export class GnosisOmniSignerEVM<TSafeConfig extends ConnectSafeConfig> extends

async signAndSend(transaction: OmniTransaction): Promise<OmniTransactionResponse> {
this.assertTransaction(transaction)
const { safeSdk, safeService } = await this.#initSafe()
const { safeSdk, apiKit } = await this.#initSafe()
const safeTransaction = await safeSdk.createTransaction({
safeTransactionData: this.#serializeTransaction(transaction),
safeTransactionData: [this.#serializeTransaction(transaction)],
})
const safeTxHash = await safeSdk.getTransactionHash(safeTransaction)
const safeAddress = safeSdk.getAddress()
const senderSignature = await safeSdk.signTransactionHash(safeTxHash)
const safeAddress = await safeSdk.getAddress()
const senderAddress = await this.signer.getAddress()
await safeService.proposeTransaction({
await apiKit.proposeTransaction({
senderSignature: senderSignature.data,
safeAddress,
safeTransaction,
safeTransactionData: safeTransaction.data,
safeTxHash,
senderAddress,
})
Expand All @@ -122,21 +123,22 @@ export class GnosisOmniSignerEVM<TSafeConfig extends ConnectSafeConfig> extends
}
}

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

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

const contractNetworks = this.safeConfig.contractNetworks
this.safeSdk = await Safe.create({
Expand All @@ -145,9 +147,9 @@ export class GnosisOmniSignerEVM<TSafeConfig extends ConnectSafeConfig> extends
...(!!contractNetworks && { contractNetworks }),
})
}
if (!this.safeSdk || !this.safeService) {
if (!this.safeSdk || !this.apiKit) {
throw new Error('Safe SDK not initialized')
}
return { safeSdk: this.safeSdk, safeService: this.safeService }
return { safeSdk: this.safeSdk, apiKit: this.apiKit }
}
}
Loading

0 comments on commit 94f2935

Please sign in to comment.