Skip to content

Commit

Permalink
Add value/numValidators for ValidatorsExit transactions mappings (saf…
Browse files Browse the repository at this point in the history
…e-global#1880)

Adds `value` and `numValidators` calculations based on the transaction data.
  • Loading branch information
hectorgomezv authored and DenSmolonski committed Oct 24, 2024
1 parent 78e53f0 commit 6429933
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
49 changes: 36 additions & 13 deletions src/routes/transactions/mappers/common/native-staking.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
ChainsRepositoryModule,
IChainsRepository,
} from '@/domain/chains/chains.repository.interface';
import { Chain } from '@/domain/chains/entities/chain.entity';
import { getNumberString } from '@/domain/common/utils/utils';
import { DataDecoded } from '@/domain/data-decoder/entities/data-decoded.entity';
import { ModuleTransaction } from '@/domain/safe/entities/module-transaction.entity';
import { MultisigTransaction } from '@/domain/safe/entities/multisig-transaction.entity';
import { IStakingRepository } from '@/domain/staking/staking.repository.interface';
import { StakingRepositoryModule } from '@/domain/staking/staking.repository.module';
import { NULL_ADDRESS } from '@/routes/common/constants';
Expand Down Expand Up @@ -117,8 +121,7 @@ export class NativeStakingMapper {
chainId: string;
to: `0x${string}`;
value: string | null;
isConfirmed: boolean;
validatorsExitExecutionDate: Date | null;
transaction: MultisigTransaction | ModuleTransaction | null;
}): Promise<NativeStakingValidatorsExitTransactionInfo> {
const chain = await this.chainsRepository.getChain(args.chainId);
const deployment = await this.stakingRepository.getDeployment({
Expand All @@ -138,20 +141,18 @@ export class NativeStakingMapper {
args.chainId,
);

// TODO: value from dataDecoded?
const value = args.value ? Number(args.value) : 0;
const value = args.transaction?.dataDecoded
? this.getValueFromDataDecoded(args.transaction.dataDecoded, chain)
: Number(args.value ?? 0);

const numValidators = Math.floor(
value /
Math.pow(10, chain.nativeCurrency.decimals) /
NativeStakingMapper.ETH_ETHERS_PER_VALIDATOR,
);

return new NativeStakingValidatorsExitTransactionInfo({
status: this.mapValidatorsExitStatus(
networkStats,
args.isConfirmed,
args.validatorsExitExecutionDate,
),
status: this.mapValidatorsExitStatus(networkStats, args.transaction),
estimatedExitTime: networkStats.estimated_exit_time_seconds,
estimatedWithdrawalTime: networkStats.estimated_withdrawal_time_seconds,
value: getNumberString(value),
Expand Down Expand Up @@ -205,26 +206,48 @@ export class NativeStakingMapper {

private mapValidatorsExitStatus(
networkStats: NetworkStats,
isConfirmed: boolean,
validatorsExitExecutionDate: Date | null,
transaction: MultisigTransaction | ModuleTransaction | null,
): StakingValidatorsExitStatus {
const isConfirmed =
transaction &&
'confirmations' in transaction &&
!!transaction.confirmations &&
transaction.confirmations.length >= transaction.confirmationsRequired;

if (!isConfirmed) {
return StakingValidatorsExitStatus.SignatureNeeded;
}

if (!validatorsExitExecutionDate) {
if (!transaction.executionDate) {
return StakingValidatorsExitStatus.AwaitingExecution;
}

// TODO: get validator status from the Kiln API
const estimatedCompletionTime =
validatorsExitExecutionDate.getTime() +
transaction.executionDate.getTime() +
networkStats.estimated_exit_time_seconds * 1000;

return Date.now() <= estimatedCompletionTime
? StakingValidatorsExitStatus.RequestPending
: StakingValidatorsExitStatus.ReadyToWithdraw;
}

private getValueFromDataDecoded(data: DataDecoded, chain: Chain): number {
if (data.method === 'requestValidatorsExit') {
const publicKeys =
data.parameters?.filter(
(parameter) => parameter.name === '_publicKeys',
) ?? [];
const decimals = chain.nativeCurrency.decimals;
return (
publicKeys.length *
Math.pow(10, decimals) *
NativeStakingMapper.ETH_ETHERS_PER_VALIDATOR
);
}

return 0;
}
}

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,11 @@ export class MultisigTransactionInfoMapper {
}

try {
const isConfirmed =
'confirmations' in transaction &&
!!transaction.confirmations &&
transaction.confirmations.length >= transaction.confirmationsRequired;

return await this.nativeStakingMapper.mapValidatorsExitInfo({
chainId,
to: nativeStakingValidatorsExitTransaction.to,
value: transaction.value,
isConfirmed,
validatorsExitExecutionDate: transaction.executionDate,
transaction,
});
} catch (error) {
this.loggingService.warn(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ describe('TransactionsViewController tests', () => {
},
});
});
// TODO: add error cases
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions src/routes/transactions/transactions-view.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@ export class TransactionsViewService {
chainId: args.chainId,
to: args.to,
value: args.value,
isConfirmed: false,
validatorsExitExecutionDate: null,
transaction: null,
});
return new NativeStakingValidatorsExitConfirmationView({
method: args.dataDecoded.method,
Expand Down

0 comments on commit 6429933

Please sign in to comment.