-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ca9b8e
commit 7190a6b
Showing
5 changed files
with
419 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { | ||
Data, | ||
toUnit, | ||
TxHash, | ||
Constr, | ||
UTxO, | ||
Script, | ||
} from "https://deno.land/x/lucid@0.10.7/mod.ts"; | ||
import { fetchReferenceScript, lucidBase } from "./utils.ts"; | ||
import { | ||
AssetClassT, | ||
PelletDatum, | ||
PelletDatumT, | ||
ShipDatum, | ||
ShipDatumT, | ||
} from "./types.ts"; | ||
|
||
async function gatherFuel( | ||
spacetimeRefTxHash: string, | ||
pelletRefTxHash: string, | ||
admin_token: AssetClassT, | ||
gather_amount: bigint, | ||
ship_tx_hash: TxHash, | ||
pellet_tx_hash: TxHash, | ||
pellet_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 spacetimeRef = await fetchReferenceScript(lucid, spacetimeRefTxHash); | ||
const spacetimeValidator = spacetimeRef.scriptRef as Script; | ||
const spacetimeAddressBech32 = | ||
lucid.utils.validatorToAddress(spacetimeValidator); | ||
const shipyardPolicyId = lucid.utils.mintingPolicyToId(spacetimeValidator); | ||
|
||
const pelletRef = await fetchReferenceScript(lucid, pelletRefTxHash); | ||
const pelletValidator = pelletRef.scriptRef as Script; | ||
const pelletAddressBech32 = lucid.utils.validatorToAddress(pelletValidator); | ||
|
||
const ship: UTxO = ( | ||
await lucid.utxosByOutRef([ | ||
{ | ||
txHash: ship_tx_hash, | ||
outputIndex: 0, | ||
}, | ||
]) | ||
)[0]; | ||
if (!ship.datum) { | ||
throw Error("Ship datum not found"); | ||
} | ||
const shipAda = ship.assets.lovelace; | ||
|
||
const pellet: UTxO = ( | ||
await lucid.utxosByOutRef([ | ||
{ | ||
txHash: pellet_tx_hash, | ||
outputIndex: pellet_tx_index, | ||
}, | ||
]) | ||
)[0]; | ||
if (!ship.datum) { | ||
throw Error("Ship datum not found"); | ||
} | ||
const pelletAda = pellet.assets.lovelace; | ||
|
||
const shipInputDatum = Data.from<ShipDatumT>( | ||
ship.datum as string, | ||
ShipDatum as unknown as ShipDatumT | ||
); | ||
const shipInfo = { | ||
fuel: shipInputDatum.fuel + gather_amount, | ||
pos_x: shipInputDatum.pos_x, | ||
pos_y: shipInputDatum.pos_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 pelletInputDatum = Data.from<PelletDatumT>( | ||
pellet.datum as string, | ||
PelletDatum as unknown as PelletDatumT | ||
); | ||
const pelletInfo = { | ||
fuel: pelletInputDatum.fuel - gather_amount, | ||
pos_x: pelletInputDatum.pos_x, | ||
pos_y: pelletInputDatum.pos_y, | ||
shipyard_policy: shipyardPolicyId, | ||
}; | ||
const pelletOutputDatum = Data.to<PelletDatumT>( | ||
pelletInfo, | ||
PelletDatum as unknown as PelletDatumT | ||
); | ||
|
||
const shipTokenUnit = toUnit( | ||
shipyardPolicyId, | ||
shipInputDatum.ship_token_name | ||
); | ||
const pilotTokenUnit = toUnit( | ||
shipyardPolicyId, | ||
shipInputDatum.pilot_token_name | ||
); | ||
const adminTokenUnit = toUnit(admin_token.policy, admin_token.name); | ||
|
||
const shipRedeemer = Data.to(new Constr(1, [new Constr(1, [gather_amount])])); | ||
const pelletRedeemer = Data.to(new Constr(0, [gather_amount])); | ||
const tx = await lucid | ||
.newTx() | ||
.collectFrom([ship], shipRedeemer) | ||
.collectFrom([pellet], pelletRedeemer) | ||
.readFrom([spacetimeRef, pelletRef]) | ||
.payToContract( | ||
spacetimeAddressBech32, | ||
{ inline: shipOutputDatum }, | ||
{ | ||
[shipTokenUnit]: BigInt(1), | ||
lovelace: shipAda, | ||
} | ||
) | ||
.payToContract( | ||
pelletAddressBech32, | ||
{ inline: pelletOutputDatum }, | ||
{ | ||
[adminTokenUnit]: BigInt(1), | ||
lovelace: pelletAda, | ||
} | ||
) | ||
.payToAddress(await lucid.wallet.address(), { | ||
[pilotTokenUnit]: BigInt(1), | ||
}) | ||
.complete(); | ||
|
||
const signedTx = await tx.sign().complete(); | ||
return signedTx.submit(); | ||
} | ||
|
||
export { gatherFuel }; |
Oops, something went wrong.