Skip to content

Commit

Permalink
Merge pull request #2 from decentraland/feat/add-blockchain-types
Browse files Browse the repository at this point in the history
feat: add network and chain types
  • Loading branch information
nachomazzara authored Feb 19, 2021
2 parents 27cd7ad + c35f78c commit e9b9757
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
*
* DEFAULT VALUE: false
*/
"skipLibCheck": true,
"skipLibCheck": true
},
/**
* Configures how the API report file (*.api.md) will be generated.
Expand Down Expand Up @@ -129,7 +129,7 @@
* SUPPORTED TOKENS: <projectFolder>, <packageName>, <unscopedPackageName>
* DEFAULT VALUE: "<projectFolder>/etc/"
*/
"reportFolder": "<projectFolder>/report/",
"reportFolder": "<projectFolder>/report/"
/**
* Specifies the folder where the temporary report file is written. The file name portion is determined by
* the "reportFileName" setting.
Expand Down
42 changes: 42 additions & 0 deletions report/schemas.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ export type AbstractTypedSchema<T> = {

export { Ajv }

// @alpha
export enum ChainId {
// (undocumented)
ETHEREUM_GOERLI = 5,
// (undocumented)
ETHEREUM_KOVAN = 42,
// (undocumented)
ETHEREUM_MAINNET = 1,
// (undocumented)
ETHEREUM_RINKEBY = 4,
// (undocumented)
ETHEREUM_ROPSTEN = 3,
// (undocumented)
MATIC_MAINNET = 89,
// (undocumented)
MATIC_MUMBAI = 13881
}

// @alpha (undocumented)
export namespace ChainId {
const // (undocumented)
schema: JSONSchema<ChainId>;
const // (undocumented)
validate: ValidateFunction<ChainId>;
}

// @public
export function generateValidator<T>(schema: JSONSchema<T>): ValidateFunction<T>;

Expand All @@ -36,6 +62,22 @@ export namespace MetaTransaction {
validate: ValidateFunction<MetaTransaction>;
}

// @alpha
export enum Network {
// (undocumented)
ETHEREUM = "ETHEREUM",
// (undocumented)
MATIC = "MATIC"
}

// @alpha (undocumented)
export namespace Network {
const // (undocumented)
schema: JSONSchema<Network>;
const // (undocumented)
validate: ValidateFunction<Network>;
}

export { ValidateFunction }

// @public
Expand Down
27 changes: 27 additions & 0 deletions src/dapps/chain-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { generateValidator, JSONSchema, ValidateFunction } from '../validation'

/**
* Different supported chain ids
* @alpha
*/
export enum ChainId {
ETHEREUM_MAINNET = 1,
ETHEREUM_ROPSTEN = 3,
ETHEREUM_RINKEBY = 4,
ETHEREUM_GOERLI = 5,
ETHEREUM_KOVAN = 42,
MATIC_MAINNET = 89,
MATIC_MUMBAI = 13881,
}

/**
* @alpha
*/
export namespace ChainId {
export const schema: JSONSchema<ChainId> = {
type: 'number',
enum: Object.values(ChainId),
}

export const validate: ValidateFunction<ChainId> = generateValidator(schema)
}
16 changes: 9 additions & 7 deletions src/dapps/meta-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateValidator, JSONSchema, ValidateFunction } from "../validation"
import { generateValidator, JSONSchema, ValidateFunction } from '../validation'

/**
* Meta-transaction to be relayed
Expand All @@ -14,19 +14,21 @@ export type MetaTransaction = {
*/
export namespace MetaTransaction {
export const schema: JSONSchema<MetaTransaction> = {
type: "object",
type: 'object',
properties: {
from: { type: "string" },
from: { type: 'string' },
params: {
type: "array",
items: [{ type: "string" }, { type: "string" }],
type: 'array',
items: [{ type: 'string' }, { type: 'string' }],
additionalItems: false,
minItems: 2,
},
},
additionalProperties: false,
required: ["from", "params"],
required: ['from', 'params'],
}

export const validate: ValidateFunction<MetaTransaction> = generateValidator(schema)
export const validate: ValidateFunction<MetaTransaction> = generateValidator(
schema
)
}
22 changes: 22 additions & 0 deletions src/dapps/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { generateValidator, JSONSchema, ValidateFunction } from '../validation'

/**
* Different supported networks
* @alpha
*/
export enum Network {
ETHEREUM = 'ETHEREUM',
MATIC = 'MATIC',
}

/**
* @alpha
*/
export namespace Network {
export const schema: JSONSchema<Network> = {
type: 'string',
enum: Object.values(Network),
}

export const validate: ValidateFunction<Network> = generateValidator(schema)
}
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// export the utils
export * from "./validation"
export * from './validation'

// export all the types
export { MetaTransaction } from "./dapps/meta-transactions"
export { ChainId } from './dapps/chain-id'
export { Network } from './dapps/network'
export { MetaTransaction } from './dapps/meta-transactions'
15 changes: 15 additions & 0 deletions test/chain-id.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import expect from 'expect'
import { ChainId } from '../src'
import { testTypeSignature } from './test-utils'

describe('ChainId tests', () => {
const chainId: ChainId = ChainId.ETHEREUM_KOVAN

testTypeSignature(ChainId, chainId)

it('static tests must pass', () => {
expect(ChainId.validate(chainId)).toEqual(true)
expect(ChainId.validate(null)).toEqual(false)
expect(ChainId.validate({})).toEqual(false)
})
})
15 changes: 15 additions & 0 deletions test/network.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import expect from 'expect'
import { Network } from '../src'
import { testTypeSignature } from './test-utils'

describe('Network tests', () => {
const network: Network = Network.ETHEREUM

testTypeSignature(Network, network)

it('static tests must pass', () => {
expect(Network.validate(network)).toEqual(true)
expect(Network.validate(null)).toEqual(false)
expect(Network.validate({})).toEqual(false)
})
})

0 comments on commit e9b9757

Please sign in to comment.