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

Set all fields, except ergoTree, optional on mockUTxO function #74

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/silver-plants-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fleet-sdk/mock-chain": patch
---

Set all fields, except `ergoTree`, optional on `mockUTxO` function
34 changes: 29 additions & 5 deletions packages/mock-chain/src/objectMocking.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { BoxCandidate, hasDuplicatesBy } from "@fleet-sdk/common";
import { ErgoBox } from "@fleet-sdk/core";
import { ErgoBox, SAFE_MIN_BOX_VALUE } from "@fleet-sdk/core";
import { hex, randomBytes } from "@fleet-sdk/crypto";
import { describe, expect, it } from "vitest";
import { mockHeaders, mockUTxO } from "./objectMocking";

describe("candidateToUTxO()", () => {
describe("mockUTxO()", () => {
it("Should set default values when omitted", () => {
const ergoTree = "0008cd03a621f820dbed198b42a2dca799a571911f2dabbd2e4d441c9aad558da63f084d";

const utxo = mockUTxO({ ergoTree });

expect(utxo.boxId).to.have.length(64);
expect(utxo.value).to.be.equal(SAFE_MIN_BOX_VALUE);
expect(utxo.ergoTree).to.be.equal(ergoTree);
expect(utxo.assets).to.be.empty;
expect(utxo.creationHeight).to.be.equal(0);
expect(utxo.additionalRegisters).to.be.empty;

expect(utxo.transactionId).to.have.length(64);
expect(utxo.index).to.be.equal(0);

expect(ErgoBox.validate(utxo)).to.be.true;
});

it("Should transform a candidate in a valid UTxO", () => {
const candidate: BoxCandidate<bigint> = {
ergoTree: "0008cd03a621f820dbed198b42a2dca799a571911f2dabbd2e4d441c9aad558da63f084d",
Expand All @@ -21,9 +39,15 @@ describe("candidateToUTxO()", () => {

const utxo = mockUTxO(candidate);

expect(utxo.boxId).not.to.be.undefined;
expect(utxo.transactionId).not.to.be.undefined;
expect(utxo.index).not.to.be.undefined;
expect(utxo.boxId).to.have.length(64);
expect(utxo.value).to.be.equal(candidate.value);
expect(utxo.ergoTree).to.be.equal(candidate.ergoTree);
expect(utxo.assets).to.be.deep.equal(candidate.assets);
expect(utxo.creationHeight).to.be.equal(candidate.creationHeight);
expect(utxo.additionalRegisters).to.be.deep.equal(candidate.additionalRegisters);

expect(utxo.transactionId).to.have.length(64);
expect(utxo.index).to.be.equal(0);

expect(ErgoBox.validate(utxo)).to.be.true;
});
Expand Down
19 changes: 14 additions & 5 deletions packages/mock-chain/src/objectMocking.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { Box, BoxCandidate } from "@fleet-sdk/common";
import { Box } from "@fleet-sdk/common";
import { SAFE_MIN_BOX_VALUE } from "@fleet-sdk/core";
import { hex } from "@fleet-sdk/crypto";
import { blake2b256, randomBytes } from "@fleet-sdk/crypto";
import { serializeBox } from "@fleet-sdk/serializer";

export function mockUTxO(candidate: BoxCandidate<bigint>): Box<bigint> {
type MockBoxOptions = Partial<Omit<Box<bigint>, "boxId">> & { ergoTree: string };

export function mockUTxO(mock: MockBoxOptions): Box<bigint> {
const box: Box<bigint> = {
boxId: "",
transactionId: hex.encode(randomBytes(32)),
index: 0,
...candidate

value: mock.value ?? SAFE_MIN_BOX_VALUE,
ergoTree: mock.ergoTree,
assets: mock.assets ?? [],
creationHeight: mock.creationHeight ?? 0,
additionalRegisters: mock.additionalRegisters ?? {},

transactionId: mock.transactionId ?? getRandomId(),
index: mock.index ?? 0
};

const bytes = serializeBox(box).toBytes();
Expand Down