Skip to content

Commit

Permalink
feat(staking-pool): add partial withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
hejkerooo committed Nov 25, 2021
1 parent 201b58e commit 7801dc4
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 17 deletions.
Expand Up @@ -14,8 +14,11 @@ Abstraction over staking pool smart contract

- [checkReward](modules_staking_staking_pool_service.StakingPoolService.md#checkreward)
- [getContributionLimit](modules_staking_staking_pool_service.StakingPoolService.md#getcontributionlimit)
- [getEnd](modules_staking_staking_pool_service.StakingPoolService.md#getend)
- [getHardCap](modules_staking_staking_pool_service.StakingPoolService.md#gethardcap)
- [getStake](modules_staking_staking_pool_service.StakingPoolService.md#getstake)
- [getStart](modules_staking_staking_pool_service.StakingPoolService.md#getstart)
- [partialWithdraw](modules_staking_staking_pool_service.StakingPoolService.md#partialwithdraw)
- [putStake](modules_staking_staking_pool_service.StakingPoolService.md#putstake)
- [withdraw](modules_staking_staking_pool_service.StakingPoolService.md#withdraw)

Expand Down Expand Up @@ -56,6 +59,16 @@ ___

___

### getEnd

**getEnd**(): `Promise`<`BigNumber`\>

#### Returns

`Promise`<`BigNumber`\>

___

### getHardCap

**getHardCap**(): `Promise`<`BigNumber`\>
Expand All @@ -78,6 +91,34 @@ Stake

___

### getStart

**getStart**(): `Promise`<`BigNumber`\>

#### Returns

`Promise`<`BigNumber`\>

___

### partialWithdraw

**partialWithdraw**(`value`): `Promise`<`ContractReceipt`\>

**`description`**

#### Parameters

| Name | Type |
| :------ | :------ |
| `value` | `number` \| `BigNumber` |

#### Returns

`Promise`<`ContractReceipt`\>

___

### putStake

**putStake**(`stake`): `Promise`<`void`\>
Expand Down
51 changes: 40 additions & 11 deletions e2e/staking-pool.e2e.ts
Expand Up @@ -61,6 +61,9 @@ const ratioInt = utils.parseUnits(ratio.toString(), 18);

const rewards = oneEWT.mul(11);

let poolStart;
let poolEnd;

export const setupStakingPoolFactory = async () => {
const { chainId } = await deployer.provider.getNetwork();
const { claimManagerAddress } = chainConfigs()[chainId];
Expand All @@ -70,17 +73,14 @@ export const setupStakingPoolFactory = async () => {
const start = timestamp + 10;
const end = start + duration;

const stakingPoolFactory = await (
await new StakingPool__factory(deployer).deploy(
claimManagerAddress,
start,
end,
ratioInt,
hardCap,
contributionLimit,
{ value: rewards },
)
).deployed();
const stakingPoolFactory = await (await new StakingPool__factory(deployer).deploy()).deployed();

poolStart = start;
poolEnd = end;

await stakingPoolFactory.init(claimManagerAddress, start, end, ratioInt, hardCap, contributionLimit, {
value: rewards,
});

setChainConfig(chainId, { stakingPoolFactoryAddress: stakingPoolFactory.address });
};
Expand Down Expand Up @@ -203,4 +203,33 @@ describe("StakingPool tests", () => {

expect(poolContributionLimit.eq(contributionLimit)).toEqual(true);
});

it("should be able to unstake", async () => {
const pool = await stakingPoolService.getPool();

await putStake(pool, 2);

await pool.partialWithdraw(1);
await provider.send("evm_mine", []);

const currentStake = await pool.getStake();
expect(currentStake.amount.toNumber()).toBe(1);
expect(currentStake.status).toBe(StakeStatus.STAKING);
});

it("should return start of the pool", async () => {
const pool = await stakingPoolService.getPool();

const start = await pool.getStart();

expect(start.toNumber()).toBe(poolStart);
});

it("should return end of the pool", async () => {
const pool = await stakingPoolService.getPool();

const end = await pool.getEnd();

expect(end.toNumber()).toBe(poolEnd);
});
});
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -65,7 +65,7 @@
"@babel/runtime": "^7.12.5",
"@energyweb/ekc": "^0.6.4",
"@energyweb/iam-contracts": "^3.5.0",
"@energyweb/staking-pool": "^1.0.0-rc.2",
"@energyweb/staking-pool": "^1.0.0-rc.5",
"@ensdomains/ens": "^0.4.5",
"@ew-did-registry/claims": "^0.6.3-alpha.262.0",
"@ew-did-registry/did": "^0.6.3-alpha.262.0",
Expand Down
22 changes: 21 additions & 1 deletion src/modules/staking/staking-pool.service.ts
@@ -1,4 +1,4 @@
import { BigNumber, providers, utils } from "ethers";
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";
Expand Down Expand Up @@ -61,6 +61,14 @@ export class StakingPoolService {
).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();
}
Expand All @@ -69,6 +77,18 @@ export class StakingPoolService {
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
Expand Down

0 comments on commit 7801dc4

Please sign in to comment.