From f0854dcf2fcf27bb452238bb46050379f766f876 Mon Sep 17 00:00:00 2001 From: Jay Taylor Date: Wed, 24 Jul 2024 18:16:09 +1000 Subject: [PATCH] feat: Blockfrost provider returns UTxOs with datums and script refs (#91) --- .changeset/sour-panthers-double.md | 5 + examples/blockfrost/index.js | 18 ++++ packages/blaze-query/src/blockfrost.ts | 143 +++++++++++++++++++------ 3 files changed, 133 insertions(+), 33 deletions(-) create mode 100644 .changeset/sour-panthers-double.md diff --git a/.changeset/sour-panthers-double.md b/.changeset/sour-panthers-double.md new file mode 100644 index 0000000..8bbfa7c --- /dev/null +++ b/.changeset/sour-panthers-double.md @@ -0,0 +1,5 @@ +--- +"@blaze-cardano/query": patch +--- + +feat: Blockfrost provider returns UTxOs with datums and script refs diff --git a/examples/blockfrost/index.js b/examples/blockfrost/index.js index 68b7939..ab493a6 100644 --- a/examples/blockfrost/index.js +++ b/examples/blockfrost/index.js @@ -92,6 +92,14 @@ const txIns = [ "74baf638a18a6ab54798cac310112af61cb1f1d6eacb8c893a10b6877cf71a8d", 1, ), + new TransactionInput( + "036ed48c89169a9c4e475e08a6d22ea1e09ba8a6a6cfbabfa4f1deefd269652c", + 0, + ), + new TransactionInput( + "ad54aa407df81404ab74343a4fca56e51c4ab488c54f12c4bc43e8b093c976a5", + 1, + ), ]; const resolvedOutputs = await provider.resolveUnspentOutputs(txIns); @@ -110,6 +118,16 @@ for (const utxo of resolvedOutputs) { console.log(`Amount of ${assetName}: ${amount}`); } + + if (utxo.output().datum()) { + console.log("Datum:"); + console.log(utxo.output().datum().toCore()); + } + + if (utxo.output().scriptRef()) { + console.log("Reference Script:"); + console.log(utxo.output().scriptRef().hash()); + } } const datumHash = diff --git a/packages/blaze-query/src/blockfrost.ts b/packages/blaze-query/src/blockfrost.ts index d0efe5a..753a86b 100644 --- a/packages/blaze-query/src/blockfrost.ts +++ b/packages/blaze-query/src/blockfrost.ts @@ -2,9 +2,9 @@ import type { AssetId as AssetIdType, CostModels, Credential, - DatumHash, ProtocolParameters, Redeemer, + ScriptHash, TokenMap, Transaction, } from "@blaze-cardano/core"; @@ -12,12 +12,18 @@ import { Address, AddressType, AssetId, + Datum, + DatumHash, ExUnits, fromHex, + Hash28ByteBase16, HexBlob, PlutusData, + PlutusV1Script, + PlutusV2Script, Redeemers, RedeemerTag, + Script, TransactionId, TransactionInput, TransactionOutput, @@ -147,7 +153,7 @@ export class Blockfrost implements Provider { paymentPart: address.toCore(), }).toBech32(); - const buildTxUnspentOutput = buildTransactionUnspentOutput( + const buildTxUnspentOutput = this.buildTransactionUnspentOutput( Address.fromBech32(bech32), ); @@ -173,7 +179,7 @@ export class Blockfrost implements Provider { } for (const blockfrostUTxO of response) { - results.add(buildTxUnspentOutput(blockfrostUTxO)); + results.add(await buildTxUnspentOutput(blockfrostUTxO)); } if (response.length < maxPageCount) { @@ -212,7 +218,7 @@ export class Blockfrost implements Provider { paymentPart: address.toCore(), }).toBech32(); - const buildTxUnspentOutput = buildTransactionUnspentOutput( + const buildTxUnspentOutput = this.buildTransactionUnspentOutput( Address.fromBech32(bech32), ); @@ -242,7 +248,7 @@ export class Blockfrost implements Provider { } for (const blockfrostUTxO of response) { - results.add(buildTxUnspentOutput(blockfrostUTxO)); + results.add(await buildTxUnspentOutput(blockfrostUTxO)); } if (response.length < maxPageCount) { @@ -349,11 +355,11 @@ export class Blockfrost implements Provider { // Blockfrost API does not return tx hash, so it must be manually set blockfrostUTxO.tx_hash = txIn.transactionId(); - const buildTxUnspentOutput = buildTransactionUnspentOutput( + const buildTxUnspentOutput = this.buildTransactionUnspentOutput( Address.fromBech32(blockfrostUTxO.address), ); - results.add(buildTxUnspentOutput(blockfrostUTxO)); + results.add(await buildTxUnspentOutput(blockfrostUTxO)); } } @@ -584,6 +590,103 @@ export class Blockfrost implements Provider { Array.from(evaledRedeemers).map((x) => x.toCore()), ); } + + private async getScriptRef(scriptHash: ScriptHash): Promise