Skip to content

Commit

Permalink
Add support for coin lockup txn types
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazy Nina committed Jan 9, 2024
1 parent 9303c55 commit 6d711bc
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
86 changes: 84 additions & 2 deletions src/app/approve/approve.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import {
TransactionMetadataUnstake,
TransactionMetadataUnlockStake,
TransactionMetadataUnjailValidator,
TransactionMetadataCoinLockup,
TransactionMetadataUpdateCoinLockupParams,
TransactionMetadataCoinLockupTransfer,
TransactionMetadataCoinUnlock,
} from '../../lib/deso/transaction';
import { ExtraData } from '../../types/identity';
import { AccountService } from '../account.service';
Expand Down Expand Up @@ -639,6 +643,76 @@ export class ApproveComponent implements OnInit {
case TransactionMetadataUnjailValidator:
description = 'unjail your validator';
break;
case TransactionMetadataCoinLockup:
// TODO: enhanced description for coin lockup.
// Do we need to special case lock ups of DESO?
const coinLockupMetadata = this.transaction
.metadata as TransactionMetadataCoinLockup;
const lockupAmount = this.hexBaseUnitsToUnitString(
coinLockupMetadata.lockupAmountBaseUnits
);
const lockupProfilePubKey = this.base58KeyCheck(
coinLockupMetadata.profilePublicKey
);
const lockUpRecipientPubKey = this.base58KeyCheck(
coinLockupMetadata.recipientPublicKey
);
publicKeys = [lockupProfilePubKey, lockUpRecipientPubKey];
description = `lockup ${lockupAmount} of your ${lockupProfilePubKey} coins`;
break;
case TransactionMetadataUpdateCoinLockupParams:
// TODO: enhanced description for coin lockup params
const updateCoinLockupParamsMetadata = this.transaction
.metadata as TransactionMetadataUpdateCoinLockupParams;
description =
`update your coin lockup params\n` +
`${
updateCoinLockupParamsMetadata.removeYieldCurvePoint
? 'Remove'
: 'Add'
} yield curve point with
${
updateCoinLockupParamsMetadata.lockupYieldAPYBasisPoints / 100
}% APY for a lockup of
${
updateCoinLockupParamsMetadata.lockupYieldDurationNanoSecs
} nanoseconds
${
!updateCoinLockupParamsMetadata.newLockupTransferRestrictions
? ''
: 'and update your lockup transfer restrictions'
}`;

break;
case TransactionMetadataCoinLockupTransfer:
// TODO: enhanced description for coin lockup transfer
// Do we need a special case for DESO?
const coinLockupTransferMetadata = this.transaction
.metadata as TransactionMetadataCoinLockupTransfer;
const lockupTransferAmount = this.hexBaseUnitsToUnitString(
coinLockupTransferMetadata.lockedCoinsToTransferBaseUnits
);
const lockupTransferProfilePubKey = this.base58KeyCheck(
coinLockupTransferMetadata.profilePublicKey
);
const lockupTransferRecipientPubKey = this.base58KeyCheck(
coinLockupTransferMetadata.recipientPublicKey
);
publicKeys = [
lockupTransferProfilePubKey,
lockupTransferRecipientPubKey,
];
description = `transfer ${lockupTransferAmount} of your locked ${lockupTransferProfilePubKey} coins to ${lockupTransferRecipientPubKey}`;
break;
case TransactionMetadataCoinUnlock:
const coinUnlockMetadata = this.transaction
.metadata as TransactionMetadataCoinUnlock;
const unlockProfilePubKey = this.base58KeyCheck(
coinUnlockMetadata.profilePublicKey
);
publicKeys = [unlockProfilePubKey];
description = `unlock your locked ${unlockProfilePubKey} coins`;
break;
}

// Set the transaction description based on the description populated with public keys.
Expand All @@ -659,16 +733,24 @@ export class ApproveComponent implements OnInit {
return bs58check.encode(Buffer.from([...prefix, ...keyBytes]));
}

// TODO: create hexBaseUnitsToUnitString function to support proper
// DAO Coin base unit conversions.
// uint256 to DESO nano conversions.
hexNanosToUnitString(nanos: Buffer): string {
return this.nanosToUnitString(parseInt(nanos.toString('hex'), 16));
}

// unit256 to base unit conversions.
hexBaseUnitsToUnitString(baseUnits: Buffer): string {
return this.baseUnitsToUnitString(parseInt(baseUnits.toString('hex'), 16));
}

nanosToUnitString(nanos: number): string {
return this.toFixedLengthDecimalString(nanos / 1e9);
}

baseUnitsToUnitString(baseUnits: number): string {
return this.toFixedLengthDecimalString(baseUnits / 1e18);
}

hexScaledExchangeRateToFloat(hex: Buffer): number {
return parseInt(hex.toString('hex'), 16) / 1e38;
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/identity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import {
TransactionMetadataUnstake,
TransactionMetadataUnlockStake,
TransactionMetadataUnjailValidator,
TransactionMetadataCoinLockup,
TransactionMetadataUpdateCoinLockupParams,
TransactionMetadataCoinLockupTransfer,
TransactionMetadataCoinUnlock,
} from '../lib/deso/transaction';
import { SwalHelper } from '../lib/helpers/swal-helper';
import { AccessLevel, PublicUserInfo } from '../types/identity';
Expand Down Expand Up @@ -529,6 +533,10 @@ export class IdentityService {
case TransactionMetadataStake:
case TransactionMetadataUnstake:
case TransactionMetadataUnlockStake:
case TransactionMetadataCoinLockup:
case TransactionMetadataUpdateCoinLockupParams:
case TransactionMetadataCoinLockupTransfer:
case TransactionMetadataCoinUnlock:
return AccessLevel.Full;

case TransactionMetadataFollow:
Expand Down
63 changes: 63 additions & 0 deletions src/lib/deso/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,65 @@ export class TransactionMetadataUnlockStake extends TransactionMetadata {

export class TransactionMetadataUnjailValidator extends TransactionMetadata {}

export class TransactionMetadataCoinLockup extends TransactionMetadata {
@Transcode(VarBuffer)
profilePublicKey: Buffer = Buffer.alloc(0);

@Transcode(VarBuffer)
recipientPublicKey: Buffer = Buffer.alloc(0);

// TODO: Implement Varint64
@Transcode(Uvarint64)
unlockTimestampNanoSecs: number = 0;

// TODO: Implement Varint64
@Transcode(Uvarint64)
vestingEndTimestampNanoSecs: number = 0;

// TODO: We may want a better way to handle uint256s.
@Transcode(Optional(VarBuffer))
lockupAmountBaseUnits: Buffer = Buffer.alloc(0);
}

export class TransactionMetadataUpdateCoinLockupParams extends TransactionMetadata {
// TODO: Implement Varint64
@Transcode(Uvarint64)
lockupYieldDurationNanoSecs: number = 0;

@Transcode(Uvarint64)
lockupYieldAPYBasisPoints: number = 0;

@Transcode(Boolean)
removeYieldCurvePoint: boolean = false;

@Transcode(Boolean)
newLockupTransferRestrictions: boolean = false;

@Transcode(Uint8)
lockupTransferRestrictionStatus: number = 0;
}

export class TransactionMetadataCoinLockupTransfer extends TransactionMetadata {
@Transcode(VarBuffer)
recipientPublicKey: Buffer = Buffer.alloc(0);

@Transcode(VarBuffer)
profilePublicKey: Buffer = Buffer.alloc(0);

// TODO: implement varint64
@Transcode(Uvarint64)
unlockTimestampNanoSecs: number = 0;

// TODO: We may want a better way to handle uint256s.
@Transcode(Optional(VarBuffer))
lockedCoinsToTransferBaseUnits: Buffer = Buffer.alloc(0);
}

export class TransactionMetadataCoinUnlock extends TransactionMetadata {
@Transcode(VarBuffer)
profilePublicKey: Buffer = Buffer.alloc(0);
}

export const TransactionTypeMetadataMap = {
1: TransactionMetadataBlockReward,
2: TransactionMetadataBasicTransfer,
Expand Down Expand Up @@ -682,6 +741,10 @@ export const TransactionTypeMetadataMap = {
37: TransactionMetadataUnstake,
38: TransactionMetadataUnlockStake,
39: TransactionMetadataUnjailValidator,
40: TransactionMetadataCoinLockup,
41: TransactionMetadataUpdateCoinLockupParams,
42: TransactionMetadataCoinLockupTransfer,
43: TransactionMetadataCoinUnlock,
};

export class Transaction extends BinaryRecord {
Expand Down

0 comments on commit 6d711bc

Please sign in to comment.