Skip to content

Commit

Permalink
add more header option for mina instances
Browse files Browse the repository at this point in the history
  • Loading branch information
kadirchan committed Feb 4, 2025
1 parent bc695b8 commit dcd73ce
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased](https://github.com/o1-labs/o1js/compare/b857516...HEAD)

### Added

- `setFee` and `setFeePerSnarkCost` for `Transaction` and `PendingTransaction` https://github.com/o1-labs/o1js/pull/1968
- Doc comments for various ZkProgram methods https://github.com/o1-labs/o1js/pull/1974
- `MerkleList.popOption()` for popping the last element and also learning if there was one https://github.com/o1-labs/o1js/pull/1997
- 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

### Changed

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

Expand Down Expand Up @@ -381,7 +384,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `Reducer.reduce()` requires the maximum number of actions per method as an explicit (optional) argument https://github.com/o1-labs/o1js/pull/1450
- The default value is 1 and should work for most existing contracts
- `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)
As a replacement, `UInt64.Unsafe.fromField()` was introduced
As a replacement, `UInt64.Unsafe.fromField()` was introduced
- This prevents you from accidentally creating a `UInt64` without proving that it fits in 64 bits
- Equivalent changes were made to `UInt32`
- 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
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mina/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@ async function fetchLatestBlockZkappStatus(
await makeGraphqlRequest<LastBlockQueryFailureCheckResponse>(
lastBlockQueryFailureCheck(blockLength),
graphqlEndpoint,
networkConfig.minaFallbackEndpoints
networkConfig.minaFallbackEndpoints,
{ headers: networkConfig.minaDefaultHeaders }
);
if (error) throw Error(`Error making GraphQL request: ${error.statusText}`);
let bestChain = resp?.data;
Expand Down
26 changes: 20 additions & 6 deletions src/lib/mina/mina-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ type Mina = {
fetchEvents: (
publicKey: PublicKey,
tokenId?: Field,
filterOptions?: EventActionFilterOptions
filterOptions?: EventActionFilterOptions,
headers?: HeadersInit
) => ReturnType<typeof Fetch.fetchEvents>;
fetchActions: (
publicKey: PublicKey,
actionStates?: ActionStates,
tokenId?: Field
tokenId?: Field,
headers?: HeadersInit
) => ReturnType<typeof Fetch.fetchActions>;
getActions: (
publicKey: PublicKey,
Expand Down Expand Up @@ -186,9 +188,15 @@ function getBalance(publicKey: PublicKey, tokenId?: Field) {
async function fetchEvents(
publicKey: PublicKey,
tokenId: Field,
filterOptions: EventActionFilterOptions = {}
filterOptions: EventActionFilterOptions = {},
headers?: HeadersInit
) {
return await activeInstance.fetchEvents(publicKey, tokenId, filterOptions);
return await activeInstance.fetchEvents(
publicKey,
tokenId,
filterOptions,
headers
);
}

/**
Expand All @@ -197,9 +205,15 @@ async function fetchEvents(
async function fetchActions(
publicKey: PublicKey,
actionStates?: ActionStates,
tokenId?: Field
tokenId?: Field,
headers?: HeadersInit
) {
return await activeInstance.fetchActions(publicKey, actionStates, tokenId);
return await activeInstance.fetchActions(
publicKey,
actionStates,
tokenId,
headers
);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/lib/mina/mina.network.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ describe('Test network with headers', () => {

it('Archive default headers with fetchActions', async () => {
await Mina.fetchActions(bobAccount);

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
Authorization: 'Bearer archive-default-token',
Expand All @@ -308,10 +309,23 @@ describe('Test network with headers', () => {

it('Archive default headers with fetchEvents', async () => {
await Mina.fetchEvents(bobAccount, TokenId.empty());

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
Authorization: 'Bearer archive-default-token',
'X-Test': 'archive-test',
});
});

it('Archive default headers with per request headers in fetchActions', async () => {
await Mina.fetchActions(bobAccount, undefined, undefined, {
'X-Test': 'per-request-test',
});

expect(lastFetchOptions.headers).toMatchObject({
'Content-Type': 'application/json',
Authorization: 'Bearer archive-default-token',
'X-Test': 'per-request-test',
});
});
});
31 changes: 23 additions & 8 deletions src/lib/mina/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,24 @@ function Network(
async fetchEvents(
publicKey: PublicKey,
tokenId: Field = TokenId.default,
filterOptions: EventActionFilterOptions = {}
filterOptions: EventActionFilterOptions = {},
headers?: HeadersInit
) {
let pubKey = publicKey.toBase58();
let token = TokenId.toBase58(tokenId);

return Fetch.fetchEvents(
{ publicKey: pubKey, tokenId: token },
archiveEndpoint,
filterOptions
filterOptions,
headers
);
},
async fetchActions(
publicKey: PublicKey,
actionStates?: ActionStates,
tokenId: Field = TokenId.default
tokenId: Field = TokenId.default,
headers?: HeadersInit
) {
let pubKey = publicKey.toBase58();
let token = TokenId.toBase58(tokenId);
Expand All @@ -444,7 +447,8 @@ function Network(
},
tokenId: token,
},
archiveEndpoint
archiveEndpoint,
headers
);
},
getActions(
Expand Down Expand Up @@ -509,15 +513,22 @@ function dummyAccount(pubkey?: PublicKey): Account {
return dummy;
}

async function waitForFunding(address: string): Promise<void> {
async function waitForFunding(
address: string,
headers?: HeadersInit
): Promise<void> {
let attempts = 0;
let maxAttempts = 30;
let interval = 30000;
const executePoll = async (
resolve: () => void,
reject: (err: Error) => void | Error
) => {
let { account } = await Fetch.fetchAccount({ publicKey: address });
let { account } = await Fetch.fetchAccount(
{ publicKey: address },
undefined,
{ headers }
);
attempts++;
if (account) {
return resolve();
Expand All @@ -533,7 +544,11 @@ async function waitForFunding(address: string): Promise<void> {
/**
* Requests the [testnet faucet](https://faucet.minaprotocol.com/api/v1/faucet) to fund a public key.
*/
async function faucet(pub: PublicKey, network: string = 'berkeley-qanet') {
async function faucet(
pub: PublicKey,
network: string = 'berkeley-qanet',
headers?: HeadersInit
) {
let address = pub.toBase58();
let response = await fetch('https://faucet.minaprotocol.com/api/v1/faucet', {
method: 'POST',
Expand All @@ -549,7 +564,7 @@ async function faucet(pub: PublicKey, network: string = 'berkeley-qanet') {
`Error funding account ${address}, got response status: ${response.status}, text: ${response.statusText}`
);
}
await waitForFunding(address);
await waitForFunding(address, headers);
}

function genesisToNetworkConstants(
Expand Down

0 comments on commit dcd73ce

Please sign in to comment.