-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1547 from balancer/v3-canary
publish to prod
- Loading branch information
Showing
44 changed files
with
210 additions
and
896 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
extend type Query { | ||
blocksGetAverageBlockTime: Float! | ||
blocksGetBlocksPerSecond: Float! | ||
blocksGetBlocksPerDay: Float! | ||
blocksGetBlocksPerYear: Float! | ||
} | ||
|
||
extend type Mutation { | ||
cacheAverageBlockTime: String! | ||
blocksGetAverageBlockTime: Float! @deprecated | ||
blocksGetBlocksPerSecond: Float! @deprecated | ||
blocksGetBlocksPerDay: Float! @deprecated | ||
blocksGetBlocksPerYear: Float! @deprecated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect, test, describe, mock, beforeEach } from 'bun:test'; | ||
import { blockNumbers } from './index'; | ||
import { Chain } from '@prisma/client'; | ||
|
||
describe('blockNumbers', () => { | ||
const mockEvents = { | ||
$queryRawUnsafe: mock(() => {}), | ||
}; | ||
|
||
beforeEach(() => { | ||
mockEvents.$queryRawUnsafe.mockReset(); | ||
}); | ||
|
||
describe('getBlock', () => { | ||
test('should return block number for given timestamp', async () => { | ||
const mockEvent = [{ blockNumber: 12345 }]; | ||
mockEvents.$queryRawUnsafe.mockResolvedValue(mockEvent); | ||
|
||
const service = blockNumbers(mockEvents as any); | ||
const result = await service.getBlock(Chain.MAINNET, 1000); | ||
|
||
expect(result).toBe(12345); | ||
}); | ||
|
||
test('should return undefined if no event found', async () => { | ||
mockEvents.$queryRawUnsafe.mockResolvedValue([]); | ||
|
||
const service = blockNumbers(mockEvents as any); | ||
const result = await service.getBlock(Chain.MAINNET, 1000); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('getBlocksPerDay', () => { | ||
test('should return count of blocks in last 24 hours', async () => { | ||
mockEvents.$queryRawUnsafe.mockResolvedValue([{ max: 4000, min: 1000 }]); | ||
|
||
const service = blockNumbers(mockEvents as any); | ||
const result = await service.getBlocksPerDay(Chain.MAINNET); | ||
|
||
expect(result).toBe(1000); | ||
}); | ||
}); | ||
|
||
describe('getDailyBlocks', () => { | ||
test('should return daily block numbers', async () => { | ||
const mockBlocks = [ | ||
{ timestamp: 1000, number: 12345 }, | ||
{ timestamp: 2000, number: 12445 }, | ||
]; | ||
mockEvents.$queryRawUnsafe.mockResolvedValue(mockBlocks); | ||
|
||
const service = blockNumbers(mockEvents as any); | ||
const result = await service.getDailyBlocks(Chain.MAINNET, 2); | ||
|
||
expect(result).toEqual(mockBlocks); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Chain, Prisma } from '@prisma/client'; | ||
import { prisma } from '../../prisma/prisma-client'; | ||
|
||
export const blockNumbers = (db = prisma) => ({ | ||
/** | ||
* Get the block number for a given timestamp | ||
* | ||
* @param chain | ||
* @param timestamp | ||
* @returns | ||
*/ | ||
async getBlock(chain: Chain, timestamp: number) { | ||
const [event] = await db.$queryRawUnsafe<{ blockNumber: number }[]>(` | ||
SELECT "blockNumber" | ||
FROM "PartitionedPoolEvent" | ||
WHERE chain = '${chain}' | ||
AND "blockTimestamp" <= ${timestamp}::integer | ||
ORDER BY "blockTimestamp" DESC | ||
LIMIT 1; | ||
`); | ||
|
||
return event?.blockNumber; | ||
}, | ||
/** | ||
* Gets the number of blocks per day meaning the speed of the chain. | ||
* Calculated from average number of blocks per day for the last 3 days. | ||
* | ||
* @param chain | ||
* @param timestamp | ||
* @returns | ||
*/ | ||
async getBlocksPerDay(chain: Chain) { | ||
const [blocks] = await db.$queryRawUnsafe<{ max: number; min: number }[]>(` | ||
SELECT | ||
MAX("blockNumber") as max, | ||
MIN("blockNumber") as min | ||
FROM "PartitionedPoolEvent" | ||
WHERE chain = '${chain}' | ||
AND "blockTimestamp" >= (EXTRACT(EPOCH FROM NOW()) - 86400 * 3)::integer; | ||
`); | ||
|
||
const range = blocks.max - blocks.min; | ||
|
||
return Math.ceil(range / 3); | ||
}, | ||
/** | ||
* Block numbers for the last n days closest to 00:00:00 (UTC) | ||
* | ||
* @param chain | ||
* @param days | ||
* @returns | ||
*/ | ||
async getDailyBlocks(chain: Chain, days: number) { | ||
const blocks = await db.$queryRawUnsafe<{ timestamp: number; number: number }[]>(` | ||
SELECT | ||
("blockTimestamp"/86400)::INTEGER * 86400 as timestamp, | ||
MIN("blockNumber") as number | ||
FROM "PartitionedPoolEvent" | ||
WHERE chain = '${chain}' | ||
AND "blockTimestamp" >= ((EXTRACT(EPOCH FROM NOW()) / 86400)::integer * 86400 - 86400 * ${days})::integer | ||
GROUP BY 1 | ||
ORDER BY 1 DESC; | ||
`); | ||
|
||
return blocks; | ||
}, | ||
}); |
Oops, something went wrong.