|
| 1 | +import { |
| 2 | + IsString, |
| 3 | + IsNotEmpty, |
| 4 | + IsInt, |
| 5 | + IsPositive, |
| 6 | + IsOptional, |
| 7 | + Min, |
| 8 | + Max, |
| 9 | + IsIn, |
| 10 | +} from 'class-validator'; |
| 11 | +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; |
| 12 | +import { Type } from 'class-transformer'; |
| 13 | +import type { PolicyTypeEnum, RegionTierEnum } from '../../rpc/soroban.service'; |
| 14 | + |
| 15 | +export class BuildRenewalTransactionDto { |
| 16 | + @ApiProperty({ description: 'Stellar address of the policyholder', example: 'GABC...' }) |
| 17 | + @IsString() |
| 18 | + @IsNotEmpty() |
| 19 | + holder!: string; |
| 20 | + |
| 21 | + @ApiProperty({ description: 'Per-holder monotonic policy ID (u32)', example: 1 }) |
| 22 | + @Type(() => Number) |
| 23 | + @IsInt() |
| 24 | + @IsPositive() |
| 25 | + policy_id!: number; |
| 26 | + |
| 27 | + /** |
| 28 | + * Age is required to deterministically recalculate the renewal premium using |
| 29 | + * the same on-chain formula as policy initiation. Must match the original |
| 30 | + * policy parameters to avoid premium manipulation. |
| 31 | + */ |
| 32 | + @ApiProperty({ description: 'Policyholder age (must match original policy)', example: 35 }) |
| 33 | + @Type(() => Number) |
| 34 | + @IsInt() |
| 35 | + @Min(18) |
| 36 | + @Max(120) |
| 37 | + age!: number; |
| 38 | + |
| 39 | + /** |
| 40 | + * Risk score [0–100] used in the premium formula. Must be provided by the |
| 41 | + * caller and validated against the original policy to prevent replay attacks |
| 42 | + * where a lower risk score is substituted to reduce the renewal premium. |
| 43 | + */ |
| 44 | + @ApiProperty({ description: 'Risk score 0–100 (must match original policy)', example: 5 }) |
| 45 | + @Type(() => Number) |
| 46 | + @IsInt() |
| 47 | + @Min(0) |
| 48 | + @Max(100) |
| 49 | + risk_score!: number; |
| 50 | + |
| 51 | + /** |
| 52 | + * Optional override for renewal duration in ledgers. |
| 53 | + * Defaults to POLICY_DURATION_LEDGERS (~1 year) if omitted. |
| 54 | + */ |
| 55 | + @ApiPropertyOptional({ description: 'Renewal duration in ledgers (default: ~1 year)', example: 6307200 }) |
| 56 | + @IsOptional() |
| 57 | + @Type(() => Number) |
| 58 | + @IsInt() |
| 59 | + @IsPositive() |
| 60 | + duration_ledgers?: number; |
| 61 | + |
| 62 | + /** |
| 63 | + * SEP-41 asset contract ID for premium payment. |
| 64 | + * Must match the asset bound to the original policy (assetContractId). |
| 65 | + * Omit to use the policy's existing asset. |
| 66 | + */ |
| 67 | + @ApiPropertyOptional({ description: 'SEP-41 asset contract ID (defaults to policy asset)' }) |
| 68 | + @IsOptional() |
| 69 | + @IsString() |
| 70 | + asset?: string; |
| 71 | +} |
| 72 | + |
| 73 | +export class RenewalQuoteResponseDto { |
| 74 | + @ApiProperty({ description: 'Renewal premium in stroops (i128 as string)', example: '50000000' }) |
| 75 | + premiumStroops!: string; |
| 76 | + |
| 77 | + @ApiProperty({ description: 'Renewal premium in XLM', example: '5.0000000' }) |
| 78 | + premiumXlm!: string; |
| 79 | + |
| 80 | + @ApiProperty({ description: 'Previous policy expiry ledger', example: 1000000 }) |
| 81 | + previousEndLedger!: number; |
| 82 | + |
| 83 | + @ApiProperty({ description: 'New policy expiry ledger after renewal', example: 7307200 }) |
| 84 | + newEndLedger!: number; |
| 85 | + |
| 86 | + @ApiProperty({ description: 'Current ledger at time of quote', example: 950000 }) |
| 87 | + currentLedger!: number; |
| 88 | + |
| 89 | + @ApiProperty({ description: 'Ledger at which the renewal window opened', example: 879040 }) |
| 90 | + windowOpenLedger!: number; |
| 91 | + |
| 92 | + @ApiProperty({ description: 'Exclusive ledger at which the renewal window closes', example: 1017280 }) |
| 93 | + windowCloseLedger!: number; |
| 94 | + |
| 95 | + @ApiProperty({ description: 'Whether the premium was computed on-chain or via local fallback' }) |
| 96 | + premiumSource!: 'simulation' | 'local_fallback'; |
| 97 | +} |
| 98 | + |
| 99 | +export class BuildRenewalTransactionResponseDto extends RenewalQuoteResponseDto { |
| 100 | + @ApiProperty({ description: 'Base64-encoded unsigned transaction XDR for wallet signing' }) |
| 101 | + unsignedXdr!: string; |
| 102 | + |
| 103 | + @ApiProperty({ description: 'Minimum resource fee in stroops' }) |
| 104 | + minResourceFee!: string; |
| 105 | + |
| 106 | + @ApiProperty({ description: 'Base fee in stroops' }) |
| 107 | + baseFee!: string; |
| 108 | + |
| 109 | + @ApiProperty({ description: 'Total estimated fee in stroops' }) |
| 110 | + totalEstimatedFee!: string; |
| 111 | + |
| 112 | + @ApiProperty({ description: 'Total estimated fee in XLM' }) |
| 113 | + totalEstimatedFeeXlm!: string; |
| 114 | + |
| 115 | + @ApiProperty({ description: 'Addresses that must sign Soroban auth entries' }) |
| 116 | + authRequirements!: Array<{ address: string; isContract: boolean }>; |
| 117 | + |
| 118 | + @ApiProperty({ description: 'Memo convention note' }) |
| 119 | + memoConvention!: string; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * PolicyRenewed event payload — emitted exactly once per successful renewal. |
| 124 | + * Structured for off-chain indexing and timeline UIs. |
| 125 | + * |
| 126 | + * EVENT INTEGRITY: |
| 127 | + * - Emitted only after the unsigned transaction is successfully assembled. |
| 128 | + * - Contains previousEndLedger and newEndLedger for timeline reconstruction. |
| 129 | + * - termVersion allows UIs to detect term changes between renewal cycles. |
| 130 | + * - premiumPaidStroops enables cumulative premium tracking with checked arithmetic. |
| 131 | + */ |
| 132 | +export interface PolicyRenewedEvent { |
| 133 | + /** Composite policy key: holderAddress:policyId */ |
| 134 | + policyCompositeId: string; |
| 135 | + holderAddress: string; |
| 136 | + policyId: number; |
| 137 | + /** Ledger at which the previous term expired (inclusive end of old term). */ |
| 138 | + previousEndLedger: number; |
| 139 | + /** Ledger at which the new term expires (inclusive end of new term). */ |
| 140 | + newEndLedger: number; |
| 141 | + /** Renewal premium in stroops (i128 as string) — use BigInt for arithmetic. */ |
| 142 | + premiumPaidStroops: string; |
| 143 | + /** Term version hash or identifier, if the policy terms changed at renewal. */ |
| 144 | + termVersion: string | null; |
| 145 | + /** Ledger at which the renewal was requested (for audit trail). */ |
| 146 | + renewalRequestedAtLedger: number; |
| 147 | + /** ISO-8601 wall-clock timestamp of the renewal request (approximate). */ |
| 148 | + renewalRequestedAt: string; |
| 149 | +} |
0 commit comments