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
3 changes: 2 additions & 1 deletion apps/core/src/constants/timelock.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

export const TIMELOCK_MODULE = 'timelock';
export const TIMELOCKED_STAKING_MODULE = 'timelocked_staking';
export const TIMELOCK_IOTA_TYPE = `0x2::${TIMELOCK_MODULE}::TimeLock<0x2::balance::Balance<0x2::iota::IOTA>>`;
export const TIMELOCK_STAKED_TYPE = `0x3::timelocked_staking::TimelockedStakedIota`;
export const TIMELOCK_STAKED_TYPE = `0x3::${TIMELOCKED_STAKING_MODULE}::TimelockedStakedIota`;
1 change: 0 additions & 1 deletion apps/core/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export * from './useQueryTransactionsByAddress';
export * from './useGetTransaction';
export * from './useSortedCoinsByCategories';
export * from './useGetNFTDisplay';
export * from './useUnlockTimelockedObjectsTransaction';
export * from './useGetAllOwnedObjects';
export * from './useGetTimelockedStakedObjects';
export * from './useGetStakingValidatorDetails';
Expand Down
30 changes: 0 additions & 30 deletions apps/core/src/hooks/useUnlockTimelockedObjectsTransaction.ts

This file was deleted.

2 changes: 2 additions & 0 deletions apps/core/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export * from './gasSummary';
export * from './transactionExecute';
export * from './validators';
export * from './schema';
export * from './nestedResult';
export * from './stakeObject';
7 changes: 7 additions & 0 deletions apps/core/src/types/nestedResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2026 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

export type NestedResultType = {
$kind: 'NestedResult';
NestedResult: [number, number];
};
30 changes: 30 additions & 0 deletions apps/core/src/types/stakeObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2026 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

// Timelocked stake: fields.staked_iota.fields.{pool_id, stake_activation_epoch}
export interface TimelockedStakeObjectInput {
objectId: string;
content: {
dataType: 'moveObject';
fields: {
staked_iota: {
fields: {
pool_id: string;
stake_activation_epoch: string;
};
};
};
};
}

// Regular stake: fields.{pool_id, stake_activation_epoch}
export interface RegularStakeObjectInput {
objectId: string;
content: {
dataType: 'moveObject';
fields: {
pool_id: string;
stake_activation_epoch: string;
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import {
NftOutputObject,
NftOutputObjectSchema,
} from './types';

type NestedResultType = {
$kind: 'NestedResult';
NestedResult: [number, number];
};
import { NestedResultType } from '../../types';

export async function getNativeTokensFromBag(bagId: string, client: IotaClient) {
const nativeTokenDynamicFields = await client.getDynamicFields({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) 2026 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Transaction } from '@iota/iota-sdk/transactions';
import {
IOTA_TYPE_ARG,
IOTA_FRAMEWORK_ADDRESS,
IOTA_CLOCK_OBJECT_ID,
IOTA_SYSTEM_ADDRESS,
} from '@iota/iota-sdk/utils';
import { NestedResultType, RegularStakeObjectInput, TimelockedStakeObjectInput } from '../../types';

interface CreateCollectAllTimelocksTransactionOptions {
address: string;
timelockObjectIds: string[];
timelockedStakedObjects?: TimelockedStakeObjectInput[];
existingStakedObjects?: RegularStakeObjectInput[];
}

export function createCollectAllTimelocksTransaction({
address,
timelockObjectIds,
timelockedStakedObjects = [],
existingStakedObjects = [],
}: CreateCollectAllTimelocksTransactionOptions) {
const ptb = new Transaction();
const coins: NestedResultType[] = [];

// Unlock regular timelocks and convert to coins
for (const objectId of timelockObjectIds) {
const [unlock] = ptb.moveCall({
target: `${IOTA_FRAMEWORK_ADDRESS}::timelock::unlock_with_clock`,
typeArguments: [`${IOTA_FRAMEWORK_ADDRESS}::balance::Balance<${IOTA_TYPE_ARG}>`],
arguments: [ptb.object(objectId), ptb.object(IOTA_CLOCK_OBJECT_ID)],
});

const [coin] = ptb.moveCall({
target: `${IOTA_FRAMEWORK_ADDRESS}::coin::from_balance`,
typeArguments: [IOTA_TYPE_ARG],
arguments: [ptb.object(unlock)],
});

coins.push(coin);
}

// Unlock timelock stakes and group by (pool_id, stake_activation_epoch)
const stakedIotaByKey = new Map<string, NestedResultType[]>();

for (const stakedObject of timelockedStakedObjects) {
const [unlockedStakedIota] = ptb.moveCall({
target: `${IOTA_SYSTEM_ADDRESS}::timelocked_staking::unlock_with_clock`,
arguments: [ptb.object(stakedObject.objectId), ptb.object(IOTA_CLOCK_OBJECT_ID)],
});

const poolKey = extractPoolKey(stakedObject);
if (poolKey) {
if (!stakedIotaByKey.has(poolKey)) {
stakedIotaByKey.set(poolKey, []);
}
stakedIotaByKey.get(poolKey)!.push(unlockedStakedIota);
} else {
ptb.transferObjects([unlockedStakedIota], ptb.pure.address(address));
}
}

for (const [poolKey, stakedIotaObjects] of stakedIotaByKey.entries()) {
const existingStake = findExistingStakeForKey(existingStakedObjects, poolKey);

if (existingStake) {
// Join all unlocked stakes into the existing regular stake
for (const stake of stakedIotaObjects) {
ptb.moveCall({
target: `${IOTA_SYSTEM_ADDRESS}::staking_pool::join_staked_iota`,
arguments: [ptb.object(existingStake.objectId), stake],
});
}
} else if (stakedIotaObjects.length === 1) {
ptb.transferObjects([stakedIotaObjects[0]], ptb.pure.address(address));
} else {
// Join all into the first one, then transfer
const [first, ...rest] = stakedIotaObjects;
for (const stake of rest) {
ptb.moveCall({
target: `${IOTA_SYSTEM_ADDRESS}::staking_pool::join_staked_iota`,
arguments: [first, stake],
});
}
ptb.transferObjects([first], ptb.pure.address(address));
}
}

// Transfer all collected coins
if (coins.length > 0) {
ptb.transferObjects(coins, ptb.pure.address(address));
}

return ptb;
}

function extractPoolKey(stakedObject: TimelockedStakeObjectInput): string | null {
const stakedIotaFields = stakedObject.content.fields.staked_iota?.fields;
if (stakedIotaFields?.pool_id && stakedIotaFields?.stake_activation_epoch) {
return `${stakedIotaFields.pool_id}:${stakedIotaFields.stake_activation_epoch}`;
}
return null;
}

function findExistingStakeForKey(
existingStakes: RegularStakeObjectInput[],
poolKey: string,
): RegularStakeObjectInput | undefined {
return existingStakes.find(
(s) => `${s.content.fields.pool_id}:${s.content.fields.stake_activation_epoch}` === poolKey,
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Transaction } from '@iota/iota-sdk/transactions';
import { IOTA_TYPE_ARG, IOTA_FRAMEWORK_ADDRESS, IOTA_CLOCK_OBJECT_ID } from '@iota/iota-sdk/utils';
import { createCollectAllTimelocksTransaction } from './createCollectAllTimelocksTransaction';

interface CreateUnlockTimelockedObjectTransactionOptions {
address: string;
Expand All @@ -13,25 +12,8 @@ export function createUnlockTimelockedObjectsTransaction({
address,
objectIds,
}: CreateUnlockTimelockedObjectTransactionOptions) {
const ptb = new Transaction();
const coins: { $kind: 'NestedResult'; NestedResult: [number, number] }[] = [];

for (const objectId of objectIds) {
const [unlock] = ptb.moveCall({
target: `${IOTA_FRAMEWORK_ADDRESS}::timelock::unlock_with_clock`,
typeArguments: [`${IOTA_FRAMEWORK_ADDRESS}::balance::Balance<${IOTA_TYPE_ARG}>`],
arguments: [ptb.object(objectId), ptb.object(IOTA_CLOCK_OBJECT_ID)],
});

// Convert Balance to Coin
const [coin] = ptb.moveCall({
target: `${IOTA_FRAMEWORK_ADDRESS}::coin::from_balance`,
typeArguments: [IOTA_TYPE_ARG],
arguments: [ptb.object(unlock)],
});

coins.push(coin);
}
ptb.transferObjects(coins, ptb.pure.address(address));
return ptb;
return createCollectAllTimelocksTransaction({
address,
timelockObjectIds: objectIds,
});
}
1 change: 1 addition & 0 deletions apps/core/src/utils/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export * from './createTokenTransferTransaction';
export * from './getObjectDisplayLookup';
export * from './createNftSendValidationSchema';
export * from './createUnlockTimelockedObjectsTransaction';
export * from './createCollectAllTimelocksTransaction';
export * from './isMigrationTransaction';
export * from './isUnlockTimelockedObjectTransaction';
Loading
Loading