Skip to content

Commit

Permalink
add getCurrentSlot (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljttl3q04t authored Jun 27, 2024
1 parent 474058c commit 0430d8c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/translucent/src/provider/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export class Emulator implements Provider {
this.protocolParameters = protocolParameters;
}

// deno-lint-ignore require-await
async getCurrentSlot(): Promise<number> {
return this.slot;
}

addUTxO(utxo: UTxO) {
this.ledger[utxo.txHash + utxo.outputIndex] = {
utxo,
Expand Down
12 changes: 12 additions & 0 deletions packages/translucent/src/provider/kupmios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ export class Kupmios implements Provider {
this.headers = headers;
}

async getCurrentSlot(): Promise<number> {
const result = (await fetch(
`${this.kupoUrl}/health`,
{
headers: { ...(this.headers ?? {}), Accept: "application/json;charset=utf-8" },
},
).then((res) => res.json())) as unknown as {
most_recent_node_tip: number;
};
return result.most_recent_node_tip;
}

async getProtocolParameters(): Promise<ProtocolParameters> {
const client = await this.ogmiosWsp(
"queryLedgerState/protocolParameters",
Expand Down
12 changes: 12 additions & 0 deletions packages/translucent/src/provider/kupmiosv5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ export class KupmiosV5 implements Provider {
this.ogmiosUrl = ogmiosUrl;
}

async getCurrentSlot(): Promise<number> {
const result = (await fetch(
`${this.kupoUrl}/health`,
{
headers: { Accept: "application/json;charset=utf-8" },
},
).then((res) => res.json())) as unknown as {
most_recent_node_tip: number;
};
return result.most_recent_node_tip;
}

async getProtocolParameters(): Promise<ProtocolParameters> {
const client = await this.ogmiosWsp("Query", {
query: "currentProtocolParameters",
Expand Down
7 changes: 7 additions & 0 deletions packages/translucent/src/provider/maestro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export class Maestro implements Provider {
this.turboSubmit = turboSubmit;
}

async getCurrentSlot(): Promise<number> {
const chainTipResult = await fetch(`${this.url}/chain-tip`, {
headers: this.commonHeaders(),
}).then((res) => res.json());
return parseInt(chainTipResult.data.slot);
}

async getProtocolParameters(): Promise<ProtocolParameters> {
const timestampedResult = await fetch(`${this.url}/protocol-params`, {
headers: this.commonHeaders(),
Expand Down
8 changes: 8 additions & 0 deletions packages/translucent/src/translucent/translucent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,18 @@ export class Translucent {
return verifyData(addressHex, keyHash, payload, signedMessage);
}

/**
* @deprecated
*/
currentSlot(): Slot {
return this.utils.unixTimeToSlot(Date.now());
}

async getCurrentSlot(): Promise<Slot> {
let slot = await this.provider.getCurrentSlot();
return slot;
}

utxosAt(addressOrCredential: Address | Credential): Promise<UTxO[]> {
return this.provider.getUtxos(addressOrCredential);
}
Expand Down
1 change: 1 addition & 0 deletions packages/translucent/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface Provider {
getDatum(datumHash: DatumHash): Promise<Datum>;
awaitTx(txHash: TxHash, checkInterval?: number): Promise<boolean>;
submitTx(tx: Transaction): Promise<TxHash>;
getCurrentSlot(): Promise<number>;
}

export type Credential = {
Expand Down

0 comments on commit 0430d8c

Please sign in to comment.