Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nft minting #126

Merged
merged 18 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/nft.hl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main(_, ctx: ScriptContext) -> Bool {

tt_assetclass: AssetClass = AssetClass::new(
mph,
"Thread Token".encode_utf8()
"name".encode_utf8()
);

value_minted: Value = tx.minted;
Expand All @@ -24,4 +24,4 @@ func main(_, ctx: ScriptContext) -> Bool {
(input.output_id == outputId).trace("TT2: ")
}
)
}
}
2 changes: 1 addition & 1 deletion tests/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("a template", async () => {

})

it ("docs the tx ingridients", async ({network, alice, validatorHash}) => {
it ("documents the initial state of the Emulator", async ({network, alice, validatorHash}) => {
// https://www.hyperion-bt.org/helios-book/api/reference/address.html?highlight=Address#address
const aliceUtxos = await network.getUtxos(alice.address);
// todo
Expand Down
123 changes: 123 additions & 0 deletions tests/time-props-vesting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { describe, expect, it, expectTypeOf, beforeEach, vi } from 'vitest'
import { promises as fs } from 'fs';
import {
Address,
Assets,
ByteArrayData,
ConstrData,
Datum,
hexToBytes,
IntData,
ListData,
NetworkEmulator,
NetworkParams,
Program,
Tx,
TxOutput,
Value,
} from "@hyperionbt/helios";
import {lockAda} from './src/vesting-lock.ts';
import {cancelVesting} from './src/vesting-cancel.ts';

describe("what happens when we add wait interval between lock and cancel?", async () => {

// https://vitest.dev/guide/test-context.html
beforeEach(async (context) => {
let optimize = false;

// compile script
const script = await fs.readFile('./src/vesting.hl', 'utf8');
const program = Program.new(script);
const compiledProgram = program.compile(optimize);
const validatorHash = compiledProgram.validatorHash;
const validatorAddress = Address.fromValidatorHash(validatorHash);

context.program = program;
//
context.validatorHash = validatorHash;
context.validatorAddress = Address.fromValidatorHash(validatorHash);

// instantiate the Emulator
const minAda = BigInt(2000000); // minimum lovelace needed to send an NFT
const network = new NetworkEmulator();

const alice = network.createWallet(BigInt(20000000));
network.createUtxo(alice, BigInt(50000000));
const bob = network.createWallet(BigInt(10000000));
network.tick(BigInt(10));

context.alice = alice;
context.bob = bob;
context.network = network;

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

context.networkParams = networkParams;
})

it ("documents properties", async ({network, alice, validatorHash}) => {
// https://www.hyperion-bt.org/helios-book/api/reference/address.html?highlight=Address#address
expect(alice.address.toHex().length).toBe(58)
// UTxOs
expect((await alice.utxos)[0].value.dump().lovelace).toBe('20000000');
expect((await alice.utxos)[1].value.dump().lovelace).toBe('50000000');

// https://www.hyperion-bt.org/helios-book/lang/builtins/validatorhash.html?highlight=valida#validatorhash
expect(validatorHash.hex).toBe('e7015c6a1424d748f8241fe3a43b3a382b35dc9ca67320e3ee863dc8')


})

it ("succeeds cancellation", async ({network, alice, bob, program}) => {
const optimize = false; // need to add it to the context
const compiledScript = program.compile(optimize);
const validatorHash = compiledScript.validatorHash;
const validatorAddress = Address.fromValidatorHash(validatorHash);

const adaQty = 10;
const duration = 1000000;
await lockAda(network!, alice!, bob!, program, adaQty, duration);
expect((await alice.utxos)[0].value.dump().lovelace).toBe('50000000');
expect((await alice.utxos)[1].value.dump().lovelace).toBe('9755287');

// https://www.hyperion-bt.org/helios-book/api/reference/fuzzytest.html?highlight=fuzz#fuzzytest
network.tick(BigInt(10780));

await cancelVesting(network!, alice!, program );

const oracle = await alice.utxos;

// think about which is which.
expect(oracle[2].value.dump().lovelace).toBe('9546007');
expect(oracle[1].value.dump().lovelace).toBe('10000000');//
expect(oracle[0].value.dump().lovelace).toBe('50000000');// collateral?
})
it.fails ("Error: tx invalid (not finalized or slot out of range) ", async ({network, alice, bob, program, networkParams}) => {
const optimize = false; // need to add it to the context
const compiledScript = program.compile(optimize);
const validatorHash = compiledScript.validatorHash;
const validatorAddress = Address.fromValidatorHash(validatorHash);

const adaQty = 10;
const duration = 1000000;
await lockAda(network!, alice!, bob!, program, adaQty, duration);
expect((await alice.utxos)[0].value.dump().lovelace).toBe('50000000');
expect((await alice.utxos)[1].value.dump().lovelace).toBe('9755287');

// https://www.hyperion-bt.org/helios-book/api/reference/fuzzytest.html?highlight=fuzz#fuzzytest
network.tick(BigInt(10781));

await cancelVesting(network!, alice!, program );

const oracle = await alice.utxos;

// think about which is which.
expect(oracle[2].value.dump().lovelace).toBe('9546007');
expect(oracle[1].value.dump().lovelace).toBe('10000000');//
expect(oracle[0].value.dump().lovelace).toBe('50000000');// collateral?
})

it ("describes the transaction", async ({network, alice, bob, program}) => {
})
})
2 changes: 1 addition & 1 deletion tests/unit/context-nesting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('suite name', (context) => {
describe('suite name', (context) => {

beforeEach((context) => {
context.foo = 'bar';
context.foo = 'bar';
})
it('foo', ({foo}) => {
expect(foo).toBe('bar')
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/fuzzy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, it, expectTypeOf, beforeEach, vi } from 'vitest'
import { promises as fs } from 'fs';
import {
Assets,
FuzzyTest,
MintingPolicyHash,
NetworkEmulator,
Program
} from "@hyperionbt/helios";


import {lockAda} from './src/lockAda.ts';

describe("create a network with two wallets of which one has an nft", async () => {

// https://vitest.dev/guide/test-context.html
beforeEach(async (context) => {

})

it.fails ("checks for Fuzzy", async ({network, alice, bob, mph}) => {
// https://www.hyperion-bt.org/helios-book/api/reference/fuzzytest.html?highlight=fuzzy#fuzzytest
const fuzzy = new FuzzyTest(0,100,false)
expect(await fuzzy.int(0)).toBe();


})

})
79 changes: 79 additions & 0 deletions tests/unit/mint-nft.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, expect, it, expectTypeOf, beforeEach, vi } from 'vitest'
import { promises as fs } from 'fs';
import {
hexToBytes,
Assets,
ByteArrayData,
ConstrData,
MintingPolicyHash,
NetworkEmulator,
NetworkParams,
Program,
Tx,
TxOutput,
Value
} from "@hyperionbt/helios";


import {lockAda} from './src/lockAda.ts';

describe("create a network with two wallets of which one has an nft", async () => {

// https://vitest.dev/guide/test-context.html
beforeEach(async (context) => {

const minAda = BigInt(2000000); // minimum lovelace needed to send an NFT
const network = new NetworkEmulator();

const alice = network.createWallet(BigInt(20000000));
network.createUtxo(alice, BigInt(5000000));

network.tick(10n)

const amt = 1n;
const name = 'name';
const utxos = await alice.utxos;
const script = await fs.readFile('./src/nft.hl', 'utf8');
const nftProgram = Program.new(script);
//const nftProgram = new Program();
nftProgram.parameters = {["TX_ID"] : utxos[0].txId.hex};
nftProgram.parameters = {["TX_IDX"] : utxos[0].utxoIdx};
//nftProgram.parameters = {["TN"] : name};
const nftCompiledProgram = nftProgram.compile(false);
const nftTokenName = ByteArrayData.fromString(name).toHex();
const tokens: [number[], bigint][] = [[hexToBytes(nftTokenName), amt]];

const mintRedeemer = new ConstrData(0, []);
const tx = new Tx()
.addInputs(utxos)
.attachScript(nftCompiledProgram)
.mintTokens(
nftCompiledProgram.mintingPolicyHash,
tokens,
mintRedeemer)
.addOutput(new TxOutput(
(await alice.address),
new Value(minAda, new Assets([[nftCompiledProgram.mintingPolicyHash, tokens]]))
));

// expect(tx.dump().body).toBe();

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);
network.tick(10n);

context.alice = alice;
context.network = network;

})

it ("docs the tx ingridients", async ({network, alice, }) => {
// https://www.hyperion-bt.org/helios-book/api/reference/address.html?highlight=Address#address
expect(alice.address.toHex().length).toBe(58)
expect((await alice.utxos)[0].value.dump().lovelace).toBe('2000000');
expect(Object.keys((await alice.utxos)[1].value.dump().assets)[0]).toBe();
})

})
11 changes: 5 additions & 6 deletions tests/vesting-cancel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ describe("a vesting contract: Cancel transaction", async () => {
const aliceUtxos = await alice.utxos;
// https://www.hyperion-bt.org/helios-book/api/reference/address.html?highlight=Address#address
expect(alice.address.toHex().length).toBe(58)
// UTxO
expect(aliceUtxos[1].value.dump().lovelace).toBe('50000000')
// UTxOs
expect((await alice.utxos)[0].value.dump().lovelace).toBe('20000000');
expect((await alice.utxos)[1].value.dump().lovelace).toBe('50000000');

// https://www.hyperion-bt.org/helios-book/lang/builtins/validatorhash.html?highlight=valida#validatorhash
expect(validatorHash.hex).toBe('e7015c6a1424d748f8241fe3a43b3a382b35dc9ca67320e3ee863dc8')


})

it ("tests cancelVesting.ts", async ({network, alice, bob, program}) => {
Expand All @@ -69,9 +72,6 @@ describe("a vesting contract: Cancel transaction", async () => {
const validatorHash = compiledScript.validatorHash;
const validatorAddress = Address.fromValidatorHash(validatorHash);

expect((await alice.utxos)[0].value.dump().lovelace).toBe('20000000');
expect((await alice.utxos)[1].value.dump().lovelace).toBe('50000000');

const adaQty = 10;
const duration = 1000000;
await lockAda(network!, alice!, bob!, program, adaQty, duration);
Expand Down Expand Up @@ -107,7 +107,6 @@ describe("a vesting contract: Cancel transaction", async () => {
expect((await alice.utxos)[0].value.dump().lovelace).toBe('50000000');
expect((await alice.utxos)[1].value.dump().lovelace).toBe('9755287');


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

Expand Down