Skip to content

Commit

Permalink
feat(lockADA-transaction): Moved transaction building to a separate m…
Browse files Browse the repository at this point in the history
…odule

BREAKING CHANGE: #101
  • Loading branch information
aleeusgr committed May 20, 2023
1 parent 35f1916 commit c947f0d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
101 changes: 101 additions & 0 deletions src/lockAda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { promises as fs } from 'fs';
import {
Address,
Assets,
ByteArrayData,
ConstrData,
Datum,
hexToBytes,
IntData,
ListData,
NetworkEmulator,
NetworkParams,
Program,
Tx,
TxOutput,
Value,
} from "@hyperionbt/helios";

export const lockAda = async (params : any) => {

const alice = params[0] as object;
const bob = params[1] as object;
const adaQty = params[2] as number;
const duration = params[3] as number;

const benAddr = bob.address;
const emulatorDate = 1677108984000;
const deadline = new Date(emulatorDate + duration);
const benPkh = bob.pubKeyHash;
const ownerPkh = alice.pubKeyHash;

const lovelaceAmt = Number(adaQty) * 1000000;
const adaAmountVal = new Value(BigInt(lovelaceAmt));

const datum = new ListData([new ByteArrayData(ownerPkh.bytes),
new ByteArrayData(benPkh.bytes),
new IntData(BigInt(deadline.getTime()))]);
const inlineDatum = Datum.inline(datum);

const inputUtxos = await alice.utxos;

const tx = new Tx();

tx.addInputs(inputUtxos);

const mintScript =`minting nft
const TX_ID: ByteArray = #` + inputUtxos[0].txId.hex + `
const txId: TxId = TxId::new(TX_ID)
const outputId: TxOutputId = TxOutputId::new(txId, ` + inputUtxos[0].utxoIdx + `)
func main(ctx: ScriptContext) -> Bool {
tx: Tx = ctx.tx;
mph: MintingPolicyHash = ctx.get_current_minting_policy_hash();
assetclass: AssetClass = AssetClass::new(
mph,
"Vesting Key".encode_utf8()
);
value_minted: Value = tx.minted;
// Validator logic starts
(value_minted == Value::new(assetclass, 1)).trace("NFT1: ") &&
tx.inputs.any((input: TxInput) -> Bool {
(input.output_id == outputId).trace("NFT2: ")
}
)
}`

const optimize = false; //maybe add to test context?
const mintProgram = Program.new(mintScript).compile(optimize);

tx.attachScript(mintProgram);

// Construct the NFT that we will want to send as an output
const nftTokenName = ByteArrayData.fromString("Vesting Key").toHex();
const tokens: [number[], bigint][] = [[hexToBytes(nftTokenName), BigInt(1)]];

// Create an empty Redeemer because we must always send a Redeemer with
// a plutus script transaction even if we don't actually use it.
const mintRedeemer = new ConstrData(0, []);

// Indicate the minting we want to include as part of this transaction
tx.mintTokens(
mintProgram.mintingPolicyHash,
tokens,
mintRedeemer
)

const lockedVal = new Value(adaAmountVal.lovelace, new Assets([[mintProgram.mintingPolicyHash, tokens]]));

// Add the destination address and the amount of Ada to lock including a datum
tx.addOutput(new TxOutput(validatorAddress, lockedVal, inlineDatum));

// beforeAll?
const networkParamsFile = await fs.readFile('./src/preprod.json', 'utf8');
const networkParams = new NetworkParams(JSON.parse(networkParamsFile.toString()));

await tx.finalize(networkParams, alice.address);
const txId = await network.submitTx(tx);
}
10 changes: 9 additions & 1 deletion tests/helios-vesting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TxOutput,
Value,
} from "@hyperionbt/helios";
import {lockAda} from './src/lockAda.ts';

describe("a vesting contract", async () => {

Expand Down Expand Up @@ -66,7 +67,7 @@ describe("a vesting contract", async () => {
const duration = 10000000;

const emulatorDate = 1677108984000;
const deadline = new Date(emulatorDate + duration );
const deadline = new Date(emulatorDate + duration);
const benPkh = bob.pubKeyHash;
const ownerPkh = alice.pubKeyHash;

Expand Down Expand Up @@ -149,4 +150,11 @@ describe("a vesting contract", async () => {
expect(Object.keys((await network.getUtxos(validatorAddress))[0].value.dump().assets)[0]).toEqual(mintProgram.mintingPolicyHash.hex);

})

it ("tests lockAda tx import", async ({network, alice, bob, validatorAddress}) => {
// https://github.com/lley154/helios-examples/blob/704cf0a92cfe252b63ffb9fd36c92ffafc1d91f6/vesting/pages/index.tsx#LL157C1-L280C4
const adaQty = 10 ;
const duration = 10000000;
await lockAda(alice, bob, adaQty, duration)
})
})

0 comments on commit c947f0d

Please sign in to comment.