Skip to content
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
26 changes: 26 additions & 0 deletions client/src/utils/buildUtxoBytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,32 @@ describe("buildUtxoBytes", () => {
expect(parsedUtxo).toBeDefined();
});

test("handles stakeable locktime", () => {
const txHash = "2R5bJqAd6evMJAuV4TYGqfaHkdCEQfYUx4GoHpJZxsFeor6wMi";
const outputIndex = 0;
const assetId = "U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK";
const amount = "50000000000";
const addresses = [account1.getXPAddress("P", "fuji")];
const threshold = 1;
const locktime = "1672531200"; // Some future timestamp
const stakeableLocktime = "1672531200"; // Some future timestamp

const result = buildUtxoBytes(
txHash,
outputIndex,
assetId,
amount,
addresses,
locktime,
threshold,
stakeableLocktime
);

expect(result).toMatch(/^0x/);
const parsedUtxo = getUtxoFromBytes(result, "P");
expect(parsedUtxo).toBeDefined();
});

test("handles different thresholds", () => {
const txHash = "2R5bJqAd6evMJAuV4TYGqfaHkdCEQfYUx4GoHpJZxsFeor6wMi";
const outputIndex = 0;
Expand Down
12 changes: 10 additions & 2 deletions client/src/utils/buildUtxoBytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Id,
Int,
OutputOwners,
pvmSerial,
Short,
TransferOutput,
utils,
Expand All @@ -19,6 +20,7 @@ import {
* @param addresses - The addresses who can sign the transaction for consuming this output.
* @param locktime - The UNIX timestamp in seconds after which this output can be consumed.
* @param threshold - The threshold of the addresses who can sign the transaction for consuming this output.
* @param stakeableLocktime - The stakeable locktime in seconds before which this output can only be used as staking input.
* @returns The UTXO hex string.
*
* @example
Expand All @@ -42,7 +44,8 @@ export function buildUtxoBytes(
amount: string,
addresses: string[],
locktime: string,
threshold: number
threshold: number,
stakeableLocktime = '0',
): `0x${string}` {
const transferOutput = new TransferOutput(
new BigIntPr(BigInt(amount)),
Expand All @@ -60,10 +63,15 @@ export function buildUtxoBytes(
)
);

const stakeableLockOut = new pvmSerial.StakeableLockOut(
new BigIntPr(BigInt(stakeableLocktime ?? 0)),
transferOutput,
);

const utxo = new Utxo(
new avaxSerial.UTXOID(Id.fromString(txHash), new Int(outputIndex)),
Id.fromString(assetId),
transferOutput
stakeableLocktime !== '0' ? stakeableLockOut : transferOutput
);

return utils.bufferToHex(
Expand Down