From 68f0675e138a3d1eb28b6ccaedf5d95b85bf3382 Mon Sep 17 00:00:00 2001 From: microproofs Date: Mon, 1 Jul 2024 13:03:55 -0400 Subject: [PATCH] work on adding nomination function --- miner/main.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ miner/utils.ts | 9 +++++++++ 2 files changed, 59 insertions(+) diff --git a/miner/main.ts b/miner/main.ts index ee2c23f..89103fe 100644 --- a/miner/main.ts +++ b/miner/main.ts @@ -30,6 +30,7 @@ import { incrementNonceV2, readValidator, readValidators, + readNewSpendValidator, sha256, calculateInterlink, } from './utils'; @@ -1296,6 +1297,55 @@ app encoding: 'utf8', }), ); + + const newSpend = readNewSpendValidator(); + + const utxos = (await lucid.wallet.getUtxos()).sort((a, b) => { + return a.txHash.localeCompare(b.txHash) || a.outputIndex - b.outputIndex; + }); + + const utxoRef = utxos[0]; + + const newSpendApplied = applyParamsToScript(newSpend.script, [ + tunav2ValidatorHash, + new Constr(0, [new Constr(0, [utxoRef.txHash]), BigInt(utxoRef.outputIndex)]), + ]); + + const newSpendScript: Script = { script: newSpendApplied, type: 'PlutusV2' }; + + const newSpendAddress = lucid.utils.validatorToAddress(newSpendScript); + const newSpendHash = lucid.utils.validatorToScriptHash(newSpendScript); + + // NominateUpgrade(validatorHash, outputIndex) + const mintNominateRedeemer = new Constr(3, [newSpendHash, 0n]); + + const spendNominateRedeemer = new Constr(2, []); + + const setupTx = await lucid + .newTx() + .collectFrom([utxos[1]]) + .payToContract(newSpendAddress, { inline: Data.to(0n) }, { lovelace: 1500000n }) + .complete(); + + const setupTxSigned = await setupTx.sign().complete(); + + await setupTxSigned.submit(); + + lucid.awaitTx(setupTxSigned.toHash()); + + const contractUtxo = await lucid.utxosByOutRef([ + { txHash: setupTxSigned.toHash(), outputIndex: 0 }, + ]); + + const nominateTx = await lucid + .newTx() + .collectFrom(contractUtxo, Data.to(spendNominateRedeemer)) + .mintAssets( + { [tunav2ValidatorHash + fromText('NOMA') + newSpendHash]: 1n }, + Data.to(mintNominateRedeemer), + ) + .payToContract(address, outputData, assets) + .complete(); }); app.parse(); diff --git a/miner/utils.ts b/miner/utils.ts index f92839f..52cb3de 100644 --- a/miner/utils.ts +++ b/miner/utils.ts @@ -52,6 +52,15 @@ export function readValidators(): SpendingValidator[] { ]; } +export function readNewSpendValidator(): SpendingValidator { + const spendValidator = blueprint.validators.filter((v) => v.title === 'new_spend.mine')[0]; + + return { + type: 'PlutusV2', + script: spendValidator.compiledCode, + }; +} + export function printExecutionDetails(tx: TxSigned, name: string) { const redeemers = tx.txSigned.witness_set().redeemers()!; let steps = 0;