-
Notifications
You must be signed in to change notification settings - Fork 1
/
SiennaRewards_v4.ts
161 lines (150 loc) · 4.46 KB
/
SiennaRewards_v4.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
152
153
154
155
156
157
158
159
160
161
import {
Address,
ContractLink,
Duration,
Message,
Uint128,
linkStruct,
now,
} from './Core'
import {
AuthClient,
AuthMethod
} from './Auth'
import {
Rewards,
RewardsInitParams
} from './SiennaRewards'
import {
Rewards_v3_Account,
Rewards_v3_1,
Rewards_v3_Total
} from './SiennaRewards_v3'
export class Rewards_v4_1 extends Rewards_v3_1 {
/** Create an init message for Sienna Rewards v4 */
static init({
authProvider,
rewardToken,
stakedToken,
bonding = 86400,
unbonding = 86400
}: RewardsInitParams): Message {
if (!authProvider) {
throw new Error('Pass authProvider')
}
return {
provider: linkStruct(authProvider),
config: {
reward_token: linkStruct(rewardToken),
lp_token: linkStruct(stakedToken),
bonding_period: bonding,
unbonding_period: unbonding,
},
};
}
get auth (): AuthClient {
return new AuthClient(this.agent, this.address, this.codeHash)
}
async setGovernanceLink<T>(link: ContractLink): Promise<T> {
return (await this.execute({ rewards: { configure: { governance: link } } })) as T;
}
async getConfig() {
const result: { rewards: { config: Rewards_v4_Config } } = await this.query({
rewards: 'config',
});
return result.rewards.config;
}
async getAccount(auth: AuthMethod<RewardsPermissions>, at: number = now()) {
const msg = { rewards: { user_info: { auth, at } } };
const result: { rewards: { user_info: Rewards_v4_Account } } = await this.query(msg);
return result.rewards.user_info;
}
async getUserInfo(key = '', address = this.agent?.address, at = now()): Promise<Rewards_v4_Account> {
// Cant change signature to throw error when address is not provided
const auth_method: AuthMethod<RewardsPermissions> = {
viewing_key: { address: address ?? '', key },
};
const msg = { rewards: { user_info: { at, auth_method } } };
const result: { rewards: { user_info: Rewards_v4_Account } } = await this.query(msg);
return result.rewards.user_info;
}
async getBalance(
auth_method: AuthMethod<RewardsPermissions>,
address = this.agent?.address,
at = now()
) {
const result: {
rewards: { balance: { amount: Uint128 } }
} = await this.query({
rewards: { balance: { address, auth_method } },
});
return result.rewards.balance;
}
async getBondingBalances(auth_method: AuthMethod<RewardsPermissions>, at: number = now()) {
const result: {
rewards: { all_balances: Rewards_v4_BondingBalances };
} = await this.query({
rewards: { all_balances: { at, auth_method } },
});
return result.rewards.all_balances;
}
unbond(amount: Uint128) {
return this.execute({
rewards: { unbond: { amount } },
});
}
}
/** Reward pool configuration */
export interface Rewards_v4_Config {
lp_token?: ContractLink;
reward_token?: ContractLink;
reward_vk?: string;
claim_throttle?: number;
timekeeper?: Address;
bonding_period?: number;
unbonding_period?: number;
governance?: ContractLink;
rewards_toggle: RewardsToggle;
}
export interface RewardsToggle {
bonded: boolean;
unbonding: boolean;
}
export interface Rewards_v4_Account extends Rewards_v3_Account {
/** How much total liquidity does this user provide. */
balance: Uint128;
/** The part of the user's stake that is counted for rewards. */
staked: Uint128;
/** How many units of time (seconds) remain until the user can claim?
* Replaces "bonding" in v3, bonding is now something else. */
claim_countdown: Duration;
}
export interface Rewards_v4_BondingBalances {
/** How much is deposited by the user */
balance: Uint128;
/** How much the user has staked which is valid for rewards */
staked: Uint128;
/** How much is currently bonding for the user */
bonding: Uint128;
/** How much is currently bonded for the user */
bonded: Uint128;
/** How much is the process of being unbonded, total */
unbonding: Uint128;
/** How much has been unbonded and is ready for withdrawing */
unbonded: Uint128;
/** All of the entries for bonding and unbonding */
history: Rewards_v4_HistoryEntry[];
}
export interface Rewards_v4_HistoryEntry {
// the type of bonding
bonding_type: 'bonding' | 'unbonding';
// When it started
timestamp: number;
// How many tokens
amount: Uint128;
}
enum RewardsPermissions {
UserInfo = 'user_info',
Balance = 'balance',
}
Rewards['v4.1'] = Rewards_v4_1