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

Add: Koios token support #438

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/six-readers-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lucid-evolution/provider": patch
---

Add token support to Koios provider
22 changes: 19 additions & 3 deletions packages/provider/src/internal/koios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ export const DatumInfo = S.Struct({
bytes: S.String,
});

export const getHeadersWithToken = (
token?: string,
headers: Record<string, string> = {},
): Record<string, string> => {
if (token) {
return {
...headers,
Authorization: `Bearer ${token}`,
};
}
return headers;
};

export const postWithSchemaValidation = <A, I, R>(
url: string | URL,
data: unknown,
Expand Down Expand Up @@ -295,10 +308,12 @@ export const postWithSchemaValidation = <A, I, R>(
export const getWithSchemaValidation = <A, I, R>(
url: string | URL,
schema: S.Schema<A, I, R>,
headers: Record<string, string> = {},
) =>
pipe(
HttpClientRequest.get(url),
HttpClient.fetchOk,
Effect.succeed(HttpClientRequest.get(url)),
Effect.map(HttpClientRequest.setHeaders(headers)),
Effect.flatMap(HttpClient.fetch),
HttpClientResponse.json,
Effect.flatMap(S.decodeUnknown(schema)),
);
Expand Down Expand Up @@ -353,6 +368,7 @@ const toScriptRef = (
export const getUtxosEffect = (
baseUrl: string,
addressOrCredential: CoreType.Address | CoreType.Credential,
headers: Record<string, string> = {},
): Effect.Effect<
CoreType.UTxO[],
string | HttpClientError | HttpBodyError | ParseError
Expand All @@ -366,7 +382,7 @@ export const getUtxosEffect = (
Effect.if(typeof addressOrCredential === "string", {
onFalse: () =>
Effect.fail("Credential Type is not supported in Koios yet."),
onTrue: () => postWithSchemaValidation(url, body, schema),
onTrue: () => postWithSchemaValidation(url, body, schema, headers),
}),
Effect.map(([result]) =>
result
Expand Down
68 changes: 56 additions & 12 deletions packages/provider/src/koios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ export class KoiosError extends Data.TaggedError("KoiosError")<{
*/
export class Koios implements Provider {
private readonly baseUrl: string;
private readonly token?: string;

constructor(baseUrl: string) {
constructor(baseUrl: string, token?: string) {
this.baseUrl = baseUrl;
this.token = token;
}

async getProtocolParameters(): Promise<ProtocolParameters> {
const url = `${this.baseUrl}/epoch_params?limit=1`;
const schema = S.Array(_Koios.ProtocolParametersSchema);
const [result] = await pipe(
_Koios.getWithSchemaValidation(url, schema),
_Koios.getWithSchemaValidation(
url,
schema,
_Koios.getHeadersWithToken(this.token),
),
Effect.timeout(10_000),
Effect.catchAllCause((cause) => new KoiosError({ cause })),
Effect.runPromise,
Expand Down Expand Up @@ -91,7 +97,11 @@ export class Koios implements Provider {

async getUtxos(addressOrCredential: Address | Credential): Promise<UTxO[]> {
const result = await pipe(
_Koios.getUtxosEffect(this.baseUrl, addressOrCredential),
_Koios.getUtxosEffect(
this.baseUrl,
addressOrCredential,
_Koios.getHeadersWithToken(this.token),
),
Effect.timeout(10_000),
Effect.catchAllCause((cause) => new KoiosError({ cause })),
Effect.runPromise,
Expand All @@ -104,7 +114,11 @@ export class Koios implements Provider {
unit: Unit,
): Promise<UTxO[]> {
const result = await pipe(
_Koios.getUtxosEffect(this.baseUrl, addressOrCredential),
_Koios.getUtxosEffect(
this.baseUrl,
addressOrCredential,
_Koios.getHeadersWithToken(this.token),
),
Effect.map((utxos) =>
utxos.filter((utxo) => {
const keys = Object.keys(utxo.assets);
Expand All @@ -122,7 +136,11 @@ export class Koios implements Provider {
let { policyId, assetName } = fromUnit(unit);
const url = `${this.baseUrl}/asset_addresses?_asset_policy=${policyId}&_asset_name=${assetName}`;
const result: UTxO = await pipe(
_Koios.getWithSchemaValidation(url, S.Array(_Koios.AssetAddressSchema)),
_Koios.getWithSchemaValidation(
url,
S.Array(_Koios.AssetAddressSchema),
_Koios.getHeadersWithToken(this.token),
),
Effect.flatMap((adresses) =>
adresses.length === 0
? Effect.fail("Unit not found")
Expand Down Expand Up @@ -163,7 +181,12 @@ export class Koios implements Provider {
};
const schema = S.Array(_Koios.TxInfoSchema);
const [result] = await pipe(
_Koios.postWithSchemaValidation(url, body, schema),
_Koios.postWithSchemaValidation(
url,
body,
schema,
_Koios.getHeadersWithToken(this.token),
),
Effect.timeout(10_000),
Effect.catchAllCause((cause) => new KoiosError({ cause })),
Effect.runPromise,
Expand Down Expand Up @@ -209,6 +232,7 @@ export class Koios implements Provider {
url,
body,
S.Array(_Koios.AccountInfoSchema),
_Koios.getHeadersWithToken(this.token),
),
Effect.flatMap((result) =>
result.length === 0
Expand All @@ -232,7 +256,12 @@ export class Koios implements Provider {
_datum_hashes: [datumHash],
};
const result = await pipe(
_Koios.postWithSchemaValidation(url, body, S.Array(_Koios.DatumInfo)),
_Koios.postWithSchemaValidation(
url,
body,
S.Array(_Koios.DatumInfo),
_Koios.getHeadersWithToken(this.token),
),
Effect.flatMap((result) =>
result.length === 0
? Effect.fail("No Datum Found by Datum Hash")
Expand All @@ -252,7 +281,12 @@ export class Koios implements Provider {
};
const schema = S.Array(_Koios.TxInfoSchema);
const result = await pipe(
_Koios.postWithSchemaValidation(url, body, schema),
_Koios.postWithSchemaValidation(
url,
body,
schema,
_Koios.getHeadersWithToken(this.token),
),
Effect.repeat({
schedule: Schedule.exponential(checkInterval),
until: (result) => result.length > 0,
Expand All @@ -271,9 +305,14 @@ export class Koios implements Provider {
const body = fromHex(tx);
const schema = _Koios.TxHashSchema;
const result = await pipe(
_Koios.postWithSchemaValidation(url, body, schema, {
"Content-Type": "application/cbor",
}),
_Koios.postWithSchemaValidation(
url,
body,
schema,
_Koios.getHeadersWithToken(this.token, {
"Content-Type": "application/cbor",
}),
),
Effect.timeout(10_000),
Effect.catchAllCause((cause) => new KoiosError({ cause })),
Effect.runPromise,
Expand All @@ -298,7 +337,12 @@ export class Koios implements Provider {
};
const schema = _Ogmios.JSONRPCSchema(S.Array(_Ogmios.RedeemerSchema));
const result = await pipe(
_Koios.postWithSchemaValidation(url, body, schema),
_Koios.postWithSchemaValidation(
url,
body,
schema,
_Koios.getHeadersWithToken(this.token),
),
Effect.flatMap((response) =>
"error" in response
? Effect.fail(response)
Expand Down
Loading
Loading