Skip to content

Commit

Permalink
44 implement lucid offchain tx building for gathering fuel and mining…
Browse files Browse the repository at this point in the history
… asteria (#47)

* scripts for gathering fuel and mining asteria.

Co-authored-by: Sofia Bobbiesi <sofia-bobbiesi@users.noreply.github.com>

* fixed error in pellet validator.

* added script refs directory.

* fixed onchain rewards check.

Co-authored-by: Sofia Bobbiesi <sofia-bobbiesi@users.noreply.github.com>

* fixed shipyard policy burn checks.

* script for quitting game.

* refactored offchain scripts.

Co-authored-by: Sofia Bobbiesi <sofia-bobbiesi@users.noreply.github.com>

* added `fetchReferenceScript` function.

* added `constants` file.

Co-authored-by: Sofia Bobbiesi <sofia-bobbiesi@users.noreply.github.com>

* pretty print of tx urls.

Co-authored-by: Sofia Bobbiesi <sofia-bobbiesi@users.noreply.github.com>

* updated params.

---------

Co-authored-by: Sofia Bobbiesi <sofia-bobbiesi@users.noreply.github.com>
  • Loading branch information
franciscojoray and sofia-bobbiesi authored Apr 25, 2024
1 parent ab01df0 commit d5b4006
Show file tree
Hide file tree
Showing 36 changed files with 608 additions and 215 deletions.
1 change: 0 additions & 1 deletion offchain/asteria-ref.json

This file was deleted.

25 changes: 25 additions & 0 deletions offchain/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { AssetClassT } from "./types.ts";

const admin_token: AssetClassT = {
policy: "516238dd0a79bac4bebe041c44bad8bf880d74720733d2fc0d255d28",
name: fromText("asteriaAdmin"),
};
const ship_mint_lovelace_fee = 20_000_000n;
const max_asteria_mining = 100n;
const max_moving_distance = 5n;
const max_ship_fuel = 30n;
const fuel_per_step = 1n;
const initial_fuel = max_ship_fuel;
const min_asteria_distance = 50n;

export {
admin_token,
ship_mint_lovelace_fee,
max_asteria_mining,
max_moving_distance,
max_ship_fuel,
fuel_per_step,
initial_fuel,
min_asteria_distance,
};
1 change: 0 additions & 1 deletion offchain/pellet-ref.json

This file was deleted.

1 change: 1 addition & 0 deletions offchain/script-refs/asteria-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txHash":"c2badd0df205f93fef630785163b86d6872c2190cf4dd7628a3934b89b56f9bb"}
1 change: 1 addition & 0 deletions offchain/script-refs/pellet-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txHash":"87f626782b975a1496d751746b2a78579f9378a4843e00e72c4e9a74f519f24f"}
1 change: 1 addition & 0 deletions offchain/script-refs/spacetime-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txHash":"431aaa1871f6187d3f3ebebafd41ef17f712d3af54c0bba4ff780ed176179603"}
1 change: 0 additions & 1 deletion offchain/spacetime-ref.json

This file was deleted.

11 changes: 3 additions & 8 deletions offchain/tests/create-asteria.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { admin_token } from "../constants.ts";
import { createAsteria } from "../transactions/create-asteria.ts";
import { AssetClassT } from "../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
import { printTxURL } from "../utils.ts";

const txHash = await createAsteria(admin_token);

console.log(txHash);
printTxURL(txHash);
17 changes: 6 additions & 11 deletions offchain/tests/create-pellet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { AssetClassT } from "../types.ts";
import { admin_token } from "../constants.ts";
import { createPellet } from "../transactions/create-pellet.ts";
import { printTxURL } from "../utils.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
const fuel = 40n;
const pos_x = 7n;
const pos_y = -10n;

const fuel = 90n;
const pos_x = 12n;
const pos_y = -50n;
const txHash = await createPellet(admin_token, fuel, pos_x, pos_y);

console.log(txHash);
printTxURL(txHash);
27 changes: 15 additions & 12 deletions offchain/tests/create-ship.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { AssetClassT } from "../types.ts";
import {
admin_token,
initial_fuel,
ship_mint_lovelace_fee,
} from "../constants.ts";
import { createShip } from "../transactions/create-ship.ts";
import { printTxURL } from "../utils.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
const ship_mint_lovelace_fee = 3000n;
const initial_fuel = 15n;
const pos_x = 20n;
const pos_y = -13n;
const pos_x = 7n;
const pos_y = -10n;
const asteria_tx_hash =
"2ad4ee7c7cd9156de39f33c5a1c31e701fae299366bf886c85bbb00ac84f210b";
const asteria_tx_index = 0;

const txHash = await createShip(
admin_token,
ship_mint_lovelace_fee,
initial_fuel,
pos_x,
pos_y
pos_y,
asteria_tx_hash,
asteria_tx_index
);

console.log(txHash);
printTxURL(txHash);
17 changes: 7 additions & 10 deletions offchain/tests/deploy/deploy-asteria.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { deployAsteria } from "../../transactions/deploy/deploy-asteria.ts";
import { AssetClassT } from "../../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
const ship_mint_lovelace_fee = 3000n;
const max_asteria_mining = 50n;
import {
admin_token,
max_asteria_mining,
ship_mint_lovelace_fee,
} from "../../constants.ts";
import { printTxURL } from "../../utils.ts";

const txHash = await deployAsteria(
admin_token,
ship_mint_lovelace_fee,
max_asteria_mining
);

console.log(txHash);
printTxURL(txHash);
11 changes: 3 additions & 8 deletions offchain/tests/deploy/deploy-pellet.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { deployPellet } from "../../transactions/deploy/deploy-pellet.ts";
import { AssetClassT } from "../../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
import { admin_token } from "../../constants.ts";
import { printTxURL } from "../../utils.ts";

const txHash = await deployPellet(admin_token);

console.log(txHash);
printTxURL(txHash);
23 changes: 10 additions & 13 deletions offchain/tests/deploy/deploy-spacetime.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { deploySpacetime } from "../../transactions/deploy/deploy-spacetime.ts";
import { AssetClassT } from "../../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
const max_moving_distance = 20n;
const max_ship_fuel = 100n;
const fuel_per_step = 1n;
const initial_fuel = 15n;
const min_asteria_distance = 10n;
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";

const txHash = await deploySpacetime(
admin_token,
Expand All @@ -21,4 +18,4 @@ const txHash = await deploySpacetime(
min_asteria_distance
);

console.log(txHash);
printTxURL(txHash);
12 changes: 4 additions & 8 deletions offchain/tests/deploy/spend-refs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { spendRefUTxOs } from "../../transactions/deploy/spend-refs.ts";
import { AssetClassT } from "../../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};
import { admin_token } from "../../constants.ts";
import { printTxURL } from "../../utils.ts";

const txHash = await spendRefUTxOs(admin_token);
console.log(txHash);

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

const gather_amount = 20n;
const ship_tx_hash =
"6ca378639a51b1283ec2c84deb772f62bff59603eca230aeb711fedee8389d69";
const pellet_tx_hash =
"b1d5dedc6c9ba333d8eab4c9f05edda4563fc313fc9b124393ec3b64e3676fb0";

const txHash = await gatherFuel(
admin_token,
gather_amount,
ship_tx_hash,
pellet_tx_hash
);

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

const ship_tx_hash =
"40930f706ba2bb29be56326a3171ffa17e2d7f4e1d9cdeac347e05f3559efcd8";
const asteria_tx_hash =
"6ca378639a51b1283ec2c84deb772f62bff59603eca230aeb711fedee8389d69";
const max_asteria_mining = 50n;

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

printTxURL(txHash);
25 changes: 8 additions & 17 deletions offchain/tests/move-ship.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { fromText } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fuel_per_step } from "../constants.ts";
import { moveShip } from "../transactions/move-ship.ts";
import { printTxURL } from "../utils.ts";

const fuel_per_step = 1n;
const delta_x = 2n;
const delta_y = -3n;
const ship_token_name = fromText("SHIP0");
const pilot_token_name = fromText("PILOT0");
const shipTxHash =
"ca30a841e582f284b7b71d39c2cbc8b9a7fca9476d921b6b9d97c0c7e31bf2bc";
const delta_x = -7n;
const delta_y = 10n;
const ship_tx_hash =
"85568f8fc35a103b6f5753a2634631477769f3bc9ae8ea7c60dd7f7c2f342cf8";

const txHash = await moveShip(
fuel_per_step,
delta_x,
delta_y,
ship_token_name,
pilot_token_name,
shipTxHash
);
const txHash = await moveShip(fuel_per_step, delta_x, delta_y, ship_tx_hash);

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

const ship_tx_hash =
"85568f8fc35a103b6f5753a2634631477769f3bc9ae8ea7c60dd7f7c2f342cf8";

const txHash = await quit(ship_tx_hash);

printTxURL(txHash);
25 changes: 11 additions & 14 deletions offchain/transactions/create-asteria.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Data, toUnit, TxHash } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { lucidBase } from "../utils.ts";
import {
Data,
Script,
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";

async function createAsteria(admin_token: AssetClassT): Promise<TxHash> {
Expand All @@ -11,22 +16,14 @@ async function createAsteria(admin_token: AssetClassT): Promise<TxHash> {
lucid.selectWalletFromSeed(seed);

const asteriaRefTxHash: { txHash: string } = JSON.parse(
await Deno.readTextFile("./asteria-ref.json")
await Deno.readTextFile("./script-refs/asteria-ref.json")
);
const asteriaRef = await lucid.utxosByOutRef([
{
txHash: asteriaRefTxHash.txHash,
outputIndex: 0,
},
]);
const asteriaValidator = asteriaRef[0].scriptRef;
if (!asteriaValidator) {
throw Error("Could not read Asteria validator from ref UTxO");
}
const asteriaRef = await fetchReferenceScript(lucid, asteriaRefTxHash.txHash);
const asteriaValidator = asteriaRef.scriptRef as Script;
const asteriaAddressBech32 = lucid.utils.validatorToAddress(asteriaValidator);

const spacetimeRefTxHash: { txHash: string } = JSON.parse(
await Deno.readTextFile("./spacetime-ref.json")
await Deno.readTextFile("./script-refs/spacetime-ref.json")
);
const spacetimeRef = await lucid.utxosByOutRef([
{
Expand Down
40 changes: 16 additions & 24 deletions offchain/transactions/create-pellet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Data, toUnit, TxHash } from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { lucidBase } from "../utils.ts";
import {
Data,
Script,
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";

async function createPellet(
Expand All @@ -16,33 +21,20 @@ async function createPellet(
lucid.selectWalletFromSeed(seed);

const pelletRefTxHash: { txHash: string } = JSON.parse(
await Deno.readTextFile("./pellet-ref.json")
await Deno.readTextFile("./script-refs/pellet-ref.json")
);
const pelletRef = await lucid.utxosByOutRef([
{
txHash: pelletRefTxHash.txHash,
outputIndex: 0,
},
]);
const pelletValidator = pelletRef[0].scriptRef;
if (!pelletValidator) {
throw Error("Could not read pellet validator from ref UTxO");
}
const pelletRef = await fetchReferenceScript(lucid, pelletRefTxHash.txHash);
const pelletValidator = pelletRef.scriptRef as Script;
const pelletAddressBech32 = lucid.utils.validatorToAddress(pelletValidator);

const spacetimeRefTxHash: { txHash: string } = JSON.parse(
await Deno.readTextFile("./spacetime-ref.json")
await Deno.readTextFile("./script-refs/spacetime-ref.json")
);
const spacetimeRef = await lucid.utxosByOutRef([
{
txHash: spacetimeRefTxHash.txHash,
outputIndex: 0,
},
]);
const spacetimeValidator = spacetimeRef[0].scriptRef;
if (!spacetimeValidator) {
throw Error("Could not read pellet validator from ref UTxO");
}
const spacetimeRef = await fetchReferenceScript(
lucid,
spacetimeRefTxHash.txHash
);
const spacetimeValidator = spacetimeRef.scriptRef as Script;
const shipyardPolicyId = lucid.utils.mintingPolicyToId(spacetimeValidator);

const pelletInfo = {
Expand Down
Loading

0 comments on commit d5b4006

Please sign in to comment.