@cowprotocol/cow-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # CoW protocol SDK
2
+ <p align="center">
3
+ <img width="400" src="docs/images/CoW.png">
4
+ </p>
5
+
6
+
7
+ ### Install Dependencies
8
+
9
+ ```bash
10
+ yarn
11
+ ```
12
+
13
+ ### Build
14
+
15
+ ```bash
16
+ yarn build
17
+ ```
18
+
19
+ ### Run
20
+
21
+ ```bash
22
+ yarn start
23
+ ```
24
+
25
+ ### Unit testing
26
+
27
+ ```bash
28
+ yarn test
29
+ ```
30
+
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ presets: [
3
+ ['@babel/preset-env', {targets: {node: 'current'}}],
4
+ '@babel/preset-typescript',
5
+ ],
6
+ };
Binary file
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@cowprotocol/cow-sdk",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "devDependencies": {
6
+ "@babel/preset-typescript": "^7.16.0",
7
+ "@types/jest": "^27.0.3",
8
+ "jest": "^27.3.1",
9
+ "microbundle": "^0.14.2"
10
+ },
11
+ "source": "src/index.ts",
12
+ "exports": {
13
+ "require": "./dist/cow-sdk.cjs",
14
+ "default": "./dist/cow-sdk.modern.js"
15
+ },
16
+ "main": "./dist/cow-sdk.cjs",
17
+ "module": "./dist/cow-sdk.module.js",
18
+ "unpkg": "./dist/cow-sdk.umd.js",
19
+ "scripts": {
20
+ "build": "microbundle",
21
+ "start": "microbundle watch",
22
+ "test": "jest"
23
+ },
24
+ "types": "dist/index.d.ts"
25
+ }
package/src/CowSdk.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { version as SDK_VERSION } from '../package.json'
2
+ import { validateAppDataDocument } from './utils/appData'
3
+
4
+ export class CowSdk {
5
+ static version = SDK_VERSION
6
+
7
+ validateAppDataDocument = validateAppDataDocument
8
+ }
9
+
10
+ export default CowSdk
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './CowSdk'
@@ -0,0 +1,86 @@
1
+ {
2
+ "$id": "https://cowswap.exchange/appdata.schema.json",
3
+ "$schema": "http://json-schema.org/draft-07/schema",
4
+ "description": "Metadata JSON document for adding information to orders.",
5
+ "required": [
6
+ "version",
7
+ "metadata"
8
+ ],
9
+ "title": "AppData Root Schema",
10
+ "type": "object",
11
+ "properties": {
12
+ "version": {
13
+ "$id": "#/properties/version",
14
+ "description": "Semantic versioning of document",
15
+ "examples": [
16
+ "1.0.0",
17
+ "1.2.3"
18
+ ],
19
+ "title": "Semantic Versioning",
20
+ "type": "string"
21
+ },
22
+ "appCode": {
23
+ "$id": "#/properties/appCode",
24
+ "description": "The code identifying the CLI, UI, service generating the order.",
25
+ "examples": [
26
+ "CowSwap"
27
+ ],
28
+ "title": "App Code",
29
+ "type": "string"
30
+ },
31
+ "metadata": {
32
+ "$id": "#/properties/metadata",
33
+ "default": {},
34
+ "description": "Each metadata will specify one aspect of the order.",
35
+ "required": [],
36
+ "title": "Metadata descriptors",
37
+ "type": "object",
38
+ "properties": {
39
+ "referrer": {
40
+ "$ref": "#/definitions/kindMetadata/referrer"
41
+ }
42
+ }
43
+ }
44
+ },
45
+ "definitions": {
46
+ "version": {
47
+ "$id": "#/definitions/version",
48
+ "description": "Semantic versioning of document",
49
+ "examples": [
50
+ "1.0.0",
51
+ "1.2.3"
52
+ ],
53
+ "title": "Semantic Versioning",
54
+ "type": "string"
55
+ },
56
+ "ethereumAddress": {
57
+ "$id": "#/definitions/ethereumAddress",
58
+ "pattern": "^0x[a-fA-F0-9]{40}$",
59
+ "title": "Ethereum compatible address",
60
+ "examples": [
61
+ "0xb6BAd41ae76A11D10f7b0E664C5007b908bC77C9"
62
+ ],
63
+ "type": "string"
64
+ },
65
+ "kindMetadata": {
66
+ "referrer": {
67
+ "$id": "#/definitions/referrer",
68
+ "required": [
69
+ "version",
70
+ "address"
71
+ ],
72
+ "title": "Referrer",
73
+ "type": "object",
74
+ "properties": {
75
+ "version": {
76
+ "$ref": "#/definitions/version"
77
+ },
78
+ "address": {
79
+ "$ref": "#/definitions/ethereumAddress",
80
+ "title": "Referrer address"
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,68 @@
1
+ import {validateAppDataDocument } from './appData'
2
+
3
+ const VALID_RESULT = {
4
+ result: true,
5
+ }
6
+
7
+ test('Valid minimal document', async () => {
8
+ const validation = await validateAppDataDocument({
9
+ "version": "0.1.0",
10
+ "metadata": {}
11
+ })
12
+ expect(validation).toEqual(VALID_RESULT);
13
+ });
14
+
15
+ test('Valid minimal document + appCode', async () => {
16
+ const validation = await validateAppDataDocument({
17
+ "version": "0.1.0",
18
+ "appCode": "MyApp",
19
+ "metadata": {}
20
+ })
21
+ expect(validation).toEqual(VALID_RESULT);
22
+ });
23
+
24
+
25
+ test('Valid minimal document + appCode + referrer', async () => {
26
+ const validation = await validateAppDataDocument({
27
+ "version": "0.1.0",
28
+ "appCode": "MyApp",
29
+ "metadata": {
30
+ "referrer": {
31
+ "version": "0.1.0",
32
+ "address": "0xFEB4acf3df3cDEA7399794D0869ef76A6EfAff52"
33
+ }
34
+ }
35
+ })
36
+ expect(validation).toEqual(VALID_RESULT);
37
+ });
38
+
39
+ test('Invalid: Bad referrer', async () => {
40
+ const validation = await validateAppDataDocument({
41
+ "version": "0.1.0",
42
+ "appCode": "MyApp",
43
+ "metadata": {
44
+ "referrer": {
45
+ "version": "0.1.0",
46
+ "address": "this is not an ethereum address"
47
+ }
48
+ }
49
+ })
50
+ expect(validation.result).toBeFalsy();
51
+ });
52
+
53
+ test('Invalid: No version', async () => {
54
+ const validation = await validateAppDataDocument({
55
+ "appCode": "MyApp",
56
+ "metadata": {}
57
+ })
58
+ expect(validation.result).toBeFalsy();
59
+ });
60
+
61
+
62
+ test('Invalid: No metadata', async () => {
63
+ const validation = await validateAppDataDocument({
64
+ "version": "0.1.0",
65
+ "appCode": "MyApp",
66
+ })
67
+ expect(validation.result).toBeFalsy();
68
+ });
@@ -0,0 +1,34 @@
1
+ import Ajv from 'ajv'
2
+
3
+ let validate: Ajv.ValidateFunction | undefined
4
+ let ajv: Ajv.Ajv
5
+
6
+ interface ValidationResult {
7
+ result: boolean,
8
+ errors?: Ajv.ErrorObject[]
9
+ }
10
+
11
+ async function getValidator(): Promise<{ ajv: Ajv.Ajv, validate: Ajv.ValidateFunction }> {
12
+ if (!ajv) {
13
+ ajv = new Ajv()
14
+ }
15
+
16
+ if (!validate) {
17
+ const appDataSchema = await import('../schemas/appData.schema.json');
18
+ validate = ajv.compile(appDataSchema)
19
+ }
20
+
21
+
22
+ return { ajv, validate }
23
+ }
24
+
25
+ export async function validateAppDataDocument(appDataDocument: any): Promise<ValidationResult>{
26
+ const { ajv, validate } = await getValidator()
27
+ const result = !!(await validate(appDataDocument))
28
+
29
+
30
+ return {
31
+ result,
32
+ errors: ajv.errors ?? undefined
33
+ }
34
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "moduleResolution": "node",
4
+ "module": "ESNext",
5
+ "target": "ESNext",
6
+ "strict": true,
7
+ "alwaysStrict": true,
8
+ "resolveJsonModule": true,
9
+ "allowSyntheticDefaultImports": true
10
+ },
11
+ }