Skip to content

Commit dcd73ce

Browse files
committed
add more header option for mina instances
1 parent bc695b8 commit dcd73ce

File tree

5 files changed

+63
-16
lines changed

5 files changed

+63
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1818
## [Unreleased](https://github.com/o1-labs/o1js/compare/b857516...HEAD)
1919

2020
### Added
21+
2122
- `setFee` and `setFeePerSnarkCost` for `Transaction` and `PendingTransaction` https://github.com/o1-labs/o1js/pull/1968
2223
- Doc comments for various ZkProgram methods https://github.com/o1-labs/o1js/pull/1974
2324
- `MerkleList.popOption()` for popping the last element and also learning if there was one https://github.com/o1-labs/o1js/pull/1997
25+
- Added custom header support for `Fetch` methods such as `fetchEvents`, `fetchActions` etc. and to `Mina` instance. Also added two new methods `setMinaDefaultHeaders` and `setArchiveDefaultHeaders` https://github.com/o1-labs/o1js/pull/2004
2426

2527
### Changed
28+
2629
- Sort order for actions now includes the transaction sequence number and the exact account id sequence https://github.com/o1-labs/o1js/pull/1917
2730
- Updated typedoc version for generating docs https://github.com/o1-labs/o1js/pull/1973
2831

@@ -381,7 +384,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
381384
- `Reducer.reduce()` requires the maximum number of actions per method as an explicit (optional) argument https://github.com/o1-labs/o1js/pull/1450
382385
- The default value is 1 and should work for most existing contracts
383386
- `new UInt64()` and `UInt64.from()` no longer unsafely accept a field element as input. https://github.com/o1-labs/o1js/pull/1438 [@julio4](https://github.com/julio4)
384-
As a replacement, `UInt64.Unsafe.fromField()` was introduced
387+
As a replacement, `UInt64.Unsafe.fromField()` was introduced
385388
- This prevents you from accidentally creating a `UInt64` without proving that it fits in 64 bits
386389
- Equivalent changes were made to `UInt32`
387390
- Fixed vulnerability in `Field.to/fromBits()` outlined in [#1023](https://github.com/o1-labs/o1js/issues/1023) by imposing a limit of 254 bits https://github.com/o1-labs/o1js/pull/1461

src/lib/mina/fetch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ async function fetchLatestBlockZkappStatus(
565565
await makeGraphqlRequest<LastBlockQueryFailureCheckResponse>(
566566
lastBlockQueryFailureCheck(blockLength),
567567
graphqlEndpoint,
568-
networkConfig.minaFallbackEndpoints
568+
networkConfig.minaFallbackEndpoints,
569+
{ headers: networkConfig.minaDefaultHeaders }
569570
);
570571
if (error) throw Error(`Error making GraphQL request: ${error.statusText}`);
571572
let bestChain = resp?.data;

src/lib/mina/mina-instance.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ type Mina = {
8989
fetchEvents: (
9090
publicKey: PublicKey,
9191
tokenId?: Field,
92-
filterOptions?: EventActionFilterOptions
92+
filterOptions?: EventActionFilterOptions,
93+
headers?: HeadersInit
9394
) => ReturnType<typeof Fetch.fetchEvents>;
9495
fetchActions: (
9596
publicKey: PublicKey,
9697
actionStates?: ActionStates,
97-
tokenId?: Field
98+
tokenId?: Field,
99+
headers?: HeadersInit
98100
) => ReturnType<typeof Fetch.fetchActions>;
99101
getActions: (
100102
publicKey: PublicKey,
@@ -186,9 +188,15 @@ function getBalance(publicKey: PublicKey, tokenId?: Field) {
186188
async function fetchEvents(
187189
publicKey: PublicKey,
188190
tokenId: Field,
189-
filterOptions: EventActionFilterOptions = {}
191+
filterOptions: EventActionFilterOptions = {},
192+
headers?: HeadersInit
190193
) {
191-
return await activeInstance.fetchEvents(publicKey, tokenId, filterOptions);
194+
return await activeInstance.fetchEvents(
195+
publicKey,
196+
tokenId,
197+
filterOptions,
198+
headers
199+
);
192200
}
193201

194202
/**
@@ -197,9 +205,15 @@ async function fetchEvents(
197205
async function fetchActions(
198206
publicKey: PublicKey,
199207
actionStates?: ActionStates,
200-
tokenId?: Field
208+
tokenId?: Field,
209+
headers?: HeadersInit
201210
) {
202-
return await activeInstance.fetchActions(publicKey, actionStates, tokenId);
211+
return await activeInstance.fetchActions(
212+
publicKey,
213+
actionStates,
214+
tokenId,
215+
headers
216+
);
203217
}
204218

205219
/**

src/lib/mina/mina.network.unit-test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ describe('Test network with headers', () => {
299299

300300
it('Archive default headers with fetchActions', async () => {
301301
await Mina.fetchActions(bobAccount);
302+
302303
expect(lastFetchOptions.headers).toMatchObject({
303304
'Content-Type': 'application/json',
304305
Authorization: 'Bearer archive-default-token',
@@ -308,10 +309,23 @@ describe('Test network with headers', () => {
308309

309310
it('Archive default headers with fetchEvents', async () => {
310311
await Mina.fetchEvents(bobAccount, TokenId.empty());
312+
311313
expect(lastFetchOptions.headers).toMatchObject({
312314
'Content-Type': 'application/json',
313315
Authorization: 'Bearer archive-default-token',
314316
'X-Test': 'archive-test',
315317
});
316318
});
319+
320+
it('Archive default headers with per request headers in fetchActions', async () => {
321+
await Mina.fetchActions(bobAccount, undefined, undefined, {
322+
'X-Test': 'per-request-test',
323+
});
324+
325+
expect(lastFetchOptions.headers).toMatchObject({
326+
'Content-Type': 'application/json',
327+
Authorization: 'Bearer archive-default-token',
328+
'X-Test': 'per-request-test',
329+
});
330+
});
317331
});

src/lib/mina/mina.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,24 @@ function Network(
409409
async fetchEvents(
410410
publicKey: PublicKey,
411411
tokenId: Field = TokenId.default,
412-
filterOptions: EventActionFilterOptions = {}
412+
filterOptions: EventActionFilterOptions = {},
413+
headers?: HeadersInit
413414
) {
414415
let pubKey = publicKey.toBase58();
415416
let token = TokenId.toBase58(tokenId);
416417

417418
return Fetch.fetchEvents(
418419
{ publicKey: pubKey, tokenId: token },
419420
archiveEndpoint,
420-
filterOptions
421+
filterOptions,
422+
headers
421423
);
422424
},
423425
async fetchActions(
424426
publicKey: PublicKey,
425427
actionStates?: ActionStates,
426-
tokenId: Field = TokenId.default
428+
tokenId: Field = TokenId.default,
429+
headers?: HeadersInit
427430
) {
428431
let pubKey = publicKey.toBase58();
429432
let token = TokenId.toBase58(tokenId);
@@ -444,7 +447,8 @@ function Network(
444447
},
445448
tokenId: token,
446449
},
447-
archiveEndpoint
450+
archiveEndpoint,
451+
headers
448452
);
449453
},
450454
getActions(
@@ -509,15 +513,22 @@ function dummyAccount(pubkey?: PublicKey): Account {
509513
return dummy;
510514
}
511515

512-
async function waitForFunding(address: string): Promise<void> {
516+
async function waitForFunding(
517+
address: string,
518+
headers?: HeadersInit
519+
): Promise<void> {
513520
let attempts = 0;
514521
let maxAttempts = 30;
515522
let interval = 30000;
516523
const executePoll = async (
517524
resolve: () => void,
518525
reject: (err: Error) => void | Error
519526
) => {
520-
let { account } = await Fetch.fetchAccount({ publicKey: address });
527+
let { account } = await Fetch.fetchAccount(
528+
{ publicKey: address },
529+
undefined,
530+
{ headers }
531+
);
521532
attempts++;
522533
if (account) {
523534
return resolve();
@@ -533,7 +544,11 @@ async function waitForFunding(address: string): Promise<void> {
533544
/**
534545
* Requests the [testnet faucet](https://faucet.minaprotocol.com/api/v1/faucet) to fund a public key.
535546
*/
536-
async function faucet(pub: PublicKey, network: string = 'berkeley-qanet') {
547+
async function faucet(
548+
pub: PublicKey,
549+
network: string = 'berkeley-qanet',
550+
headers?: HeadersInit
551+
) {
537552
let address = pub.toBase58();
538553
let response = await fetch('https://faucet.minaprotocol.com/api/v1/faucet', {
539554
method: 'POST',
@@ -549,7 +564,7 @@ async function faucet(pub: PublicKey, network: string = 'berkeley-qanet') {
549564
`Error funding account ${address}, got response status: ${response.status}, text: ${response.statusText}`
550565
);
551566
}
552-
await waitForFunding(address);
567+
await waitForFunding(address, headers);
553568
}
554569

555570
function genesisToNetworkConstants(

0 commit comments

Comments
 (0)