Skip to content

Commit

Permalink
creating/consuming multiple pellets & refactored tx dir. (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscojoray committed May 3, 2024
1 parent e21b964 commit ae41bcb
Show file tree
Hide file tree
Showing 31 changed files with 150 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { admin_token } from "../constants.ts";
import { consumeAsteria } from "../transactions/consume-asteria.ts";
import { printTxURL } from "../utils.ts";
import { admin_token } from "../../constants.ts";
import { consumeAsteria } from "../../transactions/admin/consume-asteria.ts";
import { printTxURL } from "../../utils.ts";

const asteria_tx_hash =
"b661437c389a4bd8db770145980030af22729f9c03be442329c60389e4168c81";
Expand Down
14 changes: 14 additions & 0 deletions offchain/tests/admin/consume-pellets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { admin_token } from "../../constants.ts";
import { consumePellets } from "../../transactions/admin/consume-pellets.ts";
import { printTxURL } from "../../utils.ts";

const pellets_tx_hash =
"fb1a0a14864dc4ad49f53fe2e5416e2f9c8fac14734fe6ad4812b4d977237e11";
const pellets_tx_indexes = [0, 1, 2];

const txHash = await consumePellets(
admin_token,
pellets_tx_hash,
pellets_tx_indexes
);
printTxURL(txHash);
7 changes: 7 additions & 0 deletions offchain/tests/admin/create-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { admin_token } from "../../constants.ts";
import { createAsteria } from "../../transactions/admin/create-asteria.ts";
import { printTxURL } from "../../utils.ts";

const txHash = await createAsteria(admin_token);

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

const params = [
{
fuel: 90n,
pos_x: 12n,
pos_y: -50n,
},
{
fuel: 40n,
pos_x: -7n,
pos_y: 3n,
},
{
fuel: 25n,
pos_x: 20n,
pos_y: 19n,
},
];

const txHash = await createPellets(admin_token, params);
printTxURL(txHash);
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { deployAsteria } from "../../transactions/deploy/deploy-asteria.ts";
import { deployAsteria } from "../../../transactions/admin/deploy/deploy-asteria.ts";
import {
admin_token,
max_asteria_mining,
ship_mint_lovelace_fee,
} from "../../constants.ts";
import { printTxURL } from "../../utils.ts";
} from "../../../constants.ts";
import { printTxURL } from "../../../utils.ts";

const txHash = await deployAsteria(
admin_token,
Expand Down
7 changes: 7 additions & 0 deletions offchain/tests/admin/deploy/deploy-pellet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { deployPellet } from "../../../transactions/admin/deploy/deploy-pellet.ts";
import { admin_token } from "../../../constants.ts";
import { printTxURL } from "../../../utils.ts";

const txHash = await deployPellet(admin_token);

printTxURL(txHash);
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { deploySpacetime } from "../../transactions/deploy/deploy-spacetime.ts";
import { deploySpacetime } from "../../../transactions/admin/deploy/deploy-spacetime.ts";
import {
admin_token,
max_moving_distance,
max_ship_fuel,
fuel_per_step,
initial_fuel,
min_asteria_distance,
} from "../../constants.ts";
import { printTxURL } from "../../utils.ts";
} from "../../../constants.ts";
import { printTxURL } from "../../../utils.ts";

const txHash = await deploySpacetime(
admin_token,
Expand Down
7 changes: 7 additions & 0 deletions offchain/tests/admin/deploy/spend-refs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { spendRefUTxOs } from "../../../transactions/admin/deploy/spend-refs.ts";
import { admin_token } from "../../../constants.ts";
import { printTxURL } from "../../../utils.ts";

const txHash = await spendRefUTxOs(admin_token);

printTxURL(txHash);
9 changes: 0 additions & 9 deletions offchain/tests/consume-pellet.ts

This file was deleted.

7 changes: 0 additions & 7 deletions offchain/tests/create-asteria.ts

This file was deleted.

10 changes: 0 additions & 10 deletions offchain/tests/create-pellet.ts

This file was deleted.

7 changes: 0 additions & 7 deletions offchain/tests/deploy/deploy-pellet.ts

This file was deleted.

7 changes: 0 additions & 7 deletions offchain/tests/deploy/spend-refs.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
admin_token,
initial_fuel,
ship_mint_lovelace_fee,
} from "../constants.ts";
import { createShip } from "../transactions/create-ship.ts";
import { printTxURL } from "../utils.ts";
} from "../../constants.ts";
import { createShip } from "../../transactions/user/create-ship.ts";
import { printTxURL } from "../../utils.ts";

const pos_x = -7n;
const pos_y = 3n;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { admin_token } from "../constants.ts";
import { gatherFuel } from "../transactions/gather-fuel.ts";
import { printTxURL } from "../utils.ts";
import { admin_token } from "../../constants.ts";
import { gatherFuel } from "../../transactions/user/gather-fuel.ts";
import { printTxURL } from "../../utils.ts";

const gather_amount = 20n;
const ship_tx_hash =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { admin_token, max_asteria_mining } from "../constants.ts";
import { mineAsteria } from "../transactions/mine-asteria.ts";
import { printTxURL } from "../utils.ts";
import { admin_token, max_asteria_mining } from "../../constants.ts";
import { mineAsteria } from "../../transactions/user/mine-asteria.ts";
import { printTxURL } from "../../utils.ts";

const ship_tx_hash =
"a528f7e89227b16d769e46b81fae20ba223b3f8fd23d82bb61aa6298f53eb5be";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fuel_per_step } from "../constants.ts";
import { moveShip } from "../transactions/move-ship.ts";
import { printTxURL } from "../utils.ts";
import { fuel_per_step } from "../../constants.ts";
import { moveShip } from "../../transactions/user/move-ship.ts";
import { printTxURL } from "../../utils.ts";

const delta_x = -7n;
const delta_y = 10n;
Expand Down
4 changes: 2 additions & 2 deletions offchain/tests/quit.ts → offchain/tests/user/quit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { quit } from "../transactions/quit.ts";
import { printTxURL } from "../utils.ts";
import { quit } from "../../transactions/user/quit.ts";
import { printTxURL } from "../../utils.ts";

const ship_tx_hash =
"85568f8fc35a103b6f5753a2634631477769f3bc9ae8ea7c60dd7f7c2f342cf8";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
Constr,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../utils.ts";
import { AssetClassT } from "../types.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT } from "../../types.ts";

async function consumeAsteria(
admin_token: AssetClassT,
Expand All @@ -33,12 +33,8 @@ async function consumeAsteria(
},
])
)[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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {
Constr,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../utils.ts";
import { AssetClassT } from "../types.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT } from "../../types.ts";

async function consumePellet(
async function consumePellets(
admin_token: AssetClassT,
pellet_tx_hash: TxHash
pellets_tx_hash: TxHash,
pellets_tx_indexes: number[]
): Promise<TxHash> {
const lucid = await lucidBase();
const seed = Deno.env.get("SEED");
Expand All @@ -19,25 +20,19 @@ async function consumePellet(
}
lucid.selectWalletFromSeed(seed);

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

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

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))
Expand All @@ -46,13 +41,13 @@ async function consumePellet(
const consumeRedeemer = Data.to(new Constr(1, []));
const tx = await lucid
.newTx()
.readFrom([asteriaRef])
.collectFrom([pellet], consumeRedeemer)
.readFrom([pelletRef])
.collectFrom(pellets, consumeRedeemer)
.collectFrom([adminUTxO])
.complete();

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

export { consumePellet };
export { consumePellets };
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
toUnit,
TxHash,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../utils.ts";
import { AssetClassT, AsteriaDatum, AsteriaDatumT } from "../types.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT, AsteriaDatum, AsteriaDatumT } from "../../types.ts";

async function createAsteria(admin_token: AssetClassT): Promise<TxHash> {
const lucid = await lucidBase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import {
toUnit,
TxHash,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../utils.ts";
import { AssetClassT, PelletDatum, PelletDatumT } from "../types.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT, PelletDatum, PelletDatumT } from "../../types.ts";

async function createPellet(
async function createPellets(
admin_token: AssetClassT,
fuel: bigint,
pos_x: bigint,
pos_y: bigint
params: { fuel: bigint; pos_x: bigint; pos_y: bigint }[]
): Promise<TxHash> {
const lucid = await lucidBase();
const seed = Deno.env.get("SEED");
Expand All @@ -37,34 +35,33 @@ async function createPellet(
const spacetimeValidator = spacetimeRef.scriptRef as Script;
const shipyardPolicyId = lucid.utils.mintingPolicyToId(spacetimeValidator);

const pelletInfo = {
fuel: fuel,
pos_x: pos_x,
pos_y: pos_y,
shipyard_policy: shipyardPolicyId,
};

const pelletDatum = Data.to<PelletDatumT>(
pelletInfo,
PelletDatum as unknown as PelletDatumT
);

const adminTokenUnit = toUnit(admin_token.policy, admin_token.name);
let tx = await lucid.newTx();

const tx = await lucid
.newTx()
.payToContract(
for (const pellet of params) {
const pelletInfo = {
fuel: pellet.fuel,
pos_x: pellet.pos_x,
pos_y: pellet.pos_y,
shipyard_policy: shipyardPolicyId,
};
const pelletDatum = Data.to<PelletDatumT>(
pelletInfo,
PelletDatum as unknown as PelletDatumT
);

tx = tx.payToContract(
pelletAddressBech32,
{ inline: pelletDatum },
{
[adminTokenUnit]: BigInt(1),
lovelace: 2_000_000n,
}
)
.complete();

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

export { createPellet };
export { createPellets };
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Data, TxHash } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { buildAsteriaValidator } from "../../scripts/asteria.ts";
import { lucidBase, writeJson } from "../../utils.ts";
import { AssetClassT } from "../../types.ts";
import { buildDeployValidator } from "../../scripts/deploy.ts";
import { buildAsteriaValidator } from "../../../scripts/asteria.ts";
import { lucidBase, writeJson } from "../../../utils.ts";
import { AssetClassT } from "../../../types.ts";
import { buildDeployValidator } from "../../../scripts/deploy.ts";

async function deployAsteria(
admin_token: AssetClassT,
Expand Down

0 comments on commit ae41bcb

Please sign in to comment.