Skip to content

Commit

Permalink
add one part of the lock transaction function
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Dec 18, 2023
1 parent 89c3c9f commit a616961
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/hardfork.ak
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ pub fn hard_fork(
// Handle getting input state
expect [script_input1, script_input2, script_input3] = script_inputs

// I'll replace this with something better eventually
let (hard_fork_state_input, miner_lock_input, nft_input) =
if script_input1.output_reference == hard_fork_state_input {
if script_input2.output_reference == nft_input_ref {
Expand Down
207 changes: 184 additions & 23 deletions miner/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ type Genesis = {
outRef: { txHash: string; index: number };
};

type GenesisHF = {
validatorv1: string;
validatorHashv1: string;

validator: string;
validatorHash: string;
validatorAddress: string;

validatorv2: string;
validatorHashv2: string;
validatorAddressv2: string;
outRef: { txHash: string; index: number };
};

const delay = (ms: number | undefined) =>
new Promise((res) => setTimeout(res, ms));

Expand Down Expand Up @@ -380,6 +394,7 @@ const genesisRegister = new Command()
.option("-p, --preview", "Use testnet")
.action(async ({ preview, ogmiosUrl, kupoUrl }) => {
const unAppliedValidator = readValidator();

const genesisFile = Deno.readTextFileSync(
`genesis/${preview ? "preview" : "mainnet"}.json`,
);
Expand Down Expand Up @@ -439,6 +454,7 @@ const genesisRegister = new Command()
}
});

// Used to mint the initial tokens and state for the hard fork
const genesisHardFork = new Command()
.description("Create state 0 for hard fork")
.env("KUPO_URL=<value:string>", "Kupo URL", { required: true })
Expand All @@ -453,7 +469,6 @@ const genesisHardFork = new Command()

const {
validatorHash: validatorHashv1,
validatorAddress: validatorAddressv1,
validator: validatorv1,
}: Genesis = JSON
.parse(
Expand Down Expand Up @@ -546,9 +561,7 @@ const genesisHardFork = new Command()
.payToContract(validatorAddress, Data.to(new Constr(2, [0n])), {
[validatorHash + fromText("lock_state")]: 1n,
})
.complete({
coinSelection: false,
});
.complete();

const signed = await registerTx.sign().complete();

Expand Down Expand Up @@ -588,29 +601,177 @@ const userLockHardFork = new Command()
.env("OGMIOS_URL=<value:string>", "Ogmios URL", { required: true })
.option("-p, --preview", "Use testnet")
.action(async ({ preview, ogmiosUrl, kupoUrl }) => {
// const unAppliedValidator = readValidator();
// const unAppliedValidatorv2 = readValidatorv2();
// const genesisFile = Deno.readTextFileSync(
// `genesis/${preview ? "preview" : "mainnet"}.json`,
// );
const genesisHFFile = Deno.readTextFileSync(
`genesis-hf/${preview ? "preview" : "mainnet"}.json`,
);

// const {
// validatorHash: validatorHashv1,
// validator: validatorv1,
// }: Genesis = JSON
// .parse(
// genesisFile,
// );
const { validatorHashv1, validatorHash, validatorAddress, validator }:
GenesisHF = JSON
.parse(
genesisHFFile,
);

// const provider = new Kupmios(kupoUrl, ogmiosUrl);
// const lucid = await Lucid.new(provider, preview ? "Preview" : "Mainnet");
// lucid.selectWalletFromSeed(Deno.readTextFileSync("seed.txt"));
const provider = new Kupmios(kupoUrl, ogmiosUrl);
const lucid = await Lucid.new(provider, preview ? "Preview" : "Mainnet");
lucid.selectWalletFromSeed(Deno.readTextFileSync("seed.txt"));

// const utxos = await lucid.wallet.getUtxos();
const utxos = await lucid.wallet.getUtxos();
const script_utxos = await lucid.utxosAt(validatorAddress);

// if (utxos.length === 0) {
// throw new Error("No UTXOs Found");
// }
if (utxos.length === 0) {
throw new Error("No UTXOs Found");
}

const userStateUtxo = script_utxos.find((u) => {
Object.keys(u.assets).find((a) => {
return a == validatorHash + "lock_state";
});
})!;

const hardForkUtxo = script_utxos.find((u) => {
Object.keys(u.assets).find((a) => {
return a == validatorHash + "hfs";
});
})!;

const hasLockNFT = utxos.find((u) => {
Object.keys(u.assets).find((a) => {
return a.slice(0, 56) == validatorHash;
});
});

const validatorScript: Script = {
type: "PlutusV2",
script: validator,
};

const validatorRewardAddress = lucid.utils.validatorToRewardAddress(
validatorScript,
);

if (!hasLockNFT) {
const utxo = utxos[0];

const token_amount = utxos.reduce((acc, u) => {
const token_amount = u.assets[validatorHashv1 + fromText("tuna")];
return acc + token_amount;
}, 0n);

const nft_name = lucid.utils.datumToHash(Data.to(
new Constr(0, [
new Constr(0, [utxo.txHash]),
utxo.outputIndex,
]),
));
const datum = userStateUtxo.datum!;

const userState = (Data.from(datum) as Constr<string | bigint | string[]>)
.fields[0] as bigint;

const lockTx = await lucid
.newTx()
.withdraw(
validatorRewardAddress,
0n,
Data.to(
new Constr(1, [
new Constr(0, [
new Constr(0, [hardForkUtxo.txHash]),
hardForkUtxo.outputIndex,
]),
0n,
1n,
new Constr(2, []),
]),
),
)
.mintAssets({
[validatorHash + nft_name]: 1n,
}, Data.to(0n))
.readFrom([hardForkUtxo])
.collectFrom([utxo])
.collectFrom(
[userStateUtxo],
Data.to(
new Constr(1, [
0n,
]),
),
).payToContract(
validatorAddress,
Data.to(new Constr(2, [userState + token_amount])),
{
[validatorHash + "lock_state"]: 1n,
},
).payToContract(
validatorAddress,
Data.to(new Constr(3, [nft_name])),
// amount of tuna to lock
{
[validatorHashv1 + fromText("tuna")]: token_amount,
},
)
.complete();
} else {
const utxo = hasLockNFT!;

const token_amount = utxos.reduce((acc, u) => {
const token_amount = u.assets[validatorHashv1 + fromText("tuna")];
return acc + token_amount;
}, 0n);

const nft_name = Object.keys(utxo.assets);
const datum = userStateUtxo.datum!;

const userState = (Data.from(datum) as Constr<string | bigint | string[]>)
.fields[0] as bigint;

// Not done yet
const lockTx = await lucid
.newTx()
.withdraw(
validatorRewardAddress,
0n,
Data.to(
new Constr(1, [
new Constr(0, [
new Constr(0, [hardForkUtxo.txHash]),
hardForkUtxo.outputIndex,
]),
0n,
1n,
new Constr(3, []),
]),
),
)
.mintAssets({
[validatorHash + nft_name]: 1n,
}, Data.to(0n))
.readFrom([hardForkUtxo])
.collectFrom([utxo])
.collectFrom(
[userStateUtxo],
Data.to(
new Constr(1, [
0n,
]),
),
).payToContract(
validatorAddress,
Data.to(new Constr(2, [userState + token_amount])),
{
[validatorHash + "lock_state"]: 1n,
},
).payToContract(
validatorAddress,
Data.to(new Constr(3, [nft_name])),
// amount of tuna to lock
{
[validatorHashv1 + fromText("tuna")]: token_amount,
},
)
.complete();
}

// const initOutputRef = new Constr(0, [
// new Constr(0, [utxos[0].txHash]),
Expand Down

0 comments on commit a616961

Please sign in to comment.