Skip to content

Commit

Permalink
refactor(auth-kit): Reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv authored and dasanra committed Oct 25, 2023
1 parent 214662b commit 50c696d
Show file tree
Hide file tree
Showing 32 changed files with 289 additions and 2,441 deletions.
12 changes: 10 additions & 2 deletions guides/integrating-the-safe-core-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ As stated in the introduction, the [Safe API Kit](https://github.com/safe-global
```js
import SafeApiKit from '@safe-global/api-kit'

const txServiceUrl = 'https://safe-transaction-mainnet.safe.global'
const safeService = new SafeApiKit({ txServiceUrl, ethAdapter })
const safeService = new SafeApiKit({ chainId })
```

Using the `chainId` is enough for chains where Safe runs a Transaction Service. For those chains where Safe doesn't run a service, use the `txServiceUrl` parameter to set the custom service endpoint.

```js
const safeService = new SafeApiKit({
chainId,
txServiceUrl: 'https://txServiceUrl.com'
})
```

### Initialize the Protocol Kit
Expand Down
16 changes: 3 additions & 13 deletions packages/api-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,15 @@ npm run test

## <a name="initialization">Initialization</a>

### Instantiate an EthAdapter

First of all, we need to create an `EthAdapter`, which contains all the required utilities for the SDKs to interact with the blockchain. It acts as a wrapper for [web3.js](https://web3js.readthedocs.io/) or [ethers.js](https://docs.ethers.io/v5/) Ethereum libraries.

Depending on the library used by the Dapp, there are two options:

- [Create an `EthersAdapter` instance](https://github.com/safe-global/safe-core-sdk/tree/main/packages/protocol-kit/src/adapters/ethers)
- [Create a `Web3Adapter` instance](https://github.com/safe-global/safe-core-sdk/tree/main/packages/protocol-kit/src/adapters/web3)

Once the instance of `EthersAdapter` or `Web3Adapter` is created, it can be used in the SDK initialization.

### Initialize the SafeApiKit

```js
import SafeApiKit from '@safe-global/api-kit'

const safeService = new SafeApiKit({
txServiceUrl: 'https://safe-transaction-mainnet.safe.global',
ethAdapter
chainId: 1,
// Optional. txServiceUrl must be used to set a custom service. For example on chains where Safe doesn't run services.
txServiceUrl: 'https://safe-transaction-mainnet.safe.global'
})
```

Expand Down
2 changes: 1 addition & 1 deletion packages/api-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@safe-global/protocol-kit": "^1.3.0",
"@types/chai": "^4.3.5",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.1",
Expand All @@ -56,6 +55,7 @@
},
"dependencies": {
"@ethersproject/abstract-signer": "^5.7.0",
"@safe-global/protocol-kit": "^1.3.0",
"@safe-global/safe-core-sdk-types": "^2.3.0",
"node-fetch": "^2.6.6"
}
Expand Down
79 changes: 48 additions & 31 deletions packages/api-kit/src/SafeApiKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,45 @@ import {
TokenInfoResponse,
TransferListResponse
} from '@safe-global/api-kit/types/safeTransactionServiceTypes'
import { getTxServiceBaseUrl } from '@safe-global/api-kit/utils'
import { HttpMethod, sendRequest } from '@safe-global/api-kit/utils/httpRequests'
import { validateEip3770Address } from '@safe-global/protocol-kit'
import {
EthAdapter,
Eip3770Address,
SafeMultisigConfirmationListResponse,
SafeMultisigTransactionResponse
} from '@safe-global/safe-core-sdk-types'
import { TRANSACTION_SERVICE_URLS } from './utils/config'

export interface SafeApiKitConfig {
/** chainId - The chainId */
chainId: number
/** txServiceUrl - Safe Transaction Service URL */
txServiceUrl: string
/** ethAdapter - Ethereum adapter */
ethAdapter: EthAdapter
txServiceUrl?: string
}

class SafeApiKit {
#chainId: number
#txServiceBaseUrl: string
#ethAdapter: EthAdapter

constructor({ txServiceUrl, ethAdapter }: SafeApiKitConfig) {
this.#txServiceBaseUrl = getTxServiceBaseUrl(txServiceUrl)
this.#ethAdapter = ethAdapter
constructor({ chainId, txServiceUrl }: SafeApiKitConfig) {
this.#chainId = chainId

if (txServiceUrl) {
this.#txServiceBaseUrl = txServiceUrl
} else {
const url = TRANSACTION_SERVICE_URLS[chainId]
if (!url) {
throw new TypeError(
`There is no transaction service available for chainId ${chainId}. Please set the txServiceUrl property to use a custom transaction service.`
)
}

this.#txServiceBaseUrl = `${url}/api`
}
}

#getEip3770Address(fullAddress: string): Eip3770Address {
return validateEip3770Address(fullAddress, this.#chainId)
}

/**
Expand Down Expand Up @@ -102,7 +119,7 @@ class SafeApiKit {
if (ownerAddress === '') {
throw new Error('Invalid owner address')
}
const { address } = await this.#ethAdapter.getEip3770Address(ownerAddress)
const { address } = this.#getEip3770Address(ownerAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/owners/${address}/safes/`,
method: HttpMethod.Get
Expand All @@ -121,7 +138,7 @@ class SafeApiKit {
if (moduleAddress === '') {
throw new Error('Invalid module address')
}
const { address } = await this.#ethAdapter.getEip3770Address(moduleAddress)
const { address } = this.#getEip3770Address(moduleAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/modules/${address}/safes/`,
method: HttpMethod.Get
Expand Down Expand Up @@ -204,7 +221,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/`,
method: HttpMethod.Get
Expand All @@ -229,15 +246,15 @@ class SafeApiKit {
const url = new URL(`${this.#txServiceBaseUrl}/v1/delegates`)

if (safeAddress) {
const { address: safe } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address: safe } = this.#getEip3770Address(safeAddress)
url.searchParams.set('safe', safe)
}
if (delegateAddress) {
const { address: delegate } = await this.#ethAdapter.getEip3770Address(delegateAddress)
const { address: delegate } = this.#getEip3770Address(delegateAddress)
url.searchParams.set('delegate', delegate)
}
if (delegatorAddress) {
const { address: delegator } = await this.#ethAdapter.getEip3770Address(delegatorAddress)
const { address: delegator } = this.#getEip3770Address(delegatorAddress)
url.searchParams.set('delegator', delegator)
}
if (label) {
Expand Down Expand Up @@ -284,13 +301,13 @@ class SafeApiKit {
if (label === '') {
throw new Error('Invalid label')
}
const { address: delegate } = await this.#ethAdapter.getEip3770Address(delegateAddress)
const { address: delegator } = await this.#ethAdapter.getEip3770Address(delegatorAddress)
const { address: delegate } = this.#getEip3770Address(delegateAddress)
const { address: delegator } = this.#getEip3770Address(delegatorAddress)
const totp = Math.floor(Date.now() / 1000 / 3600)
const data = delegate + totp
const signature = await signer.signMessage(data)
const body: any = {
safe: safeAddress ? (await this.#ethAdapter.getEip3770Address(safeAddress)).address : null,
safe: safeAddress ? this.#getEip3770Address(safeAddress).address : null,
delegate,
delegator,
label,
Expand Down Expand Up @@ -325,8 +342,8 @@ class SafeApiKit {
if (delegatorAddress === '') {
throw new Error('Invalid Safe delegator address')
}
const { address: delegate } = await this.#ethAdapter.getEip3770Address(delegateAddress)
const { address: delegator } = await this.#ethAdapter.getEip3770Address(delegatorAddress)
const { address: delegate } = this.#getEip3770Address(delegateAddress)
const { address: delegator } = this.#getEip3770Address(delegatorAddress)
const totp = Math.floor(Date.now() / 1000 / 3600)
const data = delegate + totp
const signature = await signer.signMessage(data)
Expand Down Expand Up @@ -355,7 +372,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/creation/`,
method: HttpMethod.Get
Expand All @@ -380,7 +397,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/estimations/`,
method: HttpMethod.Post,
Expand Down Expand Up @@ -409,8 +426,8 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address: safe } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address: sender } = await this.#ethAdapter.getEip3770Address(senderAddress)
const { address: safe } = this.#getEip3770Address(safeAddress)
const { address: sender } = this.#getEip3770Address(senderAddress)
if (safeTxHash === '') {
throw new Error('Invalid safeTxHash')
}
Expand Down Expand Up @@ -439,7 +456,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/incoming-transfers?executed=true`,
method: HttpMethod.Get
Expand All @@ -459,7 +476,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/module-transactions/`,
method: HttpMethod.Get
Expand All @@ -478,7 +495,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/safes/${address}/multisig-transactions/`,
method: HttpMethod.Get
Expand All @@ -502,7 +519,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
const nonce = currentNonce ? currentNonce : (await this.getSafeInfo(address)).nonce
return sendRequest({
url: `${
Expand All @@ -527,7 +544,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
const url = new URL(`${this.#txServiceBaseUrl}/v1/safes/${address}/all-transactions/`)

const trusted = options?.trusted?.toString() || 'true'
Expand Down Expand Up @@ -558,7 +575,7 @@ class SafeApiKit {
if (safeAddress === '') {
throw new Error('Invalid Safe address')
}
const { address } = await this.#ethAdapter.getEip3770Address(safeAddress)
const { address } = this.#getEip3770Address(safeAddress)
const pendingTransactions = await this.getPendingTransactions(address)
if (pendingTransactions.results.length > 0) {
const nonces = pendingTransactions.results.map((tx) => tx.nonce)
Expand Down Expand Up @@ -593,7 +610,7 @@ class SafeApiKit {
if (tokenAddress === '') {
throw new Error('Invalid token address')
}
const { address } = await this.#ethAdapter.getEip3770Address(tokenAddress)
const { address } = this.#getEip3770Address(tokenAddress)
return sendRequest({
url: `${this.#txServiceBaseUrl}/v1/tokens/${address}/`,
method: HttpMethod.Get
Expand Down
14 changes: 14 additions & 0 deletions packages/api-kit/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const TRANSACTION_SERVICE_URLS: Record<number, string> = {
1: 'https://safe-transaction-mainnet.safe.global',
5: 'https://safe-transaction-goerli.safe.global',
10: 'https://safe-transaction-optimism.safe.global',
56: 'https://safe-transaction-bsc.safe.global',
100: 'https://safe-transaction-gnosis-chain.safe.global',
137: 'https://safe-transaction-polygon.safe.global',
8453: 'https://safe-transaction-base.safe.global',
42161: 'https://safe-transaction-arbitrum.safe.global',
42220: 'https://safe-transaction-celo.safe.global',
43114: 'https://safe-transaction-avalanche.safe.global',
84531: 'https://safe-transaction-base-testnet.safe.global',
1313161554: 'https://safe-transaction-aurora.safe.global'
}
3 changes: 0 additions & 3 deletions packages/api-kit/src/utils/index.ts

This file was deleted.

Loading

0 comments on commit 50c696d

Please sign in to comment.