-
Notifications
You must be signed in to change notification settings - Fork 2
/
StakingClient.ts
151 lines (141 loc) · 5.18 KB
/
StakingClient.ts
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
146
147
148
149
150
151
// @ts-ignore
import { stabilityPool as backend } from '@xbacked-dao/xbacked-contracts';
import { Account } from './Account';
import { AccountInterface, StakeGlobalView, StakeLocalView } from './interfaces';
export class StakingClient extends Account {
/** @property Unique identifier for the contract */
readonly id: number | undefined;
constructor(params: AccountInterface, contractId: number) {
super(params);
this.id = contractId;
}
/**
* @description Get global state of contract
* @param params account object that contains the reach account
* @returns StakeGlobalView
*/
async getState(params: { account: Account }): Promise<StakeGlobalView> {
const ctc = params.account.reachAccount.contract(backend, this.id);
const get = ctc.v.State;
const stateView = await get.read();
if (stateView[0] === 'None') {
throw new Error('The view is null');
}
const stakeState = stateView[1];
return {
stakingAssetID: stakeState.stakingAsset.toNumber(),
policy: {
applicationType: stakeState.policy.applicationType.toNumber(),
status: stakeState.policy.status.toNumber(),
time: stakeState.policy.time.toNumber(),
rewardMethod: stakeState.policy.rewardMethod.toNumber(),
rewardRate: stakeState.policy.rewardRate.toNumber(),
},
totalStake: stakeState.totalStake.toNumber(),
totalUsers: stakeState.totalUsers.toNumber(),
totalRewards: stakeState.totalRewards.toNumber(),
};
}
/**
* @description Get local state of user
* @param params account object that contains the reach account, address for the look up
* @returns StakeLocalView
*/
async getUserInfo(params: { account: Account; address: string }): Promise<StakeLocalView> {
const ctc = params.account.reachAccount.contract(backend, this.id);
const get = ctc.v.State;
const stateView = await get.readUserAccount(params.address);
if (stateView[1][0] === 'None') {
return {
amountStaked: stateView.amountStaked.toNumber(),
rewardsClaimed: stateView.rewardsClaimed.toNumber(),
lastClaim: stateView.lastClaim.toNumber(),
found: false,
};
}
const stakingState = stateView[1][1];
return { ...StakingClient.parseUserInfo(stakingState), found: true };
}
/**
* @description stake assets to the contract
* @param amount amount to be staked
* @returns boolean
*/
async stakeAsset(amount: number): Promise<boolean> {
await this.initialiseReachAccount();
const ctc = this.reachAccount.contract(backend, this.id);
const put = ctc.a.LiquidationVaultAPI;
return put.stakeAsset(amount);
}
/**
* @description unstake assets from the contract
* @param amount amount to be unstaked
* @returns boolean
*/
async unstakeAsset(amount: number): Promise<boolean> {
await this.initialiseReachAccount();
const ctc = this.reachAccount.contract(backend, this.id);
const put = ctc.a.LiquidationVaultAPI;
return put.unstakeAsset(amount);
}
/**
* @description exchange accrued points for rewards in the contract
* @param amount amount of points to exchange for rewards
* @returns Boolean
*/
async exchangePoints(amount: number): Promise<boolean> {
await this.initialiseReachAccount();
const ctc = this.reachAccount.contract(backend, this.id);
const put = ctc.a.LiquidationVaultAPI;
return put.exchangePoints(amount);
}
/**
* @description withdraw accrued rewards from liquidations
* @param amount amount of liquidation rewards to withdraw
* @returns
*/
async withdrawRewards(amount: number): Promise<boolean> {
await this.initialiseReachAccount();
const ctc = this.reachAccount.contract(backend, this.id);
const put = ctc.a.LiquidationVaultAPI;
return put.withdrawRewards(amount);
}
/**
* @description update accrued points for an account
* @param address address of the user to accrue points for
* @returns boolean
*/
async cachePoints(address: string): Promise<boolean> {
await this.initialiseReachAccount();
const ctc = this.reachAccount.contract(backend, this.id);
const put = ctc.a.LiquidationVaultAPI;
return put.cachePoints(address);
}
/**
* @description Use staked xUSD in a liquidation of a vault
* @param stakerAddress address of the staked xUSD
* @param liquidateAddress address to liquidate
* @param amount amount of xUSD to use in liquidation
* @returns boolean
*/
async liquidateVault(stakerAddress: string, liquidateAddress: string, amount: string): Promise<boolean> {
await this.initialiseReachAccount();
const ctc = this.reachAccount.contract(backend, this.id);
const put = ctc.a.LiquidatorAPI;
return put.liquidateVault(stakerAddress, liquidateAddress, amount);
}
// -------------
/**
* @description Used for internal formatting of BigNumbers
* @param stakingState StakeLocalView type
* @returns StakeLocalView
*/
static parseUserInfo(stakingState: any): StakeLocalView {
return {
amountStaked: stakingState.amountStaked.toNumber(),
rewardsClaimed: stakingState.rewardsClaimed.toNumber(),
lastClaim: stakingState.lastClaim.toNumber(),
found: stakingState.found,
};
}
}