Skip to content

Commit e41c5f7

Browse files
authored
Merge pull request #196 from fleet-sdk/ergobox-cloning
Support instantiating ErgoBox from another ErgoBox
2 parents d3911f2 + 5125398 commit e41c5f7

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

.changeset/sharp-boxes-take.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fleet-sdk/core": patch
3+
---
4+
5+
Support instantiating `ErgoBox` from another `ErgoBox`

packages/core/src/models/ergoBox.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ describe("Construction", () => {
3131
expect(ergoBox.index).to.be.equal(tv.index);
3232
});
3333

34+
test.each(regularBoxes)("Should construct from an ErgoBox candidate", (tv) => {
35+
const ergoBox = new ErgoBox(new ErgoBox(tv), tv.transactionId, tv.index);
36+
37+
expect(ergoBox.boxId).to.be.equal(tv.boxId);
38+
expect(ergoBox.transactionId).to.be.equal(tv.transactionId);
39+
expect(ergoBox.index).to.be.equal(tv.index);
40+
});
41+
3442
it("Should throw if transactionId or index is not provided for box candidate", () => {
3543
const box = regularBoxes[0];
3644
const candidate = boxToCandidate(box);

packages/core/src/models/ergoBox.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ export class ErgoBox<R extends NonMandatoryRegisters = NonMandatoryRegisters> {
6262
constructor(candidate: ErgoBoxCandidate<R>, transactionId: string, index: number);
6363
constructor(candidate: BoxCandidate<Amount, R>, transactionId: string, index: number);
6464
constructor(box: Box<Amount, R>);
65+
constructor(box: ErgoBox<R>);
6566
constructor(
66-
box: Box<Amount, R> | ErgoBoxCandidate<R> | BoxCandidate<Amount, R>,
67+
box: Box<Amount, R> | ErgoBox<R> | ErgoBoxCandidate<R> | BoxCandidate<Amount, R>,
6768
transactionId?: string,
6869
index?: number
6970
) {
70-
if (isBox(box)) {
71+
if (box instanceof ErgoBox) {
72+
this.#candidate = box.#candidate;
73+
this.#transactionId = box.transactionId;
74+
this.#index = box.index;
75+
this.#boxId = box.boxId;
76+
} else if (isBox(box)) {
7177
this.#candidate = new ErgoBoxCandidate(box);
7278
this.#transactionId = box.transactionId;
7379
this.#index = box.index;

packages/core/src/models/ergoUnsignedInput.spec.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
import { SBool } from "@fleet-sdk/serializer";
22
import { regularBoxes } from "_test-vectors";
3-
import { describe, expect, it } from "vitest";
3+
import { ErgoBox } from "packages/core/dist";
4+
import { describe, expect, it, test } from "vitest";
45
import { ErgoUnsignedInput } from "./ergoUnsignedInput";
56

67
describe("Construction", () => {
7-
it("Should construct from a vanilla object", () => {
8-
for (const box of regularBoxes) {
9-
const input = new ErgoUnsignedInput(box);
8+
test.each(regularBoxes)("Should construct from a vanilla object", (box) => {
9+
const input = new ErgoUnsignedInput(box);
1010

11-
expect(input.boxId).toBe(box.boxId);
12-
expect(input.value).toBe(box.value);
13-
expect(input.ergoTree).toBe(box.ergoTree);
14-
expect(input.assets).toEqual(box.assets);
15-
expect(input.creationHeight).toBe(box.creationHeight);
16-
expect(input.additionalRegisters).toBe(box.additionalRegisters);
17-
expect(input.transactionId).toBe(box.transactionId);
18-
expect(input.index).toBe(box.index);
19-
expect(input.extension).to.be.deep.equal({});
20-
}
11+
expect(input.boxId).toBe(box.boxId);
12+
expect(input.value).toBe(box.value);
13+
expect(input.ergoTree).toBe(box.ergoTree);
14+
expect(input.assets).toEqual(box.assets);
15+
expect(input.creationHeight).toBe(box.creationHeight);
16+
expect(input.additionalRegisters).toBe(box.additionalRegisters);
17+
expect(input.transactionId).toBe(box.transactionId);
18+
expect(input.index).toBe(box.index);
19+
expect(input.extension).to.be.deep.equal({});
20+
});
21+
22+
test.each(regularBoxes)("Should construct from a ErgoBox object", (box) => {
23+
const input = new ErgoUnsignedInput(new ErgoBox(box));
24+
25+
expect(input.boxId).toBe(box.boxId);
26+
expect(input.value).toBe(box.value);
27+
expect(input.ergoTree).toBe(box.ergoTree);
28+
expect(input.assets).toEqual(box.assets);
29+
expect(input.creationHeight).toBe(box.creationHeight);
30+
expect(input.additionalRegisters).toBe(box.additionalRegisters);
31+
expect(input.transactionId).toBe(box.transactionId);
32+
expect(input.index).toBe(box.index);
33+
expect(input.extension).to.be.deep.equal({});
2134
});
2235
});
2336

0 commit comments

Comments
 (0)