Skip to content

Commit 9acbb13

Browse files
committed
feat: evaluateTx
1 parent 6069a71 commit 9acbb13

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

packages/mesh-provider/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"dependencies": {
3838
"@meshsdk/common": "1.9.0-beta.43",
3939
"@meshsdk/core-cst": "1.9.0-beta.43",
40-
"@utxorpc/sdk": "0.6.2",
41-
"@utxorpc/spec": "0.10.1",
40+
"@utxorpc/sdk": "^0.6.7",
41+
"@utxorpc/spec": "^0.16.0",
4242
"axios": "^1.7.2"
4343
},
4444
"prettier": "@meshsdk/configs/prettier",

packages/mesh-provider/src/utxo-rpc.ts

+41-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
IListener,
1919
ISubmitter,
2020
Protocol,
21+
RedeemerTagType,
2122
toBytes,
2223
TransactionInfo,
2324
UTxO,
@@ -115,8 +116,7 @@ import { utxosToAssets } from "./common/utxos-to-assets";
115116
* ```
116117
*/
117118
export class U5CProvider
118-
implements IFetcher, ISubmitter, IEvaluator, IListener
119-
{
119+
implements IFetcher, ISubmitter, IEvaluator, IListener {
120120
// Clients for querying and submitting transactions on the Cardano blockchain.
121121
private queryClient: CardanoQueryClient;
122122
private submitClient: CardanoSubmitClient;
@@ -176,8 +176,33 @@ export class U5CProvider
176176
* Evaluates the resources required to execute the transaction
177177
* @param tx - The transaction to evaluate
178178
*/
179-
evaluateTx(tx: string): Promise<Omit<Action, "data">[]> {
180-
throw new Error("Method not implemented.");
179+
async evaluateTx(tx: string): Promise<Omit<Action, "data">[]> {
180+
181+
const report = await this.submitClient.evalTx(hexToBytes(tx));
182+
const evalResult = report.report[0]!.chain.value?.redeemers!;
183+
184+
const tagMap: { [key: number]: RedeemerTagType } = {
185+
// 0: "UNSPECIFIED", // REDEEMER_PURPOSE_UNSPECIFIED
186+
1: "SPEND", // REDEEMER_PURPOSE_SPEND
187+
2: "MINT", // REDEEMER_PURPOSE_MINT
188+
3: "CERT", // REDEEMER_PURPOSE_CERT
189+
4: "REWARD", // REDEEMER_PURPOSE_REWARD
190+
5: "VOTE", // REDEEMER_PURPOSE_VOTE
191+
6: "PROPOSE", // REDEEMER_PURPOSE_PROPOSE
192+
};
193+
194+
const result: Omit<Action, "data">[] = [];
195+
196+
evalResult.map((action: any) => {
197+
result.push({
198+
tag: tagMap[action.purpose!]!,
199+
index: action.index,
200+
budget: { mem: Number(action.exUnits!.memory), steps: Number(action.exUnits!.steps) },
201+
});
202+
})
203+
204+
return result;
205+
181206
}
182207

183208
/**
@@ -213,7 +238,7 @@ export class U5CProvider
213238
* @returns UTxOs for the given address
214239
*/
215240
async fetchAddressUTxOs(address: string, asset?: string): Promise<UTxO[]> {
216-
const addressBytes = Buffer.from(Address.fromBech32(address).toBytes());
241+
const addressBytes = hexToBytes(Address.fromBech32(address).toBytes());
217242

218243
const utxoSearchResult =
219244
await this.queryClient.searchUtxosByAddress(addressBytes);
@@ -344,13 +369,21 @@ export class U5CProvider
344369
}
345370

346371
/**
347-
* Unimplemented - open for contribution
372+
* Not complete - open for contribution
348373
*
349374
* Fetches output UTxOs of a given transaction hash.
350375
* @param hash - The transaction hash
351376
*/
352-
fetchUTxOs(hash: string): Promise<UTxO[]> {
353-
throw new Error("Method not implemented.");
377+
async fetchUTxOs(hash: string, index?: number): Promise<UTxO[]> {
378+
const utxoSearchResult = await this.queryClient.readUtxosByOutputRef(
379+
[{
380+
txHash: hexToBytes(hash),
381+
outputIndex: index || 0, // TODO: handle case when index is not provided. Note: readUtxos might not support this, try when readTx is implemented
382+
}]
383+
);
384+
return utxoSearchResult.map((item) => {
385+
return this._rpcUtxoToMeshUtxo(item.txoRef, item.parsedValued!);
386+
});
354387
}
355388

356389
/**

0 commit comments

Comments
 (0)