From 9528b79c3846e8927510a61a70361f86b2720b8b Mon Sep 17 00:00:00 2001 From: alessandrokonrad Date: Wed, 7 Aug 2024 23:07:00 +0200 Subject: [PATCH] updated ogmios in provider --- src/provider/kupmios.ts | 75 +++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/src/provider/kupmios.ts b/src/provider/kupmios.ts index 5c759370..74159101 100644 --- a/src/provider/kupmios.ts +++ b/src/provider/kupmios.ts @@ -17,8 +17,6 @@ import { import { C } from "../core/mod.ts"; import { fromHex, fromUnit, toHex } from "../utils/mod.ts"; -// TODO: Ogmios related endpoints need to be udpated due to new Cardano node and breaking Ogmios v6 - export class Kupmios implements Provider { kupoUrl: string; ogmiosUrl: string; @@ -33,9 +31,7 @@ export class Kupmios implements Provider { } async getProtocolParameters(): Promise { - const client = await this.ogmiosWsp("Query", { - query: "currentProtocolParameters", - }); + const client = await this.rpc("queryLedgerState/protocolParameters"); return new Promise((res, rej) => { client.addEventListener("message", (msg: MessageEvent) => { @@ -44,33 +40,39 @@ export class Kupmios implements Provider { // deno-lint-ignore no-explicit-any const costModels: any = {}; - Object.keys(result.costModels).forEach((v) => { + Object.keys(result.plutusCostModels).forEach((v) => { const version = v.split(":")[1].toUpperCase(); const plutusVersion = "Plutus" + version; - costModels[plutusVersion] = result.costModels[v]; + costModels[plutusVersion] = result.plutusCostModels[v]; }); - const [memNum, memDenom] = result.prices.memory.split("/"); - const [stepsNum, stepsDenom] = result.prices.steps.split("/"); + const [memNum, memDenom] = result.scriptExecutionPrices.memory.split( + "/", + ); + const [stepsNum, stepsDenom] = result.scriptExecutionPrices.cpu.split( + "/", + ); res( { minFeeA: parseInt(result.minFeeCoefficient), - minFeeB: parseInt(result.minFeeConstant), - maxTxSize: parseInt(result.maxTxSize), - maxValSize: parseInt(result.maxValueSize), - keyDeposit: BigInt(result.stakeKeyDeposit), - poolDeposit: BigInt(result.poolDeposit), + minFeeB: parseInt(result.minFeeConstant.ada.lovelace), + maxTxSize: parseInt(result.maxTransactionSize.bytes), + maxValSize: parseInt(result.maxValueSize.bytes), + keyDeposit: BigInt(result.stakeCredentialDeposit.ada.lovelace), + poolDeposit: BigInt(result.stakePoolDeposit.ada.lovelace), priceMem: parseInt(memNum) / parseInt(memDenom), priceStep: parseInt(stepsNum) / parseInt(stepsDenom), maxTxExMem: BigInt(result.maxExecutionUnitsPerTransaction.memory), maxTxExSteps: BigInt( - result.maxExecutionUnitsPerTransaction.steps, + result.maxExecutionUnitsPerTransaction.cpu, ), - coinsPerUtxoByte: BigInt(result.coinsPerUtxoByte), + coinsPerUtxoByte: BigInt(result.minUtxoDepositCoefficient), collateralPercentage: parseInt(result.collateralPercentage), maxCollateralInputs: parseInt(result.maxCollateralInputs), costModels, - minfeeRefscriptCostPerByte: 0, // TODO + minfeeRefscriptCostPerByte: parseInt( + result.minFeeReferenceScripts.base, + ), }, ); client.close(); @@ -151,22 +153,23 @@ export class Kupmios implements Provider { } async getDelegation(rewardAddress: RewardAddress): Promise { - const client = await this.ogmiosWsp("Query", { - query: { "delegationsAndRewards": [rewardAddress] }, - }); + const client = await this.rpc( + "queryLedgerState/rewardAccountSummaries", + { keys: [rewardAddress] }, // TODO: Does this work for reward addresses that are scripts as well? + ); return new Promise((res, rej) => { client.addEventListener("message", (msg: MessageEvent) => { try { const { result } = JSON.parse(msg.data); const delegation = (result ? Object.values(result)[0] : {}) as { - delegate: string; - rewards: number; + delegate: { id: string }; + rewards: { ada: { lovelace: number } }; }; res( { - poolId: delegation?.delegate || null, - rewards: BigInt(delegation?.rewards || 0), + poolId: delegation?.delegate.id || null, + rewards: BigInt(delegation?.rewards.ada.lovelace || 0), }, ); client.close(); @@ -203,17 +206,17 @@ export class Kupmios implements Provider { } async submitTx(tx: Transaction): Promise { - const client = await this.ogmiosWsp("SubmitTx", { - submit: tx, + const client = await this.rpc("submitTransaction", { + transaction: { cbor: tx }, }); return new Promise((res, rej) => { client.addEventListener("message", (msg: MessageEvent) => { try { - const { result } = JSON.parse(msg.data); + const { result, error } = JSON.parse(msg.data); - if (result.SubmitSuccess) res(result.SubmitSuccess.txId); - else rej(result.SubmitFail); + if (result?.transaction) res(result.transaction.id); + else rej(error); client.close(); } catch (e) { rej(e); @@ -267,20 +270,18 @@ export class Kupmios implements Provider { })); } - private async ogmiosWsp( - methodname: string, - args: unknown, + private async rpc( + method: string, + params?: unknown, ): Promise { const client = new WebSocket(this.ogmiosUrl); await new Promise((res) => { client.addEventListener("open", () => res(1), { once: true }); }); client.send(JSON.stringify({ - type: "jsonwsp/request", - version: "1.0", - servicename: "ogmios", - methodname, - args, + "jsonrpc": "2.0", + method, + params, })); return client; }