Skip to content

Commit

Permalink
Add transcoder for stake transaction metadata. Add support for approv…
Browse files Browse the repository at this point in the history
…e component for this new txn type.
  • Loading branch information
Lazy Nina authored and lazynina committed Jan 29, 2024
1 parent 34af38b commit 2cd728d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/app/approve/approve.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
TransactionSpendingLimit,
TransactionMetadataRegisterAsValidator,
TransactionMetadataUnregisterAsValidator,
TransactionMetadataStake,
} from '../../lib/deso/transaction';
import { ExtraData } from '../../types/identity';
import { AccountService } from '../account.service';
Expand Down Expand Up @@ -597,6 +598,18 @@ export class ApproveComponent implements OnInit {
case TransactionMetadataUnregisterAsValidator:
description = 'unregister as a validator';
break;
case TransactionMetadataStake:
const stakeMetadata = this.transaction
.metadata as TransactionMetadataStake;
const stakeValidatorPublicKey = this.base58KeyCheck(
stakeMetadata.validatorPublicKey
);
publicKeys = [stakeValidatorPublicKey];
const stakeAmountNanos = this.hexNanosToUnitString(
stakeMetadata.stakeAmountNanos
);
description = `stake ${stakeAmountNanos} $DESO to ${stakeValidatorPublicKey}`;
break;
}

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

// TODO: create hexBaseUnitsToUnitString function to support proper
// DAO Coin base unit conversions.
hexNanosToUnitString(nanos: Buffer): string {
return this.nanosToUnitString(parseInt(nanos.toString('hex'), 16));
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/identity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
TransactionMetadataUpdateProfile,
TransactionMetadataRegisterAsValidator,
TransactionMetadataUnregisterAsValidator,
TransactionMetadataStake,
} from '../lib/deso/transaction';
import { SwalHelper } from '../lib/helpers/swal-helper';
import { AccessLevel, PublicUserInfo } from '../types/identity';
Expand Down Expand Up @@ -522,6 +523,7 @@ export class IdentityService {
case TransactionMetadataDAOCoinLimitOrder:
case TransactionMetadataRegisterAsValidator:
case TransactionMetadataUnregisterAsValidator:
case TransactionMetadataStake:
return AccessLevel.Full;

case TransactionMetadataFollow:
Expand Down
20 changes: 20 additions & 0 deletions src/lib/bindata/transcoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ export function Optional<T>(transcoder: Transcoder<T>): Transcoder<T | null> {
};
}

export function BoolOptional<T>(
transcoder: Transcoder<T>
): Transcoder<T | null> {
return {
read: (bytes: Buffer) => {
const existence = bytes.readUInt8(0) != 0;
if (!existence) {
return [null, bytes.slice(1)];
}
return transcoder.read(bytes.slice(1));
},
write: (value: T | null) => {
if (value === null) {
return Buffer.alloc(1);
}
return Buffer.concat([Buffer.alloc(1, 1), transcoder.write(value)]);
},
};
}

export const ChunkBuffer = (width: number): Transcoder<Buffer[]> => ({
read: (bytes) => {
let [count, buffer] = bufToUvarint64(bytes);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/deso/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BinaryRecord, Transcode } from '../bindata';
import {
ArrayOf,
Boolean,
BoolOptional,
ChunkBuffer,
Enum,
FixedBuffer,
Expand Down Expand Up @@ -609,6 +610,18 @@ export class TransactionMetadataRegisterAsValidator extends TransactionMetadata

export class TransactionMetadataUnregisterAsValidator extends TransactionMetadata {}

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

@Transcode(Uint8)
rewardMethod: number = 0;

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

export const TransactionTypeMetadataMap = {
1: TransactionMetadataBlockReward,
2: TransactionMetadataBasicTransfer,
Expand Down Expand Up @@ -644,6 +657,7 @@ export const TransactionTypeMetadataMap = {
33: TransactionMetadataNewMessage,
34: TransactionMetadataRegisterAsValidator,
35: TransactionMetadataUnregisterAsValidator,
36: TransactionMetadataStake,
};

export class Transaction extends BinaryRecord {
Expand Down

0 comments on commit 2cd728d

Please sign in to comment.