Skip to content

Commit

Permalink
Add data decoder/master copy endpoint calls (#42)
Browse files Browse the repository at this point in the history
* build: Add get master copies + decoded data

Co-authored-by: Aaron Cook <[email protected]>

* chore: Clean-up types

* fix: Remove unnecessary test

* fix: Broken tests + bump version

* fix: Remove frontend types from response

* build: Add e2e tests

* fix: Remove lint

Co-authored-by: Diogo Soares <[email protected]>
Co-authored-by: Aaron Cook <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2021
1 parent c84f332 commit a9c2051
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 471 deletions.
28 changes: 28 additions & 0 deletions e2e/get-decoded-data.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getDecodedData } from '../src'
import config from './config'

describe('getDecodedData tests', () => {
it('should post getDecodedData', async () => {
const result = await getDecodedData(
config.baseUrl,
'4',
'0x095ea7b3000000000000000000000000ae9844f89d98c150f5e61bfc676d68b4921559900000000000000000000000000000000000000000000000000001c6bf52634000',
)

expect(result).toStrictEqual({
method: 'approve',
parameters: [
{
name: 'spender',
type: 'address',
value: '0xae9844F89D98c150F5e61bfC676D68b492155990',
},
{
name: 'value',
type: 'uint256',
value: '500000000000000',
},
],
})
})
})
15 changes: 15 additions & 0 deletions e2e/get-master-copy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getMasterCopies } from '../src'
import config from './config'

describe('get mastercopy tests', () => {
it('should get master copy response', async () => {
const masterCopies = await getMasterCopies(config.baseUrl, '4')

expect(Array.isArray(masterCopies)).toBe(true)

// version 1.1.1 should be present
const lastVersionRinkeby = masterCopies.find((mastercopy) => mastercopy.version === '1.1.1')
expect(lastVersionRinkeby).toBeDefined()
expect(lastVersionRinkeby.address).toBe('0x34CfAC646f301356fAa8B21e94227e3583Fe3F5F')
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gnosis.pm/safe-react-gateway-sdk",
"version": "2.6.0",
"version": "2.7.0",
"main": "dist/index.min.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
25 changes: 7 additions & 18 deletions src/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import { fetchData, insertParams, stringifyQuery } from './utils'
import { paths } from './types/api'
import { fetchData, stringifyQuery } from './utils'

type Primitive = string | number | boolean | null

interface Params {
path?: { [key: string]: Primitive }
query?: { [key: string]: Primitive }
body?: unknown
}

export function callEndpoint<T extends keyof paths>(
baseUrl: string,
path: T,
parameters?: paths[T]['get']['parameters'],
rawUrl?: string,
): Promise<paths[T]['get']['responses'][200]['schema']> {
let url = rawUrl
let body
if (!url) {
const params = parameters as Params
const pathname = insertParams(path, params?.path)
const search = stringifyQuery(params?.query)
url = `${baseUrl}${pathname}${search}`
body = params?.body
export function callEndpoint<T>(url: string, params?: Params, rawUrl?: string): Promise<T> {
if (rawUrl) {
return fetchData(rawUrl)
}

return fetchData(url, body)
const search = stringifyQuery(params?.query)
const endpoint = `${url}${search}`
return fetchData(endpoint, params?.body)
}
101 changes: 58 additions & 43 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { callEndpoint } from './endpoint'
import { operations } from './types/api'
import { SafeTransactionEstimation, TransactionDetails, TransactionListPage } from './types/transactions'
import {
MultisigTransactionRequest,
SafeTransactionEstimation,
SafeTransactionEstimationRequest,
TransactionDetails,
TransactionListPage,
} from './types/transactions'
import { FiatCurrencies, OwnedSafes, SafeBalanceResponse, SafeCollectibleResponse, SafeInfo } from './types/common'
import { MasterCopyReponse } from './types/master-copies'
import { ChainListResponse, ChainInfo } from './types/chains'
import { SafeAppsResponse } from './types/safe-apps'
import { DecodedDataResponse } from './types/decoded-data'
export * from './types/safe-apps'
export * from './types/transactions'
export * from './types/chains'
Expand All @@ -15,7 +22,7 @@ export * from './types/common'
* Get basic information about a Safe. E.g. owners, modules, version etc
*/
export function getSafeInfo(baseUrl: string, chainId: string, address: string): Promise<SafeInfo> {
return callEndpoint(baseUrl, '/chains/{chainId}/safes/{address}/', { path: { chainId, address } })
return callEndpoint(`${baseUrl}/chains/${chainId}/safes/${address}/`)
}

/**
Expand All @@ -26,10 +33,12 @@ export function getBalances(
chainId: string,
address: string,
currency = 'usd',
query: operations['safes_balances_list']['parameters']['query'] = {},
query: {
trusted?: boolean // Return trusted tokens
exclude_spam?: boolean // Return spam tokens
} = {},
): Promise<SafeBalanceResponse> {
return callEndpoint(baseUrl, '/chains/{chainId}/safes/{address}/balances/{currency}/', {
path: { chainId, address, currency },
return callEndpoint(`${baseUrl}/chains/${chainId}/safes/${address}/balances/${currency}/`, {
query,
})
}
Expand All @@ -38,14 +47,14 @@ export function getBalances(
* Get a list of supported fiat currencies (e.g. USD, EUR etc)
*/
export function getFiatCurrencies(baseUrl: string): Promise<FiatCurrencies> {
return callEndpoint(baseUrl, '/balances/supported-fiat-codes')
return callEndpoint(`${baseUrl}/balances/supported-fiat-codes`)
}

/**
* Get the addresses of all Safes belonging to an owner
*/
export function getOwnedSafes(baseUrl: string, chainId: string, address: string): Promise<OwnedSafes> {
return callEndpoint(baseUrl, '/chains/{chainId}/owners/{address}/safes', { path: { chainId, address } })
return callEndpoint(`${baseUrl}/chains/${chainId}/owners/${address}/safes`)
}

/**
Expand All @@ -55,9 +64,12 @@ export function getCollectibles(
baseUrl: string,
chainId: string,
address: string,
query: operations['safes_collectibles_list']['parameters']['query'] = {},
query: {
trusted?: boolean // Return trusted tokens
exclude_spam?: boolean // Return spam tokens
} = {},
): Promise<SafeCollectibleResponse[]> {
return callEndpoint(baseUrl, '/chains/{chainId}/safes/{address}/collectibles/', { path: { chainId, address }, query })
return callEndpoint(`${baseUrl}/chains/${chainId}/safes/${address}/collectibles/`, { query })
}

/**
Expand All @@ -66,15 +78,10 @@ export function getCollectibles(
export function getTransactionHistory(
baseUrl: string,
chainId: string,
address: string,
safeAddress: string,
pageUrl?: string,
): Promise<TransactionListPage> {
return callEndpoint(
baseUrl,
'/chains/{chainId}/safes/{safe_address}/transactions/history',
{ path: { chainId, safe_address: address }, query: {} },
pageUrl,
)
return callEndpoint(`${baseUrl}/chains/${chainId}/safes/${safeAddress}/transactions/history`, undefined, pageUrl)
}

/**
Expand All @@ -83,15 +90,10 @@ export function getTransactionHistory(
export function getTransactionQueue(
baseUrl: string,
chainId: string,
address: string,
safeAddress: string,
pageUrl?: string,
): Promise<TransactionListPage> {
return callEndpoint(
baseUrl,
'/chains/{chainId}/safes/{safe_address}/transactions/queued',
{ path: { chainId, safe_address: address }, query: {} },
pageUrl,
)
return callEndpoint(`${baseUrl}/chains/${chainId}/safes/${safeAddress}/transactions/queued`, undefined, pageUrl)
}

/**
Expand All @@ -102,9 +104,7 @@ export function getTransactionDetails(
chainId: string,
transactionId: string,
): Promise<TransactionDetails> {
return callEndpoint(baseUrl, '/chains/{chainId}/transactions/{transactionId}', {
path: { chainId, transactionId },
})
return callEndpoint(`${baseUrl}/chains/${chainId}/transactions/${transactionId}`)
}

/**
Expand All @@ -113,11 +113,10 @@ export function getTransactionDetails(
export function postSafeGasEstimation(
baseUrl: string,
chainId: string,
address: string,
body: operations['post_safe_gas_estimation']['parameters']['body'],
safeAddress: string,
body: SafeTransactionEstimationRequest,
): Promise<SafeTransactionEstimation> {
return callEndpoint(baseUrl, '/chains/{chainId}/safes/{safe_address}/multisig-transactions/estimations', {
path: { chainId, safe_address: address },
return callEndpoint(`${baseUrl}/chains/${chainId}/safes/${safeAddress}/multisig-transactions/estimations`, {
body,
})
}
Expand All @@ -128,11 +127,10 @@ export function postSafeGasEstimation(
export function proposeTransaction(
baseUrl: string,
chainId: string,
address: string,
body: operations['propose_transaction']['parameters']['body'],
safeAddress: string,
body: MultisigTransactionRequest,
): Promise<TransactionDetails> {
return callEndpoint(baseUrl, '/chains/{chainId}/transactions/{safe_address}/propose', {
path: { chainId, safe_address: address },
return callEndpoint(`${baseUrl}/chains/${chainId}/transactions/${safeAddress}/propose`, {
body,
})
}
Expand All @@ -142,9 +140,13 @@ export function proposeTransaction(
*/
export function getChainsConfig(
baseUrl: string,
query?: operations['chains_list']['parameters']['query'],
query?: {
ordering?: string
limit?: number
offset?: number
},
): Promise<ChainListResponse> {
return callEndpoint(baseUrl, '/chains/', {
return callEndpoint(`${baseUrl}/chains/`, {
query,
})
}
Expand All @@ -153,18 +155,31 @@ export function getChainsConfig(
* Returns a chain config
*/
export function getChainConfig(baseUrl: string, chainId: string): Promise<ChainInfo> {
return callEndpoint(baseUrl, '/chains/{chainId}/', {
path: { chainId: chainId },
})
return callEndpoint(`${baseUrl}/chains/${chainId}/`)
}

/**
* Returns Safe Apps List
*/
export function getSafeApps(baseUrl: string, chainId: string): Promise<SafeAppsResponse> {
return callEndpoint(baseUrl, '/chains/{chainId}/safe-apps', {
path: { chainId: chainId },
})
return callEndpoint(`${baseUrl}/chains/${chainId}/safe-apps`)
}

/**
* Returns List of Master Copies
*/
export function getMasterCopies(baseUrl: string, chainId: string): Promise<MasterCopyReponse> {
return callEndpoint(`${baseUrl}/chains/${chainId}/about/master-copies`)
}

/**
* Returns decoded data
*/
export function getDecodedData(baseUrl: string, chainId: string, encodedData: string): Promise<DecodedDataResponse> {
return callEndpoint(`${baseUrl}/chains/${chainId}/data-decoder`, {
body: {
data: encodedData,
},
})
}
/* eslint-enable @typescript-eslint/explicit-module-boundary-types */
Loading

0 comments on commit a9c2051

Please sign in to comment.