-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an http quote provider that sends a fetch request to the target URL to get a quote, instead of using a hardcoded value from env vars. Builds on #8864
- Loading branch information
1 parent
0be9f25
commit 1c3cb63
Showing
10 changed files
with
142 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,4 +86,4 @@ | |
"engines": { | ||
"node": ">=18" | ||
} | ||
} | ||
} |
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,75 @@ | ||
import { L2Block } from '@aztec/circuit-types'; | ||
import { times } from '@aztec/foundation/collection'; | ||
import { promiseWithResolvers } from '@aztec/foundation/promise'; | ||
|
||
import { type Server, createServer } from 'http'; | ||
import { type AddressInfo } from 'net'; | ||
|
||
import { HttpQuoteProvider } from './http.js'; | ||
|
||
describe('HttpQuoteProvider', () => { | ||
let server: Server; | ||
let port: number; | ||
|
||
let status: number = 200; | ||
let response: any = {}; | ||
let request: any = {}; | ||
|
||
let provider: HttpQuoteProvider; | ||
let blocks: L2Block[]; | ||
|
||
beforeAll(async () => { | ||
server = createServer({ keepAliveTimeout: 60000 }, (req, res) => { | ||
const chunks: Buffer[] = []; | ||
req | ||
.on('data', (chunk: Buffer) => { | ||
chunks.push(chunk); | ||
}) | ||
.on('end', () => { | ||
request = JSON.parse(Buffer.concat(chunks).toString()); | ||
}); | ||
|
||
res.writeHead(status, { 'Content-Type': 'application/json' }); | ||
res.end(JSON.stringify(response)); | ||
}); | ||
|
||
const { promise, resolve } = promiseWithResolvers(); | ||
server.listen(0, '127.0.0.1', () => resolve(null)); | ||
await promise; | ||
port = (server.address() as AddressInfo).port; | ||
}); | ||
|
||
beforeEach(() => { | ||
provider = new HttpQuoteProvider(`http://127.0.0.1:${port}`); | ||
blocks = times(3, i => L2Block.random(i + 1, 4)); | ||
response = { basisPointFee: 100, bondAmount: '100000000000000000000', validUntilSlot: '100' }; | ||
}); | ||
|
||
afterAll(() => { | ||
server?.close(); | ||
}); | ||
|
||
it('requests a quote sending epoch data', async () => { | ||
const quote = await provider.getQuote(1, blocks); | ||
|
||
expect(request).toEqual( | ||
expect.objectContaining({ epochNumber: 1, fromBlock: 1, toBlock: 3, txCount: 12, totalFees: expect.any(String) }), | ||
); | ||
|
||
expect(quote).toEqual({ | ||
basisPointFee: response.basisPointFee, | ||
bondAmount: BigInt(response.bondAmount), | ||
validUntilSlot: BigInt(response.validUntilSlot), | ||
}); | ||
}); | ||
|
||
it('throws an error if the response is missing required fields', async () => { | ||
response = { basisPointFee: 100 }; | ||
await expect(provider.getQuote(1, blocks)).rejects.toThrow(/Missing required fields/i); | ||
}); | ||
|
||
it('throws an error if the response is not ok', async () => { | ||
status = 400; | ||
await expect(provider.getQuote(1, blocks)).rejects.toThrow(/Failed to fetch quote/i); | ||
}); | ||
}); |
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,2 +1,47 @@ | ||
// TODO: Implement me! This should send a request to a configurable URL to get the quote. | ||
export class HttpQuoteProvider {} | ||
import { type L2Block } from '@aztec/circuit-types'; | ||
|
||
import { type QuoteProvider, type QuoteProviderResult } from './index.js'; | ||
import { getTotalFees, getTxCount } from './utils.js'; | ||
|
||
export class HttpQuoteProvider implements QuoteProvider { | ||
constructor(private readonly url: string) {} | ||
|
||
public async getQuote(epochNumber: number, epoch: L2Block[]): Promise<QuoteProviderResult | undefined> { | ||
const payload: HttpQuoteRequestPayload = { | ||
epochNumber, | ||
fromBlock: epoch[0].number, | ||
toBlock: epoch.at(-1)!.number, | ||
totalFees: getTotalFees(epoch).toString(), | ||
txCount: getTxCount(epoch), | ||
}; | ||
|
||
const response = await fetch(this.url, { | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: { 'content-type': 'application/json' }, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to fetch quote: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
if (!data.basisPointFee || !data.bondAmount) { | ||
throw new Error(`Missing required fields in response: ${JSON.stringify(data)}`); | ||
} | ||
|
||
const basisPointFee = Number(data.basisPointFee); | ||
const bondAmount = BigInt(data.bondAmount); | ||
const validUntilSlot = data.validUntilSlot ? BigInt(data.validUntilSlot) : undefined; | ||
|
||
return { basisPointFee, bondAmount, validUntilSlot }; | ||
} | ||
} | ||
|
||
export type HttpQuoteRequestPayload = { | ||
epochNumber: number; | ||
fromBlock: number; | ||
toBlock: number; | ||
totalFees: string; | ||
txCount: number; | ||
}; |
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,8 +1,8 @@ | ||
import { type EpochProofQuotePayload, type L2Block } from '@aztec/circuit-types'; | ||
|
||
type QuoteProviderResult = Pick<EpochProofQuotePayload, 'basisPointFee' | 'bondAmount'> & | ||
export type QuoteProviderResult = Pick<EpochProofQuotePayload, 'basisPointFee' | 'bondAmount'> & | ||
Partial<Pick<EpochProofQuotePayload, 'validUntilSlot'>>; | ||
|
||
export interface QuoteProvider { | ||
getQuote(epoch: L2Block[]): Promise<QuoteProviderResult | undefined>; | ||
getQuote(epochNumber: number, epoch: L2Block[]): Promise<QuoteProviderResult | undefined>; | ||
} |
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