-
Notifications
You must be signed in to change notification settings - Fork 7
/
staking-pool.service.ts
162 lines (136 loc) · 5.13 KB
/
staking-pool.service.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
162
import { BigNumber, ContractReceipt, providers, utils } from "ethers";
import { StakingPool as StakingPoolContract } from "../../../ethers-staking";
import { StakingPool__factory } from "../../../ethers-staking/factories/StakingPool__factory";
import { ERROR_MESSAGES } from "../../errors/ErrorMessages";
import { SignerService } from "../signer/signer.service";
import { chainConfigs } from "../../config/chain.config";
import { DomainsService } from "../domains/domains.service";
import { Service, Stake, StakeStatus } from "./staking.service";
const { namehash, parseUnits } = utils;
/**
* Intended for staking pool
*/
export class StakingFactoryService {
constructor(private _signerService: SignerService, private _domainsService: DomainsService) {}
static async create(signerService: SignerService, domainsService: DomainsService) {
const service = new StakingFactoryService(signerService, domainsService);
return service;
}
/**
* @description Returns all services for which pools are launched
*/
async allServices(): Promise<Service[]> {
return [
{
org: await this._domainsService.readName(namehash("energyweb.iam.ewc")),
pool: chainConfigs()[this._signerService.chainId].stakingPoolFactoryAddress,
provider: "empty",
},
];
}
/**
* @description Returns pool launched for energyweb org
*/
async getPool(): Promise<StakingPoolService> {
return new StakingPoolService(
this._signerService,
chainConfigs()[this._signerService.chainId].stakingPoolFactoryAddress,
);
}
}
/**
* Abstraction over staking pool smart contract
*/
export class StakingPoolService {
private overrides = {
gasPrice: parseUnits("0.01", "gwei"),
gasLimit: BigNumber.from(490000),
};
private pool: StakingPoolContract;
constructor(private signerService: SignerService, address: string) {
this.pool = new StakingPool__factory(
StakingPool__factory.createInterface(),
StakingPool__factory.bytecode,
).attach(address);
}
async getStart(): Promise<BigNumber> {
return this.pool.connect(this.signerService.signer).start();
}
async getEnd(): Promise<BigNumber> {
return this.pool.connect(this.signerService.signer).end();
}
async getHardCap(): Promise<BigNumber> {
return this.pool.connect(this.signerService.signer).hardCap();
}
async getContributionLimit(): Promise<BigNumber> {
return this.pool.connect(this.signerService.signer).contributionLimit();
}
/**
* @description
* @param value
*/
async partialWithdraw(value: BigNumber | number): Promise<ContractReceipt> {
value = BigNumber.from(value);
const transaction = await this.pool.connect(this.signerService.signer).unstake(value);
return transaction.wait();
}
/**
* @description Locks stake and starts accumulating reward
* @emits StakingPool.StakePut
*/
async putStake(stake: BigNumber | number): Promise<void> {
stake = BigNumber.from(stake);
const tx: providers.TransactionRequest = {
to: this.pool.address,
from: this.signerService.address,
data: this.pool.interface.encodeFunctionData("stake"),
value: stake,
};
const balance = await this.signerService.balance();
const gasPrice = await this.signerService.signer.getGasPrice();
const gas = await this.signerService.provider.estimateGas(tx);
// multiplier 2 chosen arbitrarily because it is not known how reasonably to choose it
const fee = gasPrice.mul(gas).mul(2);
const maxStake = balance.sub(fee);
if (maxStake.lte(0)) {
throw new Error(ERROR_MESSAGES.INSUFFICIENT_BALANCE);
}
tx.value = stake.lt(maxStake) ? stake : maxStake;
await this.signerService.send(tx);
}
/**
* Accumulated reward
*/
async checkReward(): Promise<BigNumber> {
const [staked, compounded] = await this.pool.connect(this.signerService.signer).total();
return compounded.sub(staked);
}
/**
* @returns Stake
*/
async getStake(): Promise<Stake> {
const [staked] = await this.pool.connect(this.signerService.signer).total();
const [start, end] = await Promise.all([
this.pool.connect(this.signerService.signer).start(),
this.pool.connect(this.signerService.signer).end(),
]);
const stakeStatus = staked.gt(0) ? StakeStatus.STAKING : StakeStatus.NONSTAKING;
return {
amount: staked,
depositStart: start,
depositEnd: end,
status: stakeStatus,
};
}
/**
* @description pays back stake with accumulated reward.
*/
async withdraw(): Promise<void> {
const tx: providers.TransactionRequest = {
to: this.pool.address,
data: this.pool.interface.encodeFunctionData("unstakeAll"),
...this.overrides,
};
await this.signerService.send(tx);
}
}