diff --git a/packages/translucent/src/provider/emulator.ts b/packages/translucent/src/provider/emulator.ts index de75dcf..87556f1 100644 --- a/packages/translucent/src/provider/emulator.ts +++ b/packages/translucent/src/provider/emulator.ts @@ -101,6 +101,11 @@ export class Emulator implements Provider { this.protocolParameters = protocolParameters; } + // deno-lint-ignore require-await + async getCurrentSlot(): Promise { + return this.slot; + } + addUTxO(utxo: UTxO) { this.ledger[utxo.txHash + utxo.outputIndex] = { utxo, diff --git a/packages/translucent/src/provider/kupmios.ts b/packages/translucent/src/provider/kupmios.ts index a44aea2..003f34e 100644 --- a/packages/translucent/src/provider/kupmios.ts +++ b/packages/translucent/src/provider/kupmios.ts @@ -58,6 +58,18 @@ export class Kupmios implements Provider { this.headers = headers; } + async getCurrentSlot(): Promise { + 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 { const client = await this.ogmiosWsp( "queryLedgerState/protocolParameters", diff --git a/packages/translucent/src/provider/kupmiosv5.ts b/packages/translucent/src/provider/kupmiosv5.ts index 287243a..d2b5c77 100644 --- a/packages/translucent/src/provider/kupmiosv5.ts +++ b/packages/translucent/src/provider/kupmiosv5.ts @@ -31,6 +31,18 @@ export class KupmiosV5 implements Provider { this.ogmiosUrl = ogmiosUrl; } + async getCurrentSlot(): Promise { + 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 { const client = await this.ogmiosWsp("Query", { query: "currentProtocolParameters", diff --git a/packages/translucent/src/provider/maestro.ts b/packages/translucent/src/provider/maestro.ts index 26a3524..7f28398 100644 --- a/packages/translucent/src/provider/maestro.ts +++ b/packages/translucent/src/provider/maestro.ts @@ -38,6 +38,13 @@ export class Maestro implements Provider { this.turboSubmit = turboSubmit; } + async getCurrentSlot(): Promise { + const chainTipResult = await fetch(`${this.url}/chain-tip`, { + headers: this.commonHeaders(), + }).then((res) => res.json()); + return parseInt(chainTipResult.data.slot); + } + async getProtocolParameters(): Promise { const timestampedResult = await fetch(`${this.url}/protocol-params`, { headers: this.commonHeaders(), diff --git a/packages/translucent/src/translucent/translucent.ts b/packages/translucent/src/translucent/translucent.ts index b43c6df..c843667 100644 --- a/packages/translucent/src/translucent/translucent.ts +++ b/packages/translucent/src/translucent/translucent.ts @@ -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 { + let slot = await this.provider.getCurrentSlot(); + return slot; + } + utxosAt(addressOrCredential: Address | Credential): Promise { return this.provider.getUtxos(addressOrCredential); } diff --git a/packages/translucent/src/types/types.ts b/packages/translucent/src/types/types.ts index c7022e8..01d0236 100644 --- a/packages/translucent/src/types/types.ts +++ b/packages/translucent/src/types/types.ts @@ -38,6 +38,7 @@ export interface Provider { getDatum(datumHash: DatumHash): Promise; awaitTx(txHash: TxHash, checkInterval?: number): Promise; submitTx(tx: Transaction): Promise; + getCurrentSlot(): Promise; } export type Credential = {