Skip to content

Commit

Permalink
53 implement admin operations for consuming pellets and asteria (#56)
Browse files Browse the repository at this point in the history
* onchain for consuming Asteria & pellets

* offchain for consuming Asteria

* Add consumePellet function

* Refactor consumeAsteria function to include adminUTxO parameter

* fixed network name.

* fixed deploy validator check.

* updated script refs.

* fixed script outputs offchain indexing.

---------

Co-authored-by: sofia-bobbiesi <sofiabobbiesi@gmail.com>
  • Loading branch information
franciscojoray and sofia-bobbiesi committed May 2, 2024
1 parent d5b4006 commit e21b964
Show file tree
Hide file tree
Showing 22 changed files with 628 additions and 72 deletions.
2 changes: 1 addition & 1 deletion offchain/script-refs/asteria-ref.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"txHash":"c2badd0df205f93fef630785163b86d6872c2190cf4dd7628a3934b89b56f9bb"}
{"txHash":"4e4d2261932fc8f0c2645c666afe8088fa1919f16394af751c17fd9b39c2064a"}
2 changes: 1 addition & 1 deletion offchain/script-refs/pellet-ref.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"txHash":"87f626782b975a1496d751746b2a78579f9378a4843e00e72c4e9a74f519f24f"}
{"txHash":"c7336f19dca642132eabab80fac385576aec37dc16811b0aaf1bbbe68bca76ee"}
2 changes: 1 addition & 1 deletion offchain/script-refs/spacetime-ref.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"txHash":"431aaa1871f6187d3f3ebebafd41ef17f712d3af54c0bba4ff780ed176179603"}
{"txHash":"1ba88f50f12a3415c4651d334a71f5520d8f9ba29037afa2e37b187d435cb28d"}
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 =
"b661437c389a4bd8db770145980030af22729f9c03be442329c60389e4168c81";
const asteria_tx_index = 0;

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

printTxURL(txHash);
9 changes: 9 additions & 0 deletions offchain/tests/consume-pellet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { admin_token } from "../constants.ts";
import { consumePellet } from "../transactions/consume-pellet.ts";
import { printTxURL } from "../utils.ts";

const pellet_tx_hash =
"65328c6dee12b38ec11049554ba91fa8da9b1f3471a849ef63141883cf2b27cf";

const txHash = await consumePellet(admin_token, pellet_tx_hash);
printTxURL(txHash);
6 changes: 3 additions & 3 deletions offchain/tests/create-ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
import { createShip } from "../transactions/create-ship.ts";
import { printTxURL } from "../utils.ts";

const pos_x = 7n;
const pos_y = -10n;
const pos_x = -7n;
const pos_y = 3n;
const asteria_tx_hash =
"2ad4ee7c7cd9156de39f33c5a1c31e701fae299366bf886c85bbb00ac84f210b";
"8884b2ccbb0d2cd5192459609f775f5b4e5681f5de8d6a4b6f9fc109ea601605";
const asteria_tx_index = 0;

const txHash = await createShip(
Expand Down
11 changes: 6 additions & 5 deletions offchain/tests/mine-asteria.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { admin_token } from "../constants.ts";
import { admin_token, max_asteria_mining } from "../constants.ts";
import { mineAsteria } from "../transactions/mine-asteria.ts";
import { printTxURL } from "../utils.ts";

const ship_tx_hash =
"40930f706ba2bb29be56326a3171ffa17e2d7f4e1d9cdeac347e05f3559efcd8";
"a528f7e89227b16d769e46b81fae20ba223b3f8fd23d82bb61aa6298f53eb5be";
const asteria_tx_hash =
"6ca378639a51b1283ec2c84deb772f62bff59603eca230aeb711fedee8389d69";
const max_asteria_mining = 50n;
"aff7cd18d9bf623cdf94544837a450323db2298913b7f6d353e51d9ac109a72a";
const asteria_tx_index = 1;

const txHash = await mineAsteria(
admin_token,
max_asteria_mining,
ship_tx_hash,
asteria_tx_hash,
max_asteria_mining
asteria_tx_index
);

printTxURL(txHash);
59 changes: 59 additions & 0 deletions offchain/transactions/consume-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 adminUTxO: UTxO = await lucid.wallet
.getUtxos()
.then((us) => us.filter((u) => u.assets[adminTokenUnit] >= 1n))
.then((us) => us[0]);

const consumeRedeemer = Data.to(new Constr(2, []));
const tx = await lucid
.newTx()
.readFrom([asteriaRef])
.collectFrom([asteria], consumeRedeemer)
.collectFrom([adminUTxO])
.complete();

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

export { consumeAsteria };
58 changes: 58 additions & 0 deletions offchain/transactions/consume-pellet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 consumePellet(
admin_token: AssetClassT,
pellet_tx_hash: TxHash
): 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/pellet-ref.json")
);
const asteriaRef = await fetchReferenceScript(lucid, asteriaRefTxHash.txHash);

const pellet: UTxO = (
await lucid.utxosByOutRef([
{
txHash: pellet_tx_hash,
outputIndex: 0,
},
])
)[0];
if (!pellet.datum) {
throw Error("Pellet datum not found");
}

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

const adminUTxO: UTxO = await lucid.wallet
.getUtxos()
.then((us) => us.filter((u) => u.assets[adminTokenUnit] >= 1n))
.then((us) => us[0]);

const consumeRedeemer = Data.to(new Constr(1, []));
const tx = await lucid
.newTx()
.readFrom([asteriaRef])
.collectFrom([pellet], consumeRedeemer)
.collectFrom([adminUTxO])
.complete();

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

export { consumePellet };
16 changes: 12 additions & 4 deletions offchain/transactions/deploy/spend-refs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Data, TxHash, toUnit } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import {
Data,
TxHash,
UTxO,
toUnit,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { buildDeployValidator } from "../../scripts/deploy.ts";
import { lucidBase } from "../../utils.ts";
import { AssetClassT } from "../../types.ts";
Expand All @@ -20,11 +25,14 @@ async function spendRefUTxOs(admin_token: AssetClassT): Promise<TxHash> {
}

const adminTokenUnit = toUnit(admin_token.policy, admin_token.name);
const adminUTxO: UTxO = await lucid.wallet
.getUtxos()
.then((us) => us.filter((u) => u.assets[adminTokenUnit] >= 1n))
.then((us) => us[0]);

const tx = await lucid
.newTx()
.payToAddress(await lucid.wallet.address(), {
[adminTokenUnit]: BigInt(1),
})
.collectFrom([adminUTxO])
.collectFrom(refUTxOs, Data.void())
.attachSpendingValidator(deployValidator)
.complete();
Expand Down
5 changes: 3 additions & 2 deletions offchain/transactions/mine-asteria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {

async function mineAsteria(
admin_token: AssetClassT,
max_asteria_mining: bigint,
ship_tx_hash: TxHash,
asteria_tx_hash: TxHash,
max_asteria_mining: bigint
asteria_tx_index: number
): Promise<TxHash> {
const lucid = await lucidBase();
const seed = Deno.env.get("SEED");
Expand Down Expand Up @@ -66,7 +67,7 @@ async function mineAsteria(
await lucid.utxosByOutRef([
{
txHash: asteria_tx_hash,
outputIndex: 1,
outputIndex: asteria_tx_index,
},
])
)[0];
Expand Down
2 changes: 1 addition & 1 deletion offchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const lucidBase = async (): Promise<Lucid> => {
"https://cardano-preview.blockfrost.io/api/v0",
Deno.env.get("BLOCKFROST_PROJECT_ID")
),
"Preprod"
"Preview"
);
return lucid;
};
Expand Down
2 changes: 2 additions & 0 deletions onchain/src/lib/asteria/test_mock.ak
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub const asteria_credential = #"2222"

pub const pilot_credential = #"3333"

pub const admin_credential = #"4444"

pub const ship_token_name = "SHIP7"

pub const pilot_token_name = "PILOT7"
2 changes: 2 additions & 0 deletions onchain/src/lib/asteria/types.ak
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub type PelletDatum {

pub type PelletRedeemer {
Provide { amount: Int }
ConsumePellet
}

pub type AsteriaDatum {
Expand All @@ -44,6 +45,7 @@ pub type AsteriaDatum {
pub type AsteriaRedeemer {
AddNewShip
Mine
ConsumeAsteria
}

pub type ShipyardRedeemer {
Expand Down
10 changes: 10 additions & 0 deletions onchain/src/lib/asteria/utils.ak
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ use aiken/dict.{keys}
use aiken/list.{any}
use aiken/math
use aiken/transaction.{Output}
use aiken/transaction/credential.{
Address, ScriptCredential, VerificationKeyCredential,
}
use aiken/transaction/value.{AssetName, PolicyId, tokens}

pub fn is_ship_token_in_utxo(utxo: Output, shipyard_policy: PolicyId) -> Bool {
Expand Down Expand Up @@ -30,3 +33,10 @@ pub fn distance(delta_x: Int, delta_y: Int) -> Int {
pub fn required_fuel(distance: Int, fuel_per_step: Int) -> Int {
distance * fuel_per_step
}

pub fn is_wallet_address(address: Address) -> Bool {
when address.payment_credential is {
VerificationKeyCredential(_) -> True
ScriptCredential(_) -> False
}
}

0 comments on commit e21b964

Please sign in to comment.