Skip to content

Commit

Permalink
updated ogmios in provider
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Aug 7, 2024
1 parent 6839130 commit 9528b79
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions src/provider/kupmios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,9 +31,7 @@ export class Kupmios implements Provider {
}

async getProtocolParameters(): Promise<ProtocolParameters> {
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<string>) => {
Expand All @@ -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();
Expand Down Expand Up @@ -151,22 +153,23 @@ export class Kupmios implements Provider {
}

async getDelegation(rewardAddress: RewardAddress): Promise<Delegation> {
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<string>) => {
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();
Expand Down Expand Up @@ -203,17 +206,17 @@ export class Kupmios implements Provider {
}

async submitTx(tx: Transaction): Promise<TxHash> {
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<string>) => {
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);
Expand Down Expand Up @@ -267,20 +270,18 @@ export class Kupmios implements Provider {
}));
}

private async ogmiosWsp(
methodname: string,
args: unknown,
private async rpc(
method: string,
params?: unknown,
): Promise<WebSocket> {
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;
}
Expand Down

0 comments on commit 9528b79

Please sign in to comment.