Skip to content

Commit

Permalink
feat: add evaluator for blockfrost and yaci
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Aug 16, 2024
1 parent 8e215a4 commit 42cf3b5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/mesh-provider/src/blockfrost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BlockInfo,
castProtocol,
fromUTF8,
IEvaluator,
IFetcher,
IListener,
ISubmitter,
Expand All @@ -28,7 +29,9 @@ import { parseAssetUnit } from "./utils/parse-asset-unit";

export type BlockfrostSupportedNetworks = "mainnet" | "preview" | "preprod";

export class BlockfrostProvider implements IFetcher, IListener, ISubmitter {
export class BlockfrostProvider
implements IFetcher, IListener, ISubmitter, IEvaluator
{
private readonly _axiosInstance: AxiosInstance;
private readonly _network: BlockfrostSupportedNetworks;

Expand Down
31 changes: 27 additions & 4 deletions packages/mesh-provider/src/yaci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import axios, { AxiosInstance } from "axios";

import {
AccountInfo,
Action,
Asset,
AssetMetadata,
BlockInfo,
castProtocol,
fromUTF8,
IEvaluator,
IFetcher,
IListener,
ISubmitter,
NativeScript,
PlutusScript,
Protocol,
RedeemerTagType,
SUPPORTED_HANDLES,
TransactionInfo,
Unit,
Expand All @@ -23,7 +26,9 @@ import { resolveRewardAddress, toScriptRef } from "@meshsdk/core-cst";
import { parseHttpError } from "./utils";
import { parseAssetUnit } from "./utils/parse-asset-unit";

export class YaciProvider implements IFetcher, IListener, ISubmitter {
export class YaciProvider
implements IFetcher, IListener, ISubmitter, IEvaluator
{
private readonly _axiosInstance: AxiosInstance;

/**
Expand Down Expand Up @@ -385,7 +390,7 @@ export class YaciProvider implements IFetcher, IListener, ISubmitter {
}
}

async evaluateTx(txHex: string): Promise<string> {
async evaluateTx(txHex: string) {
try {
const headers = { "Content-Type": "application/cbor" };
const { status, data } = await this._axiosInstance.post(
Expand All @@ -396,8 +401,26 @@ export class YaciProvider implements IFetcher, IListener, ISubmitter {
},
);

if (status === 200) {
return data;
if (status === 200 && data.result.EvaluationResult) {
const tagMap: { [key: string]: RedeemerTagType } = {
spend: "SPEND",
mint: "MINT",
certificate: "CERT",
withdrawal: "REWARD",
};
const result: Omit<Action, "data">[] = [];

Object.keys(data.result.EvaluationResult).forEach((key) => {
const [tagKey, index] = key.split(":");
const { memory, steps } = data.result.EvaluationResult[key];
result.push({
tag: tagMap[tagKey!]!,
index: Number(index),
budget: { mem: memory, steps },
});
});

return result;
}

throw parseHttpError(data);
Expand Down

0 comments on commit 42cf3b5

Please sign in to comment.