Skip to content

Commit

Permalink
offchain for consuming Asteria & pellets
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscojoray committed Apr 25, 2024
1 parent 159f6c4 commit 6c1414b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
15 changes: 15 additions & 0 deletions offchain/tests/consume-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { admin_token } from "../constants.ts";
import { consumeAsteria } from "../transactions/consume-asteria.ts";
import { printTxURL } from "../utils.ts";

const asteria_tx_hash =
"6603802230bef801697be03e3773ca10bfd17bebb0a8e6b49661353996d255a8";
const asteria_tx_index = 0;

const txHash = await consumeAsteria(
admin_token,
asteria_tx_hash,
asteria_tx_index
);

printTxURL(txHash);
56 changes: 56 additions & 0 deletions offchain/transactions/consume-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
Data,
toUnit,
TxHash,
Constr,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../utils.ts";
import { AssetClassT } from "../types.ts";

async function consumeAsteria(
admin_token: AssetClassT,
asteria_tx_hash: TxHash,
asteria_tx_index: number
): Promise<TxHash> {
const lucid = await lucidBase();
const seed = Deno.env.get("SEED");
if (!seed) {
throw Error("Unable to read wallet's seed from env");
}
lucid.selectWalletFromSeed(seed);

const asteriaRefTxHash: { txHash: string } = JSON.parse(
await Deno.readTextFile("./script-refs/asteria-ref.json")
);
const asteriaRef = await fetchReferenceScript(lucid, asteriaRefTxHash.txHash);

const asteria: UTxO = (
await lucid.utxosByOutRef([
{
txHash: asteria_tx_hash,
outputIndex: asteria_tx_index,
},
])
)[0];
if (!asteria.datum) {
throw Error("Asteria datum not found");
}

const adminTokenUnit = toUnit(admin_token.policy, admin_token.name);

const consumeRedeemer = Data.to(new Constr(2, []));
const tx = await lucid
.newTx()
.readFrom([asteriaRef])
.collectFrom([asteria], consumeRedeemer)
.payToAddress(await lucid.wallet.address(), {
[adminTokenUnit]: BigInt(1),
})
.complete();

const signedTx = await tx.sign().complete();
return signedTx.submit();
}

export { consumeAsteria };

0 comments on commit 6c1414b

Please sign in to comment.