diff --git a/.changeset/custom-change-value-per-byte.md b/.changeset/custom-change-value-per-byte.md new file mode 100644 index 00000000..f6ee732f --- /dev/null +++ b/.changeset/custom-change-value-per-byte.md @@ -0,0 +1,5 @@ +--- +"@fleet-sdk/core": patch +--- + +Support custom `valuePerByte` setting in `TransactionBuilder` for change boxes diff --git a/packages/core/src/builder/transactionBuilder.spec.ts b/packages/core/src/builder/transactionBuilder.spec.ts index 55d9825b..280cf632 100644 --- a/packages/core/src/builder/transactionBuilder.spec.ts +++ b/packages/core/src/builder/transactionBuilder.spec.ts @@ -186,13 +186,15 @@ describe("basic construction", () => { const builder = new TransactionBuilder(height).from(regularBoxes); expect(builder.settings.canBurnTokens).toBeFalsy(); expect(builder.settings.maxTokensPerChangeBox).toBe(MAX_TOKENS_PER_BOX); + expect(builder.settings.valuePerByte).toBe(BOX_VALUE_PER_BYTE); builder.configure((settings) => { - settings.allowTokenBurning(true).setMaxTokensPerChangeBox(10); + settings.allowTokenBurning(true).setMaxTokensPerChangeBox(10).setValuePerByte(500n); }); expect(builder.settings.canBurnTokens).toBeTruthy(); expect(builder.settings.maxTokensPerChangeBox).toBe(10); + expect(builder.settings.valuePerByte).toBe(500n); }); it("Should eject context", () => { @@ -745,8 +747,8 @@ describe("Building", () => { expect(change3.additionalRegisters).toEqual({}); }); - function _estimateBoxValue(box: BoxCandidate) { - return BigInt(estimateBoxSize(box)) * BOX_VALUE_PER_BYTE; + function _estimateBoxValue(box: BoxCandidate, valuePerByte = BOX_VALUE_PER_BYTE) { + return BigInt(estimateBoxSize(box)) * valuePerByte; } it("Should produce multiple change boxes based on maxTokensPerChangeBox param", () => { @@ -844,6 +846,66 @@ describe("Building", () => { ); } }); + it("Should produce token change boxes using custom valuePerByte", () => { + const tokensPerBox = 3; + const transaction = new TransactionBuilder(height) + .from(regularBoxes) + .sendChangeTo(a1.address) + .configureSelector((selector) => selector.ensureInclusion((i) => some(i.assets))) + .configure((settings) => { + settings.setMaxTokensPerChangeBox(tokensPerBox).isolateErgOnChange().setValuePerByte(500n); + }) + .build(); + + expect(transaction.inputs).toHaveLength(4); + expect(transaction.dataInputs).toHaveLength(0); + expect(transaction.outputs).toHaveLength(8); + + const [firstChange] = transaction.outputs; + expect(firstChange.assets).toHaveLength(0); + + for (let i = 1; i < transaction.outputs.length; i++) { + expect(transaction.outputs[i].assets).toHaveLength(tokensPerBox); + expect(transaction.outputs[i].value).toBe( + _estimateBoxValue(transaction.outputs[i].toCandidate(), 500n) + ); + } + }); + it("Should select more inputs when custom valuePerByte requires higher change value", () => { + const targetBoxId = "2555e34138d276905fe0bc19240bbeca10f388a71f7b4d2f65a7d0bfd23c846d"; + const inputs = regularBoxes.filter((box) => + [ + "3e67b4be7012956aa369538b46d751a4ad0136138760553d5400a10153046e52", + "e56847ed19b3dc6b72828fcfb992fdf7310828cf291221269b7ffc72fd66706e", + targetBoxId + ].includes(box.boxId) + ); + const output = new OutputBuilder(999840000n, a2.address).addTokens({ + tokenId: "007fd64d1ee54d78dd269c8930a38286caa28d3f29d27cadcb796418ab15c283", + amount: 180n + }); + + const defaultTx = new TransactionBuilder(height) + .from(inputs) + .to(output) + .payFee(RECOMMENDED_MIN_FEE_VALUE) + .sendChangeTo(a1.address) + .configureSelector((selector) => selector.ensureInclusion((box) => box.boxId === targetBoxId)) + .build(); + + expect(defaultTx.inputs).toHaveLength(2); + + const customTx = new TransactionBuilder(height) + .from(inputs) + .to(output) + .payFee(RECOMMENDED_MIN_FEE_VALUE) + .sendChangeTo(a1.address) + .configureSelector((selector) => selector.ensureInclusion((box) => box.boxId === targetBoxId)) + .configure((s) => s.setValuePerByte(1000n)) + .build(); + + expect(customTx.inputs).toHaveLength(3); + }); it("Should build in EIP-12 format", () => { const transaction = new TransactionBuilder(height) diff --git a/packages/core/src/builder/transactionBuilder.ts b/packages/core/src/builder/transactionBuilder.ts index c778c8b9..7b6b3006 100644 --- a/packages/core/src/builder/transactionBuilder.ts +++ b/packages/core/src/builder/transactionBuilder.ts @@ -26,12 +26,7 @@ import { InvalidInput, MalformedTransaction, NotAllowedTokenBurning } from "../e import { NonStandardizedMinting } from "../errors/nonStandardizedMinting"; import { ErgoAddress, InputsCollection, OutputsCollection, TokensCollection } from "../models"; import { ErgoUnsignedTransaction } from "../models/ergoUnsignedTransaction"; -import { - BOX_VALUE_PER_BYTE, - OutputBuilder, - SAFE_MIN_BOX_VALUE, - estimateMinBoxValue -} from "./outputBuilder"; +import { OutputBuilder, SAFE_MIN_BOX_VALUE, estimateMinBoxValue } from "./outputBuilder"; import { type FleetPluginContext, createPluginContext } from "./pluginContext"; import { BoxSelector } from "./selector"; import { TransactionBuilderSettings } from "./transactionBuilderSettings"; @@ -298,7 +293,8 @@ export class TransactionBuilder { creationHeight: this.#creationHeight, tokens: change.tokens, maxTokensPerBox: this.settings.maxTokensPerChangeBox, - baseIndex: this.outputs.length + 1 + baseIndex: this.outputs.length + 1, + valuePerByte: this.settings.valuePerByte }); while (minRequiredNanoErgs > change.nanoErgs) { @@ -313,13 +309,17 @@ export class TransactionBuilder { creationHeight: this.#creationHeight, tokens: change.tokens, maxTokensPerBox: this.settings.maxTokensPerChangeBox, - baseIndex: this.outputs.length + 1 + baseIndex: this.outputs.length + 1, + valuePerByte: this.settings.valuePerByte }); } const chunkedTokens = chunk(change.tokens, this.#settings.maxTokensPerChangeBox); for (const tokens of chunkedTokens) { - const output = new OutputBuilder(estimateMinBoxValue(), this.#changeAddress) + const output = new OutputBuilder( + estimateMinBoxValue(this.#settings.valuePerByte), + this.#changeAddress + ) .setCreationHeight(this.#creationHeight) .addTokens(tokens) .setFlags({ change: true }); @@ -436,11 +436,12 @@ type ChangeEstimationParams = { tokens: TokenAmount[]; baseIndex: number; maxTokensPerBox: number; + valuePerByte: bigint; }; function estimateMinChangeValue(params: ChangeEstimationParams): bigint { const size = BigInt(estimateChangeSize(params)); - return size * BOX_VALUE_PER_BYTE; + return size * params.valuePerByte; } function estimateChangeSize({ diff --git a/packages/core/src/builder/transactionBuilderSettings.spec.ts b/packages/core/src/builder/transactionBuilderSettings.spec.ts index 528993e6..4c29f767 100644 --- a/packages/core/src/builder/transactionBuilderSettings.spec.ts +++ b/packages/core/src/builder/transactionBuilderSettings.spec.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest"; import { MAX_TOKENS_PER_BOX } from "../models"; +import { BOX_VALUE_PER_BYTE } from "./outputBuilder"; import { TransactionBuilderSettings } from "./transactionBuilderSettings"; describe("Transaction builder settings", () => { @@ -58,4 +59,18 @@ describe("Transaction builder settings", () => { expect(settings.canBurnTokens).toBe(false); expect(settings.canBurnTokensFromPlugins).toBe(true); }); + + it("Should set valuePerByte with bigint, number, or string", () => { + const settings = new TransactionBuilderSettings(); + expect(settings.valuePerByte).toBe(BOX_VALUE_PER_BYTE); + + expect(settings.setValuePerByte(720n)).toBe(settings); + expect(settings.valuePerByte).toBe(720n); + + expect(settings.setValuePerByte(500)).toBe(settings); + expect(settings.valuePerByte).toBe(500n); + + expect(settings.setValuePerByte("1000")).toBe(settings); + expect(settings.valuePerByte).toBe(1000n); + }); }); diff --git a/packages/core/src/builder/transactionBuilderSettings.ts b/packages/core/src/builder/transactionBuilderSettings.ts index e846c904..6266c000 100644 --- a/packages/core/src/builder/transactionBuilderSettings.ts +++ b/packages/core/src/builder/transactionBuilderSettings.ts @@ -1,16 +1,20 @@ +import { type Amount, ensureBigInt } from "@fleet-sdk/common"; import { MAX_TOKENS_PER_BOX } from "../models"; +import { BOX_VALUE_PER_BYTE } from "./outputBuilder"; export class TransactionBuilderSettings { private _maxDistinctTokensPerChangeBox: number; private _allowTokenBurning: boolean; private _allowTokenBurningFromPlugins: boolean; private _isolateErgOnChange: boolean; + private _valuePerByte: bigint; constructor() { this._maxDistinctTokensPerChangeBox = MAX_TOKENS_PER_BOX; this._allowTokenBurning = false; this._allowTokenBurningFromPlugins = false; this._isolateErgOnChange = false; + this._valuePerByte = BOX_VALUE_PER_BYTE; } public get maxTokensPerChangeBox(): number { @@ -29,6 +33,10 @@ export class TransactionBuilderSettings { return this._isolateErgOnChange; } + public get valuePerByte(): bigint { + return this._valuePerByte; + } + /** * Define max number of distinct tokens per change box */ @@ -66,4 +74,13 @@ export class TransactionBuilderSettings { return this; } + + /** + * Define minimum nanoERG value per byte for change boxes + */ + public setValuePerByte(valuePerByte: Amount | number): TransactionBuilderSettings { + this._valuePerByte = ensureBigInt(valuePerByte); + + return this; + } }