Skip to content

Commit

Permalink
feat: blaze-sdk (#102)
Browse files Browse the repository at this point in the history
* feat: initial prototype for asteria blaze sdk

* feat: asteria blaze sdk

* docs: update ship movement and fuel gathering examples

---------

Co-authored-by: Rico Miles <ricoquiblat@gmail.com>
  • Loading branch information
Mercurial and ricomiles authored Nov 27, 2024
1 parent 83f2a6d commit 28c1aba
Show file tree
Hide file tree
Showing 17 changed files with 5,962 additions and 127 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ target
**/.env

.mono
**/.DS_Store
**/.DS_Store

node_modules/
76 changes: 54 additions & 22 deletions docs/pages/guides/creating_ship.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,58 @@ Creates a `ShipState` UTxO locking min ada and a `ShipToken` (minted in this tx)
## Lucid Example

```ts
import { fromText } from "https://deno.land/x/lucid@0.10.7/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);
import { createShip } from "../src";
import { GameIdentifier, OutRef } from "../src/types";

async function main() {
const address =
"addr_test1qzjpgxkhe06gxzstfhywg02ggy5ltuwne6mfr406dlf0mpwp9a07r34cwsnkpn44tllxuydw4wp0xvstw5jqv5q9lszsk2qynn";

const asteria_utxo: OutRef = {
tx_hash:
"a8c77645426fc3031f1daedb657dd1e9af03e2883576d694bcd188b653e91a28",
tx_index: 0n,
};

const spacetime_script_reference: OutRef = {
tx_hash:
"41e5881cd3bdc3f08bcf341796347e9027e3bcd8d58608b4fcfca5c16cbf5921",
tx_index: 0n,
};

const pellet_script_reference: OutRef = {
tx_hash:
"ba6fab625d70a81f5d1b699e7efde4b74922d06224bef1f6b84f3adf0a61f3f3",
tx_index: 0n,
};

const asteria_script_reference: OutRef = {
tx_hash:
"39871aab15b7c5ab1075ba431d7475f3977fe40fbb8d654b6bdf6f6726659277",
tx_index: 0n,
};

const pos_x = 20n;
const pos_y = 20n;

const gameIdentifier: GameIdentifier = {
asteria_utxo,
spacetime_script_reference,
pellet_script_reference,
asteria_script_reference,
};

const tx = await createShip(
address,
gameIdentifier,
pos_x,
pos_y,
);

return tx;
}

main().then((tx) => {
console.log(tx.toCbor());
});
```
50 changes: 50 additions & 0 deletions docs/pages/guides/gathering_fuel.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { max } from "rxjs";
import { GameIdentifier, OutRef } from "../src/types";
import { gatherFuel } from "../src";

async function main() {
const address =
"addr_test1qzjpgxkhe06gxzstfhywg02ggy5ltuwne6mfr406dlf0mpwp9a07r34cwsnkpn44tllxuydw4wp0xvstw5jqv5q9lszsk2qynn";

const ship_utxo: OutRef = {
tx_hash:
"3e04a7a3e4a1015705c44822feaf5f2da1e9609eebd68310c87b7eba7923739a",
tx_index: 0n,
};
const pellet_utxo: OutRef = {
tx_hash:
"22af0198b4e6e9c8306392c7ac0ea97b8a2f659a6312708b22b7717805554b97",
tx_index: 0n,
};

const spacetime_script_reference: OutRef = {
tx_hash:
"41e5881cd3bdc3f08bcf341796347e9027e3bcd8d58608b4fcfca5c16cbf5921",
tx_index: 0n,
};

const pellet_script_reference: OutRef = {
tx_hash:
"ba6fab625d70a81f5d1b699e7efde4b74922d06224bef1f6b84f3adf0a61f3f3",
tx_index: 0n,
};


const gather_fuel_identifier: GameIdentifier = {
ship_utxo,
pellet_utxo,
spacetime_script_reference,
pellet_script_reference,
};

const tx = await gatherFuel(
address,
gather_fuel_identifier,
);

return tx;
}

main().then((tx) => {
console.log(tx.toCbor());
});
58 changes: 58 additions & 0 deletions docs/pages/guides/mining_asteria.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { mineAsteria } from "../src";
import { GameIdentifier, OutRef } from "../src/types";

async function main() {
const address =
"addr_test1qzjpgxkhe06gxzstfhywg02ggy5ltuwne6mfr406dlf0mpwp9a07r34cwsnkpn44tllxuydw4wp0xvstw5jqv5q9lszsk2qynn";
const admin_token_subject =
"d37ec8944834e0ce98d655820e05a2d0215b725fd2b79562128f79fa417374657269612041646d696e";

const spacetime_script_reference: OutRef = {
tx_hash:
"41e5881cd3bdc3f08bcf341796347e9027e3bcd8d58608b4fcfca5c16cbf5921",
tx_index: 0n,
};

const pellet_script_reference: OutRef = {
tx_hash:
"ba6fab625d70a81f5d1b699e7efde4b74922d06224bef1f6b84f3adf0a61f3f3",
tx_index: 0n,
};

const asteria_script_reference: OutRef = {
tx_hash:
"39871aab15b7c5ab1075ba431d7475f3977fe40fbb8d654b6bdf6f6726659277",
tx_index: 0n,
};
const asteria_utxo: OutRef = {
tx_hash:
"bbcdff5ed1c0dd78c58c42dab8356c161f865d0c3efc1ea281026140a0d38516",
tx_index: 1n,
};

const ship_utxo: OutRef = {
tx_hash:
"594fb0c4baff88873d15d5973103efc667052deec3315ba4bc89aeaaf53dd407",
tx_index: 0n,
};


const gameIdentifier: GameIdentifier = {
asteria_utxo,
ship_utxo,
spacetime_script_reference,
pellet_script_reference,
asteria_script_reference,
};

const tx = await mineAsteria(
address,
gameIdentifier,
);

return tx;
}

main().then((tx) => {
console.log(tx.toCbor());
});
150 changes: 46 additions & 104 deletions docs/pages/guides/moving_ship.mdx
Original file line number Diff line number Diff line change
@@ -1,105 +1,47 @@

# Moving a Ship

Updates the `pos_x`, `pos_y` and `fuel` datum fields of the `ShipState` UTxO by adding the `delta_x` and `delta_y` values specified in the redeemer, and subtracting the fuel amount needed for the displacement.

![moveShip diagram](/txs/moveShip.png)

```ts
import {
Data,
toUnit,
TxHash,
Constr,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { distance, lucidBase, required_fuel } from "../utils.ts";
import { ShipDatum, ShipDatumT } from "../types.ts";

// look up these values for the specific challenge you're particpating
const FUEL_PER_STEP = 5;

async function moveShip(
fuel_per_step: bigint,
delta_x: bigint,
delta_y: bigint,
ship_token_name: string,
pilot_token_name: string,
shipTxHash: TxHash
): Promise<TxHash> {
const lucid = await lucidBase();

const spacetimeRefTxHash: { txHash: string } = JSON.parse(
await Deno.readTextFile("./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 spacetime validator from ref UTxO");
}
const spacetimeAddressBech32 =
lucid.utils.validatorToAddress(spacetimeValidator);

const shipyardPolicyId = lucid.utils.mintingPolicyToId(spacetimeValidator);
const shipTokenUnit = toUnit(shipyardPolicyId, ship_token_name);
const pilotTokenUnit = toUnit(shipyardPolicyId, pilot_token_name);

const ship: UTxO = (
await lucid.utxosByOutRef([
{
txHash: shipTxHash,
outputIndex: 0,
},
])
)[0];
if (!ship.datum) {
throw Error("Ship datum not found");
}
const shipAda = ship.assets.lovelace;

const shipInputDatum = Data.from<ShipDatumT>(
ship.datum as string,
ShipDatum as unknown as ShipDatumT
);

const moved_distance = distance(delta_x, delta_y);
const spent_fuel = required_fuel(moved_distance, fuel_per_step);
const shipInfo = {
fuel: shipInputDatum.fuel - spent_fuel,
pos_x: shipInputDatum.pos_x + delta_x,
pos_y: shipInputDatum.pos_y + delta_y,
ship_token_name: shipInputDatum.ship_token_name,
pilot_token_name: shipInputDatum.pilot_token_name,
};
const shipOutputDatum = Data.to<ShipDatumT>(
shipInfo,
ShipDatum as unknown as ShipDatumT
);

const moveShipRedeemer = Data.to(
new Constr(1, [new Constr(0, [delta_x, delta_y])])
);

const tx = await lucid
.newTx()
.collectFrom([ship], moveShipRedeemer)
.readFrom(spacetimeRef)
.payToContract(
spacetimeAddressBech32,
{ inline: shipOutputDatum },
{
[shipTokenUnit]: BigInt(1),
lovelace: shipAda,
}
)
.payToAddress(await lucid.wallet.address(), {
[pilotTokenUnit]: BigInt(1),
})
.complete();
import { createShip, moveShip } from "../src";
import { OutRef, GameIdentifier } from "../src/types";

async function main() {
const address =
"addr_test1qzjpgxkhe06gxzstfhywg02ggy5ltuwne6mfr406dlf0mpwp9a07r34cwsnkpn44tllxuydw4wp0xvstw5jqv5q9lszsk2qynn";

const ship_utxo: OutRef = {
tx_hash:
"41f2a593e131a3c5db878bbe7753b89c8c6de555b7127e812c0f488d1a898368",
tx_index: 0n,
};

const spacetime_script_reference: OutRef = {
tx_hash:
"41e5881cd3bdc3f08bcf341796347e9027e3bcd8d58608b4fcfca5c16cbf5921",
tx_index: 0n,
};

const pellet_script_reference: OutRef = {
tx_hash:
"ba6fab625d70a81f5d1b699e7efde4b74922d06224bef1f6b84f3adf0a61f3f3",
tx_index: 0n,
};

const delta_x = -1n;
const delta_y = -1n

const move_ship_identifier: GameIdentifier = {
ship_utxo,
spacetime_script_reference,
pellet_script_reference,
};

const tx = await moveShip(
address,
move_ship_identifier,
delta_x,
delta_y,
);

return tx;
}
```

main().then((tx) => {
console.log(tx.toCbor());
});
Loading

0 comments on commit 28c1aba

Please sign in to comment.