Skip to content

Commit

Permalink
Cowswap 332/quote metadata (#22)
Browse files Browse the repository at this point in the history
* Added quote metadata types

* Added quote metadata to json schema

* Added unit tests validating quote metadata

* Reformated file with prettier (did not have it setup before)

* Bumping default app version to 0.2.0
  • Loading branch information
alfetopito committed May 25, 2022
1 parent 78cb266 commit 47527e5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/api/metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AppDataDoc, MetadataDoc } from './types'
import { CowError } from '../../utils/common'

const DEFAULT_APP_CODE = 'CowSwap'
const DEFAULT_APP_VERSION = '0.1.0'
const DEFAULT_APP_VERSION = '0.2.0'

export class MetadataApi {
context: Context
Expand Down
2 changes: 1 addition & 1 deletion src/api/metadata/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const HTTP_STATUS_OK = 200
const HTTP_STATUS_INTERNAL_ERROR = 500

const DEFAULT_APP_DATA_DOC = {
version: '0.1.0',
version: '0.2.0',
appCode: 'CowSwap',
metadata: {},
}
Expand Down
7 changes: 7 additions & 0 deletions src/api/metadata/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ export interface ReferralMetadata extends Metadata {
address: string
}

export interface QuoteMetadata extends Metadata {
id?: string
sellAmount: string
buyAmount: string
}

export type MetadataDoc = {
referrer?: ReferralMetadata
quote?: QuoteMetadata
}

export type AppDataDoc = {
Expand Down
32 changes: 32 additions & 0 deletions src/schemas/appData.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"properties": {
"referrer": {
"$ref": "#/definitions/kindMetadata/referrer"
},
"quote": {
"$ref": "#/definitions/kindMetadata/quote"
}
}
}
Expand All @@ -49,6 +52,13 @@
"examples": ["0xb6BAd41ae76A11D10f7b0E664C5007b908bC77C9"],
"type": "string"
},
"bigNumber": {
"$id": "#/definitions/bigNumber",
"pattern": "\\d+",
"title": "BigNumber",
"examples": ["90741097240912730913, 0, 75891372"],
"type": "string"
},
"kindMetadata": {
"referrer": {
"$id": "#/definitions/referrer",
Expand All @@ -64,6 +74,28 @@
"title": "Referrer address"
}
}
},
"quote": {
"$id": "#/definitions/quote",
"required": ["sellAmount", "buyAmount"],
"title": "Quote",
"type": "object",
"properties": {
"id": {
"$id": "#/definitions/quote/id",
"title": "Quote id",
"examples": ["XA23443543534FF"],
"type": "string"
},
"sellAmount": {
"$ref": "#/definitions/bigNumber",
"title": "Quote sell amount"
},
"buyAmount": {
"$ref": "#/definitions/bigNumber",
"title": "Quote buy amount"
}
}
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions src/utils/appData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ const VALID_RESULT = {
result: true,
}

const BASE_DOCUMENT = {
version: '0.1.0',
metadata: {},
}

const INVALID_CID_LENGTH = 'Incorrect length'

beforeEach(() => {
fetchMock.dontMock()
})

test('Valid minimal document', async () => {
const validation = await validateAppDataDocument({
version: '0.1.0',
metadata: {},
})
const validation = await validateAppDataDocument(BASE_DOCUMENT)
expect(validation).toEqual(VALID_RESULT)
})

Expand Down Expand Up @@ -112,3 +114,31 @@ test('Valid IPFS appData from CID', async () => {

expect(validation).toEqual(VALID_RESULT)
})

test('Valid: quote metadata - minimal', async () => {
const document = { ...BASE_DOCUMENT, metadata: { quote: { sellAmount: '1', buyAmount: '1' } } }
const validation = await validateAppDataDocument(document)

expect(validation).toEqual(VALID_RESULT)
})

test('Valid: quote metadata - with all fields', async () => {
const document = { ...BASE_DOCUMENT, metadata: { quote: { sellAmount: '1', buyAmount: '1', id: 'S09D8ZFAX' } } }
const validation = await validateAppDataDocument(document)

expect(validation).toEqual(VALID_RESULT)
})

test('Invalid: quote metadata - missing fields', async () => {
const document = { ...BASE_DOCUMENT, metadata: { quote: {} } }
const validation = await validateAppDataDocument(document)

expect(validation.result).toBeFalsy()
})

test('Invalid: quote metadata - wrong type', async () => {
const document = { ...BASE_DOCUMENT, metadata: { quote: { sellAmount: 312, buyAmount: '0xbab3' } } }
const validation = await validateAppDataDocument(document)

expect(validation.result).toBeFalsy()
})

0 comments on commit 47527e5

Please sign in to comment.