Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra support for more getters for the Validator class #344

Merged
merged 5 commits into from
Dec 17, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Added

- Add getters for `Validator` object to expose more data to users.
- Add test file for `Validator` object.

### [0.11.3] - 2024-12-10

### Added
Expand Down
114 changes: 113 additions & 1 deletion src/coinbase/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Coinbase } from "./coinbase";
import { Validator as ValidatorModel, ValidatorStatus as APIValidatorStatus } from "../client/api";
import {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe some unit tests for these getters ?

Balance,
Validator as ValidatorModel,
ValidatorStatus as APIValidatorStatus,
} from "../client/api";
import { ValidatorStatus } from "./types";

/**
Expand Down Expand Up @@ -150,6 +154,105 @@ export class Validator {
return ValidatorStatus.UNKNOWN;
}
}
/**
* Returns the network ID.
*
* @returns The network ID.
*/
public getNetworkId(): string {
return this.model.network_id;
}

/**
* Returns the asset ID.
*
* @returns The asset ID.
*/
public getAssetId(): string {
return this.model.asset_id;
}

/**
* Returns the activation epoch of the validator.
*
* @returns The activation epoch as a string.
*/
public getActivationEpoch(): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem with this is - these are very ethereum specific.

In future if we say support sol validators, do we still want user to do validator. getActivationEpoch() ?

Do we want to support a helper function of this format instead validator. getEthereumDetails().getActivationEpoch() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be more in favor of revisiting this when we cross that bridge. i.e. I don't see us exposing Solana validator staking anytime soon.

return this.model.details?.activationEpoch || "";
}

/**
* Returns the balance of the validator.
*
* @returns The balance object.
*/
public getBalance(): Balance | undefined {
return this.model.details?.balance;
}

/**
* Returns the effective balance of the validator.
*
* @returns The effective balance object.
*/
public getEffectiveBalance(): Balance | undefined {
return this.model.details?.effective_balance;
}

/**
* Returns the exit epoch of the validator.
*
* @returns The exit epoch as a string.
*/
public getExitEpoch(): string {
return this.model.details?.exitEpoch || "";
}

/**
* Returns the index of the validator.
*
* @returns The validator index as a string.
*/
public getIndex(): string {
return this.model.details?.index || "";
}

/**
* Returns the public key of the validator.
*
* @returns The validator's public key as a string.
*/
public getPublicKey(): string {
return this.model.details?.public_key || "";
}

/**
* Returns whether the validator has been slashed.
*
* @returns True if the validator has been slashed, false otherwise.
*/
public isSlashed(): boolean {
return this.model.details?.slashed || false;
}

/**
* Returns the withdrawable epoch of the validator.
*
* @returns The withdrawable epoch as a string.
*/
public getWithdrawableEpoch(): string {
return this.model.details?.withdrawableEpoch || "";
}

/**
* Returns the withdrawal address of the validator.
*
* @returns The withdrawal address as a string.
*/
public getWithdrawalAddress(): string {
return this.model.details?.withdrawal_address || "";
}

/**
* Returns the string representation of the Validator.
*
Expand All @@ -158,4 +261,13 @@ export class Validator {
public toString(): string {
return `Id: ${this.getValidatorId()} Status: ${this.getStatus()}`;
}

/**
* Returns the JSON representation of the Validator.
*
* @returns The JSON representation of the Validator.
*/
public toJSON(): string {
return JSON.stringify(this.model);
}
}
95 changes: 95 additions & 0 deletions src/tests/validator_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Validator } from "../coinbase/validator";
import { ValidatorStatus } from "../coinbase/types";
import { Validator as ValidatorModel } from "../client";
import { Coinbase } from "../coinbase/coinbase";
import { ValidatorStatus as APIValidatorStatus } from "../client/api";

describe("Validator", () => {
let validator: Validator;

beforeEach(() => {
const mockModel: ValidatorModel = {
validator_id: "123",
status: APIValidatorStatus.Active,
network_id: Coinbase.networks.EthereumHolesky,
asset_id: Coinbase.assets.Eth,
details: {
effective_balance: {
amount: "100",
asset: { network_id: Coinbase.networks.EthereumHolesky, asset_id: Coinbase.assets.Eth },
},
balance: {
amount: "200",
asset: { network_id: Coinbase.networks.EthereumHolesky, asset_id: Coinbase.assets.Eth },
},
exitEpoch: "epoch-1",
activationEpoch: "epoch-0",
index: "0",
public_key: "public-key-123",
slashed: false,
withdrawableEpoch: "epoch-2",
withdrawal_address: "withdrawal-address-123",
},
};

validator = new Validator(mockModel);
});

test("getValidatorId should return the correct validator ID", () => {
expect(validator.getValidatorId()).toBe("123");
});

test("getStatus should return the correct status", () => {
expect(validator.getStatus()).toBe(ValidatorStatus.ACTIVE);
});

test("getNetworkId should return the correct network ID", () => {
expect(validator.getNetworkId()).toBe(Coinbase.networks.EthereumHolesky);
});

test("getAssetId should return the correct asset ID", () => {
expect(validator.getAssetId()).toBe(Coinbase.assets.Eth);
});

test("getActivationEpoch should return the correct activation epoch", () => {
expect(validator.getActivationEpoch()).toBe("epoch-0");
});

test("getExitEpoch should return the correct exit epoch", () => {
expect(validator.getExitEpoch()).toBe("epoch-1");
});

test("getIndex should return the correct index", () => {
expect(validator.getIndex()).toBe("0");
});

test("getPublicKey should return the correct public key", () => {
expect(validator.getPublicKey()).toBe("public-key-123");
});

test("isSlashed should return the correct slashed status", () => {
expect(validator.isSlashed()).toBe(false);
});

test("getWithdrawableEpoch should return the correct withdrawable epoch", () => {
expect(validator.getWithdrawableEpoch()).toBe("epoch-2");
});

test("getWithdrawalAddress should return the correct withdrawal address", () => {
expect(validator.getWithdrawalAddress()).toBe("withdrawal-address-123");
});

test("getEffectiveBalance should return the correct effective balance", () => {
expect(validator.getEffectiveBalance()).toEqual({
amount: "100",
asset: { network_id: Coinbase.networks.EthereumHolesky, asset_id: Coinbase.assets.Eth },
});
});

test("getBalance should return the correct balance", () => {
expect(validator.getBalance()).toEqual({
amount: "200",
asset: { network_id: Coinbase.networks.EthereumHolesky, asset_id: Coinbase.assets.Eth },
});
});
});
Loading