Skip to content

Commit

Permalink
20 implement lucid offchain tx building for grid creation 1 (#42)
Browse files Browse the repository at this point in the history
* script for creating Asteria.

* script for creating pellet.

* create ship: minting tokens.

Co-authored-by: Sofia Bobbiesi <[email protected]>

* Using reference scripts.

Co-authored-by: Sofia Bobbiesi <[email protected]>

* script for ship movement.

* separated minAda from Asteria rewards.

* Joined spacetime validator & shipyard policy as multivalidator.

Co-authored-by: Sofia Bobbiesi <[email protected]>

* deploy validator checking admin token.

* script for spending deployed script UTxOs.

* fixed minAda.

* moving ship by txHash.


---------

Co-authored-by: Sofia Bobbiesi <[email protected]>
  • Loading branch information
franciscojoray and sofia-bobbiesi authored Apr 19, 2024
1 parent 48ccb0a commit 1291e39
Show file tree
Hide file tree
Showing 37 changed files with 1,369 additions and 363 deletions.
1 change: 1 addition & 0 deletions offchain/asteria-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txHash":"48eefd160b9114e80dea85404446947e5e15d19e96914ce1182b05ee7a36fd98"}
1 change: 1 addition & 0 deletions offchain/pellet-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txHash":"eb2f88f353163aeb0abcafa040b6e6d8f64ea39c5064134061b44fd3bedb23a9"}
44 changes: 44 additions & 0 deletions offchain/scripts/asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Data,
SpendingValidator,
applyParamsToScript,
} from "https://deno.land/x/[email protected]/mod.ts";
import plutusBlueprint from "../../onchain/src/plutus.json" with { type: "json" };
import { AssetClass, AssetClassT } from "../types.ts";

const asteriaValidator = plutusBlueprint.validators.find(
({ title }) => title === "asteria.spend"
);

if (!asteriaValidator) {
throw new Error("Asteria validator indexed with 'asteria.spend' failed!");
}

const ASTERIA_SCRIPT: SpendingValidator["script"] =
asteriaValidator.compiledCode;

const ValidatorParam = Data.Tuple([
AssetClass,
Data.Integer({ minimum: 0 }),
Data.Integer({ minimum: 0, maximum: 100 }),
]);
type ValidatorParamT = Data.Static<typeof ValidatorParam>;

function buildAsteriaValidator(
admin_token: AssetClassT,
ship_mint_lovelace_fee: bigint,
max_asteria_mining: bigint
): SpendingValidator {
const appliedValidator = applyParamsToScript<ValidatorParamT>(
ASTERIA_SCRIPT,
[admin_token, ship_mint_lovelace_fee, max_asteria_mining],
ValidatorParam as unknown as ValidatorParamT
);

return {
type: "PlutusV2",
script: appliedValidator,
};
}

export { buildAsteriaValidator };
37 changes: 37 additions & 0 deletions offchain/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
Data,
SpendingValidator,
applyParamsToScript,
} from "https://deno.land/x/[email protected]/mod.ts";
import plutusBlueprint from "../../onchain/src/plutus.json" with { type: "json" };
import { AssetClass, AssetClassT } from "../types.ts";

const deployValidator = plutusBlueprint.validators.find(
({ title }) => title === "deploy.spend"
);

if (!deployValidator) {
throw new Error("Deploy validator indexed with 'deploy.spend' failed!");
}

const DEPLOY_SCRIPT: SpendingValidator["script"] =
deployValidator.compiledCode;


const ValidatorParam = Data.Tuple([AssetClass]);
type ValidatorParamT = Data.Static<typeof ValidatorParam>;

function buildDeployValidator(admin_token: AssetClassT): SpendingValidator {
const appliedValidator = applyParamsToScript<ValidatorParamT>(
DEPLOY_SCRIPT,
[admin_token],
ValidatorParam as unknown as ValidatorParamT
);

return {
type: "PlutusV2",
script: appliedValidator,
};
}

export { buildDeployValidator };
35 changes: 35 additions & 0 deletions offchain/scripts/pellet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
Data,
SpendingValidator,
applyParamsToScript,
} from "https://deno.land/x/[email protected]/mod.ts";
import plutusBlueprint from "../../onchain/src/plutus.json" with { type: "json" };
import { AssetClass, AssetClassT } from "../types.ts";

const pelletValidator = plutusBlueprint.validators.find(
({ title }) => title === "pellet.spend"
);

if (!pelletValidator) {
throw new Error("Pellet validator indexed with 'pellet.spend' failed!");
}

const PELLET_SCRIPT: SpendingValidator["script"] = pelletValidator.compiledCode;

const ValidatorParam = Data.Tuple([AssetClass]);
type ValidatorParamT = Data.Static<typeof ValidatorParam>;

function buildPelletValidator(admin_token: AssetClassT): SpendingValidator {
const appliedValidator = applyParamsToScript<ValidatorParamT>(
PELLET_SCRIPT,
[admin_token],
ValidatorParam as unknown as ValidatorParamT
);

return {
type: "PlutusV2",
script: appliedValidator,
};
}

export { buildPelletValidator };
59 changes: 59 additions & 0 deletions offchain/scripts/spacetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Address, Data, SpendingValidator, applyParamsToScript } from "https://deno.land/x/[email protected]/mod.ts";
import plutusBlueprint from "../../onchain/src/plutus.json" with { type: "json" };
import { AssetClass, AssetClassT } from "../types.ts";

const spacetimeValidator = plutusBlueprint.validators.find(
({ title }) => title === "spacetime.spend"
);

if (!spacetimeValidator) {
throw new Error("Spacetime validator indexed with 'spacetime.spend' failed!");
}

const SPACETIME_SCRIPT: SpendingValidator["script"] =
spacetimeValidator.compiledCode;

const ValidatorParam = Data.Tuple([
Data.Bytes(),
Data.Bytes(),
AssetClass,
Data.Integer({ minimum: 0 }),
Data.Integer({ minimum: 0 }),
Data.Integer({ minimum: 0 }),
Data.Integer({ minimum: 0 }),
Data.Integer({ minimum: 0 }),
]);
type ValidatorParamT = Data.Static<typeof ValidatorParam>;

function buildSpacetimeValidator(
pellet_validator_address: Address,
asteria_validator_address: Address,
admin_token: AssetClassT,
max_moving_distance: bigint,
max_ship_fuel: bigint,
fuel_per_step: bigint,
initial_fuel: bigint,
min_asteria_distance: bigint,
): SpendingValidator {
const appliedValidator = applyParamsToScript<ValidatorParamT>(
SPACETIME_SCRIPT,
[
pellet_validator_address,
asteria_validator_address,
admin_token,
max_moving_distance,
max_ship_fuel,
fuel_per_step,
initial_fuel,
min_asteria_distance
],
ValidatorParam as unknown as ValidatorParamT
);

return {
type: "PlutusV2",
script: appliedValidator,
};
}

export { buildSpacetimeValidator };
1 change: 1 addition & 0 deletions offchain/spacetime-ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txHash":"1af0fb23fb9b1a16e31c7f46bbf6f2464fc4f1795773cd8a9425133ba46c8540"}
12 changes: 12 additions & 0 deletions offchain/tests/create-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { fromText } from "https://deno.land/x/[email protected]/mod.ts";
import { createAsteria } from "../transactions/create-asteria.ts";
import { AssetClassT } from "../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};

const txHash = await createAsteria(admin_token);

console.log(txHash);
15 changes: 15 additions & 0 deletions offchain/tests/create-pellet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { fromText } from "https://deno.land/x/[email protected]/mod.ts";
import { AssetClassT } from "../types.ts";
import { createPellet } from "../transactions/create-pellet.ts";

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

const txHash = await createPellet(admin_token, fuel, pos_x, pos_y);

console.log(txHash);
22 changes: 22 additions & 0 deletions offchain/tests/create-ship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { fromText } from "https://deno.land/x/[email protected]/mod.ts";
import { AssetClassT } from "../types.ts";
import { createShip } from "../transactions/create-ship.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 txHash = await createShip(
admin_token,
ship_mint_lovelace_fee,
initial_fuel,
pos_x,
pos_y
);

console.log(txHash);
18 changes: 18 additions & 0 deletions offchain/tests/deploy/deploy-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fromText } from "https://deno.land/x/[email protected]/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;

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

console.log(txHash);
12 changes: 12 additions & 0 deletions offchain/tests/deploy/deploy-pellet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { fromText } from "https://deno.land/x/[email protected]/mod.ts";
import { deployPellet } from "../../transactions/deploy/deploy-pellet.ts";
import { AssetClassT } from "../../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};

const txHash = await deployPellet(admin_token);

console.log(txHash);
24 changes: 24 additions & 0 deletions offchain/tests/deploy/deploy-spacetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fromText } from "https://deno.land/x/[email protected]/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;

const txHash = await deploySpacetime(
admin_token,
max_moving_distance,
max_ship_fuel,
fuel_per_step,
initial_fuel,
min_asteria_distance
);

console.log(txHash);
11 changes: 11 additions & 0 deletions offchain/tests/deploy/spend-refs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { fromText } from "https://deno.land/x/[email protected]/mod.ts";
import { spendRefUTxOs } from "../../transactions/deploy/spend-refs.ts";
import { AssetClassT } from "../../types.ts";

const admin_token: AssetClassT = {
policy: "0298aa99f95e2fe0a0132a6bb794261fb7e7b0d988215da2f2de2005",
name: fromText("tokenA"),
};

const txHash = await spendRefUTxOs(admin_token);
console.log(txHash);
21 changes: 21 additions & 0 deletions offchain/tests/move-ship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { fromText } from "https://deno.land/x/[email protected]/mod.ts";
import { moveShip } from "../transactions/move-ship.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 txHash = await moveShip(
fuel_per_step,
delta_x,
delta_y,
ship_token_name,
pilot_token_name,
shipTxHash
);

console.log(txHash);
Loading

0 comments on commit 1291e39

Please sign in to comment.