Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api): updated the protocol params endpoint with the new api response #3720

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/wallet-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"@shopify/flash-list": "^1.4.1",
"@types/bip39": "^3.0.0",
"@types/pbkdf2": "^3.1.2",
"@yoroi/api": "^1.5.2",
"@yoroi/api": "1.5.3",
"@yoroi/claim": "1.0.0",
"@yoroi/common": "^1.5.4",
"@yoroi/exchange": "^2.1.1",
Expand Down Expand Up @@ -264,7 +264,7 @@
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.32.0",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"babel-eslint": "^10.1.0",
"babel-jest": "^29.2.1",
"babel-loader": "8.2.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yoroi/api",
"version": "1.5.2",
"version": "1.5.3",
"description": "The API package of Yoroi SDK",
"keywords": [
"yoroi",
Expand Down Expand Up @@ -148,7 +148,7 @@
"@types/jest": "^29.5.12",
"@types/react": "^18.2.55",
"@types/react-test-renderer": "^18.0.7",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"axios": "^1.5.0",
"axios-mock-adapter": "^1.21.5",
"commitlint": "^17.0.2",
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/cardano/api/cardano-api-maker.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* istanbul ignore file */
import {Api} from '@yoroi/types'
import {paramsMockResponse} from './protocol-params.mocks'
import {protocolParamsMockResponse} from './protocol-params.mocks'
import {bestBlockMockResponse} from './best-block.mocks'

const loading = () => new Promise(() => {})
Expand All @@ -17,9 +17,9 @@ const delayedResponse = <T = never>({
})

const getProtocolParams = {
success: () => Promise.resolve(paramsMockResponse),
success: () => Promise.resolve(protocolParamsMockResponse),
delayed: (timeout?: number) =>
delayedResponse({data: paramsMockResponse, timeout}),
delayedResponse({data: protocolParamsMockResponse, timeout}),
empty: () => Promise.resolve({}),
loading,
error: {
Expand Down
137 changes: 133 additions & 4 deletions packages/api/src/cardano/api/protocol-params.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,138 @@
import {Chain} from '@yoroi/types'

export const paramsMockResponse: Chain.Cardano.ProtocolParams = {
export const protocolParamsMockResponse: Chain.Cardano.ProtocolParams = {
linearFee: {
constant: '155381',
coefficient: '44',
},
minFeeReferenceScript: {
coinsPerByte: {
numerator: '0',
denominator: '1',
},
tierStepBytes: '0',
multiplier: '0',
},
coinsPerUtxoByte: '4310',
keyDeposit: '2000000',
linearFee: {coefficient: '44', constant: '155381'},
poolDeposit: '500000000',
epoch: 509,
keyDeposit: '2000000',
epoch: 67,
maxBlockBodySize: '90112',
maxBlockHeaderSize: '1100',
maxTxSize: '16384',
maxReferenceScriptsSize: '0',
stakePoolPledgeInfluence: {
numerator: '3',
denominator: '10',
},
monetaryExpansion: {
numerator: '3',
denominator: '1000',
},
treasuryExpansion: {
numerator: '1',
denominator: '5',
},
minPoolCost: '340000000',
maxExecutionUnits: {
perTransaction: {
memory: '14000000',
cpu: '10000000000',
},
perBlock: {
memory: '62000000',
cpu: '20000000000',
},
},
scriptExecutionPrices: {
memory: {
numerator: '577',
denominator: '10000',
},
cpu: {
numerator: '721',
denominator: '10000000',
},
},
maxCollateralInputs: '3',
collateralPercentage: '150',
maxValueSize: '5000',
version: {
major: '8',
minor: '0',
},
governanceActionDeposit: '0',
delegateRepresentativeDeposit: '0',
constitutionalCommitteeMinSize: '0',
constitutionalCommitteeMaxTermLength: '0',
governanceActionLifetime: '0',
delegateRepresentativeMaxIdleTime: '0',
desiredNumberOfStakePools: '500',
stakePoolRetirementEpochBound: '18',
votingThresholds: {
stakePool: {
noConfidence: {
numerator: '0',
denominator: '1',
},
committeeNormal: {
numerator: '0',
denominator: '1',
},
committeeNoConfidence: {
numerator: '0',
denominator: '1',
},
hardFork: {
numerator: '0',
denominator: '1',
},
ppSecurity: {
numerator: '0',
denominator: '1',
},
},
delegateRep: {
noConfidence: {
numerator: '0',
denominator: '1',
},
committeeNormal: {
numerator: '0',
denominator: '1',
},
committeeNoConfidence: {
numerator: '0',
denominator: '1',
},
constitution: {
numerator: '0',
denominator: '1',
},
hardFork: {
numerator: '0',
denominator: '1',
},
ppNetwork: {
numerator: '0',
denominator: '1',
},
ppEconomic: {
numerator: '0',
denominator: '1',
},
ppTechnical: {
numerator: '0',
denominator: '1',
},
ppGovernance: {
numerator: '0',
denominator: '1',
},
treasury: {
numerator: '0',
denominator: '1',
},
},
},
}
22 changes: 13 additions & 9 deletions packages/api/src/cardano/api/protocol-params.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getProtocolParams, isProtocolParamsResponse} from './protocol-params'
import {paramsMockResponse} from './protocol-params.mocks'
import {protocolParamsMockResponse} from './protocol-params.mocks'
import axios from 'axios'

jest.mock('axios')
Expand All @@ -8,13 +8,17 @@ const mockedAxios = axios as jest.MockedFunction<typeof axios>
describe('getProtocolParams', () => {
const baseUrl = 'https://localhost'
const mockFetch = jest.fn()
const customFetcher = jest.fn().mockResolvedValue(paramsMockResponse)
const customFetcher = jest.fn().mockResolvedValue(protocolParamsMockResponse)

beforeEach(() => {
jest.clearAllMocks()
})

it('returns parsed data when response is valid', async () => {
mockFetch.mockResolvedValue(paramsMockResponse)
mockFetch.mockResolvedValue(protocolParamsMockResponse)
const protocolParams = getProtocolParams(baseUrl, mockFetch)
const result = await protocolParams()
expect(result).toEqual(paramsMockResponse)
expect(result).toEqual(protocolParamsMockResponse)
})

it('throws an error if response is not valid', async () => {
Expand All @@ -39,16 +43,16 @@ describe('getProtocolParams', () => {
const protocolParams = getProtocolParams(baseUrl, customFetcher)
const result = await protocolParams()
expect(customFetcher).toHaveBeenCalled()
expect(result).toEqual(paramsMockResponse)
expect(result).toEqual(protocolParamsMockResponse)
})
it('uses fetcher and returns data on successful fetch', async () => {
mockedAxios.mockResolvedValue({data: paramsMockResponse})
mockedAxios.mockResolvedValue({data: protocolParamsMockResponse})

const protocolParams = getProtocolParams(baseUrl)
const result = await protocolParams()

expect(mockedAxios).toHaveBeenCalled()
expect(result).toEqual(paramsMockResponse)
expect(result).toEqual(protocolParamsMockResponse)
})

it('throws an error on network issues', async () => {
Expand All @@ -60,11 +64,11 @@ describe('getProtocolParams', () => {
})
describe('isProtocolParamsResponse', () => {
it('returns true for a valid protocol parameters response', () => {
expect(isProtocolParamsResponse(paramsMockResponse)).toBe(true)
expect(isProtocolParamsResponse(protocolParamsMockResponse)).toBe(true)
})

it('returns false for an invalid protocol parameters response', () => {
const invalidResponse = {...paramsMockResponse, epoch: 'invalid'}
const invalidResponse = {...protocolParamsMockResponse, epoch: 'invalid'}
expect(isProtocolParamsResponse(invalidResponse)).toBe(false)
})

Expand Down
81 changes: 74 additions & 7 deletions packages/api/src/cardano/api/protocol-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,84 @@ export const parseProtocolParamsResponse = (
return isProtocolParamsResponse(data) ? data : undefined
}

const AppProtocolParamsSchema = z.object({
coinsPerUtxoByte: z.string(),
keyDeposit: z.string(),
const RatioSchema = z.object({
numerator: z.string(),
denominator: z.string(),
})

const ProtocolParamsSchema = z.object({
linearFee: z.object({
coefficient: z.string(),
constant: z.string(),
coefficient: z.string(),
}),
minFeeReferenceScript: z.object({
coinsPerByte: RatioSchema,
tierStepBytes: z.string(),
multiplier: z.string(),
}),
coinsPerUtxoByte: z.string(),
poolDeposit: z.string(),
keyDeposit: z.string(),
epoch: z.number().nonnegative(),
maxBlockBodySize: z.string(),
maxBlockHeaderSize: z.string(),
maxTxSize: z.string(),
maxReferenceScriptsSize: z.string(),
stakePoolPledgeInfluence: RatioSchema,
monetaryExpansion: RatioSchema,
treasuryExpansion: RatioSchema,
minPoolCost: z.string(),
maxExecutionUnits: z.object({
perTransaction: z.object({
memory: z.string(),
cpu: z.string(),
}),
perBlock: z.object({
memory: z.string(),
cpu: z.string(),
}),
}),
scriptExecutionPrices: z.object({
memory: RatioSchema,
cpu: RatioSchema,
}),
maxCollateralInputs: z.string(),
collateralPercentage: z.string(),
maxValueSize: z.string(),
version: z.object({
major: z.string(),
minor: z.string(),
}),
governanceActionDeposit: z.string(),
delegateRepresentativeDeposit: z.string(),
constitutionalCommitteeMinSize: z.string(),
constitutionalCommitteeMaxTermLength: z.string(),
governanceActionLifetime: z.string(),
delegateRepresentativeMaxIdleTime: z.string(),
desiredNumberOfStakePools: z.string(),
stakePoolRetirementEpochBound: z.string(),
votingThresholds: z.object({
stakePool: z.object({
noConfidence: RatioSchema,
committeeNormal: RatioSchema,
committeeNoConfidence: RatioSchema,
hardFork: RatioSchema,
ppSecurity: RatioSchema,
}),
delegateRep: z.object({
noConfidence: RatioSchema,
committeeNormal: RatioSchema,
committeeNoConfidence: RatioSchema,
constitution: RatioSchema,
hardFork: RatioSchema,
ppNetwork: RatioSchema,
ppEconomic: RatioSchema,
ppTechnical: RatioSchema,
ppGovernance: RatioSchema,
treasury: RatioSchema,
}),
}),
})

export const isProtocolParamsResponse = createTypeGuardFromSchema(
AppProtocolParamsSchema,
)
export const isProtocolParamsResponse =
createTypeGuardFromSchema(ProtocolParamsSchema)
2 changes: 1 addition & 1 deletion packages/claim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"@types/react-test-renderer": "^18.0.7",
"@yoroi/common": "^1.5.4",
"@yoroi/portfolio": "1.0.3",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"bignumber.js": "^9.0.1",
"commitlint": "^17.0.2",
"del-cli": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"@types/jest": "^29.5.12",
"@types/react": "^18.2.55",
"@types/react-test-renderer": "^18.0.7",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"axios": "^1.5.0",
"axios-mock-adapter": "^1.21.5",
"bignumber.js": "^9.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
"@types/jest": "^29.5.12",
"@types/react": "^18.2.55",
"@types/react-test-renderer": "^18.0.7",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"axios-mock-adapter": "^1.21.5",
"bignumber.js": "^9.0.1",
"commitlint": "^17.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/exchange/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"@types/jest": "^29.5.12",
"@types/react": "^18.2.55",
"@types/react-test-renderer": "^18.0.7",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"axios": "^1.5.0",
"commitlint": "^17.0.2",
"del-cli": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/explorers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"@types/jest": "^29.5.12",
"@types/react": "^18.2.55",
"@types/react-test-renderer": "^18.0.7",
"@yoroi/types": "1.5.8",
"@yoroi/types": "1.5.9",
"bignumber.js": "^9.0.1",
"commitlint": "^17.0.2",
"del-cli": "^5.0.0",
Expand Down
Loading
Loading