-
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.
offchain for consuming Asteria & pellets
- Loading branch information
1 parent
159f6c4
commit 6c1414b
Showing
2 changed files
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { admin_token } from "../constants.ts"; | ||
import { consumeAsteria } from "../transactions/consume-asteria.ts"; | ||
import { printTxURL } from "../utils.ts"; | ||
|
||
const asteria_tx_hash = | ||
"6603802230bef801697be03e3773ca10bfd17bebb0a8e6b49661353996d255a8"; | ||
const asteria_tx_index = 0; | ||
|
||
const txHash = await consumeAsteria( | ||
admin_token, | ||
asteria_tx_hash, | ||
asteria_tx_index | ||
); | ||
|
||
printTxURL(txHash); |
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,56 @@ | ||
import { | ||
Data, | ||
toUnit, | ||
TxHash, | ||
Constr, | ||
UTxO, | ||
} from "https://deno.land/x/lucid@0.10.7/mod.ts"; | ||
import { fetchReferenceScript, lucidBase } from "../utils.ts"; | ||
import { AssetClassT } from "../types.ts"; | ||
|
||
async function consumeAsteria( | ||
admin_token: AssetClassT, | ||
asteria_tx_hash: TxHash, | ||
asteria_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 asteriaRefTxHash: { txHash: string } = JSON.parse( | ||
await Deno.readTextFile("./script-refs/asteria-ref.json") | ||
); | ||
const asteriaRef = await fetchReferenceScript(lucid, asteriaRefTxHash.txHash); | ||
|
||
const asteria: UTxO = ( | ||
await lucid.utxosByOutRef([ | ||
{ | ||
txHash: asteria_tx_hash, | ||
outputIndex: asteria_tx_index, | ||
}, | ||
]) | ||
)[0]; | ||
if (!asteria.datum) { | ||
throw Error("Asteria datum not found"); | ||
} | ||
|
||
const adminTokenUnit = toUnit(admin_token.policy, admin_token.name); | ||
|
||
const consumeRedeemer = Data.to(new Constr(2, [])); | ||
const tx = await lucid | ||
.newTx() | ||
.readFrom([asteriaRef]) | ||
.collectFrom([asteria], consumeRedeemer) | ||
.payToAddress(await lucid.wallet.address(), { | ||
[adminTokenUnit]: BigInt(1), | ||
}) | ||
.complete(); | ||
|
||
const signedTx = await tx.sign().complete(); | ||
return signedTx.submit(); | ||
} | ||
|
||
export { consumeAsteria }; |