-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathstake_test.ts
More file actions
145 lines (129 loc) · 5.95 KB
/
stake_test.ts
File metadata and controls
145 lines (129 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Validator } from "../coinbase/validator";
import { Coinbase } from "../coinbase/coinbase";
import {
mockEthereumValidator,
mockReturnValue,
stakeApiMock,
VALID_ACTIVE_VALIDATOR_LIST,
} from "./utils";
import { ValidatorStatus } from "../coinbase/types";
import { ValidatorStatus as APIValidatorStatus } from "../client/api";
describe("Validator", () => {
beforeAll(() => {
// Mock the validator functions.
Coinbase.apiClients.stake = stakeApiMock;
});
beforeEach(() => {
jest.clearAllMocks();
});
describe("constructor", () => {
const validatorModel = mockEthereumValidator("100", ValidatorStatus.ACTIVE, "0xpublic_key_1");
const validator = new Validator(validatorModel);
it("initializes a new Validator", () => {
expect(validator).toBeInstanceOf(Validator);
});
it("should raise an error when initialized with a model of a different type", () => {
expect(() => new Validator(null!)).toThrow("Invalid model type");
});
});
describe(".getStatus", () => {
const testCases = [
{ input: APIValidatorStatus.Unknown, expected: ValidatorStatus.UNKNOWN },
{ input: APIValidatorStatus.Provisioning, expected: ValidatorStatus.PROVISIONING },
{ input: APIValidatorStatus.Provisioned, expected: ValidatorStatus.PROVISIONED },
{ input: APIValidatorStatus.Deposited, expected: ValidatorStatus.DEPOSITED },
{ input: APIValidatorStatus.PendingActivation, expected: ValidatorStatus.PENDING_ACTIVATION },
{ input: APIValidatorStatus.Active, expected: ValidatorStatus.ACTIVE },
{ input: APIValidatorStatus.Exiting, expected: ValidatorStatus.EXITING },
{ input: APIValidatorStatus.Exited, expected: ValidatorStatus.EXITED },
{
input: APIValidatorStatus.WithdrawalAvailable,
expected: ValidatorStatus.WITHDRAWAL_AVAILABLE,
},
{
input: APIValidatorStatus.WithdrawalComplete,
expected: ValidatorStatus.WITHDRAWAL_COMPLETE,
},
{ input: APIValidatorStatus.ActiveSlashed, expected: ValidatorStatus.ACTIVE_SLASHED },
{ input: APIValidatorStatus.ExitedSlashed, expected: ValidatorStatus.EXITED_SLASHED },
{ input: APIValidatorStatus.Reaped, expected: ValidatorStatus.REAPED },
{ input: "unknown_status" as APIValidatorStatus, expected: ValidatorStatus.UNKNOWN },
];
testCases.forEach(({ input, expected }) => {
it(`should return ${expected} for ${input}`, () => {
const validatorModel = mockEthereumValidator("100", input, "0xpublic_key_1");
const validator = new Validator(validatorModel);
expect(validator.getStatus()).toBe(expected);
});
});
});
describe("#getAPIValidatorStatus", () => {
const testCases = [
{ input: ValidatorStatus.UNKNOWN, expected: APIValidatorStatus.Unknown },
{ input: ValidatorStatus.PROVISIONING, expected: APIValidatorStatus.Provisioning },
{ input: ValidatorStatus.PROVISIONED, expected: APIValidatorStatus.Provisioned },
{ input: ValidatorStatus.DEPOSITED, expected: APIValidatorStatus.Deposited },
{ input: ValidatorStatus.PENDING_ACTIVATION, expected: APIValidatorStatus.PendingActivation },
{ input: ValidatorStatus.ACTIVE, expected: APIValidatorStatus.Active },
{ input: ValidatorStatus.EXITING, expected: APIValidatorStatus.Exiting },
{ input: ValidatorStatus.EXITED, expected: APIValidatorStatus.Exited },
{
input: ValidatorStatus.WITHDRAWAL_AVAILABLE,
expected: APIValidatorStatus.WithdrawalAvailable,
},
{
input: ValidatorStatus.WITHDRAWAL_COMPLETE,
expected: APIValidatorStatus.WithdrawalComplete,
},
{ input: ValidatorStatus.ACTIVE_SLASHED, expected: APIValidatorStatus.ActiveSlashed },
{ input: ValidatorStatus.EXITED_SLASHED, expected: APIValidatorStatus.ExitedSlashed },
{ input: ValidatorStatus.REAPED, expected: APIValidatorStatus.Reaped },
{ input: "unknown_status" as ValidatorStatus, expected: APIValidatorStatus.Unknown },
];
testCases.forEach(({ input, expected }) => {
it(`should return ${expected} for ${input}`, () => {
const validatorModel = mockEthereumValidator("100", input, "0xpublic_key_1");
const validator = new Validator(validatorModel);
expect(validator.getStatus()).toBe(expected);
});
});
});
it("should return a list of validators for ethereum hoodi and eth asset", async () => {
Coinbase.apiClients.stake!.listValidators = mockReturnValue(VALID_ACTIVE_VALIDATOR_LIST);
const validators = await Validator.list(
Coinbase.networks.EthereumHoodi,
Coinbase.assets.Eth,
ValidatorStatus.ACTIVE,
);
expect(Coinbase.apiClients.stake!.listValidators).toHaveBeenCalledWith(
Coinbase.networks.EthereumHoodi,
Coinbase.assets.Eth,
ValidatorStatus.ACTIVE,
);
expect(validators.length).toEqual(3);
expect(validators[0].getValidatorId()).toEqual("0xpublic_key_1");
expect(validators[0].getStatus()).toEqual(ValidatorStatus.ACTIVE);
expect(validators[1].getValidatorId()).toEqual("0xpublic_key_2");
expect(validators[1].getStatus()).toEqual(ValidatorStatus.ACTIVE);
expect(validators[2].getValidatorId()).toEqual("0xpublic_key_3");
expect(validators[2].getStatus()).toEqual(ValidatorStatus.ACTIVE);
});
it("should return a validator for ethereum hoodi and eth asset", async () => {
Coinbase.apiClients.stake!.getValidator = mockReturnValue(
mockEthereumValidator("100", ValidatorStatus.EXITING, "0x123"),
);
const validator = await Validator.fetch(
Coinbase.networks.EthereumHoodi,
Coinbase.assets.Eth,
"0x123",
);
expect(Coinbase.apiClients.stake!.getValidator).toHaveBeenCalledWith(
Coinbase.networks.EthereumHoodi,
Coinbase.assets.Eth,
"0x123",
);
expect(validator.getValidatorId()).toEqual("0x123");
expect(validator.getStatus()).toEqual(ValidatorStatus.EXITING);
expect(validator.toString()).toEqual("Id: 0x123 Status: exiting");
});
});