Skip to content

Commit

Permalink
feat(staking): check balance is sufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 19, 2021
1 parent 768dc5d commit 0f049ee
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
5 changes: 3 additions & 2 deletions docs/api/classes/stakingpool.md
Expand Up @@ -29,13 +29,14 @@ Abstraction over staking pool smart contract

### constructor

\+ **new StakingPool**(`pool`: StakingPoolContract): [StakingPool](stakingpool.md)
\+ **new StakingPool**(`patron`: Required\<Signer>, `address`: string): [StakingPool](stakingpool.md)

#### Parameters:

Name | Type |
------ | ------ |
`pool` | StakingPoolContract |
`patron` | Required\<Signer> |
`address` | string |

**Returns:** [StakingPool](stakingpool.md)

Expand Down
7 changes: 7 additions & 0 deletions docs/api/enums/error_messages.md
Expand Up @@ -16,6 +16,7 @@
* [ENS\_REGISTRY\_NOT\_INITIALIZED](error_messages.md#ens_registry_not_initialized)
* [ENS\_RESOLVER\_NOT\_INITIALIZED](error_messages.md#ens_resolver_not_initialized)
* [ENS\_TYPE\_NOT\_SUPPORTED](error_messages.md#ens_type_not_supported)
* [INSUFFICIENT\_BALANCE](error_messages.md#insufficient_balance)
* [JWT\_NOT\_INITIALIZED](error_messages.md#jwt_not_initialized)
* [METAMASK\_EXTENSION\_NOT\_AVAILABLE](error_messages.md#metamask_extension_not_available)
* [NATS\_NOT\_CONNECTED](error_messages.md#nats_not_connected)
Expand Down Expand Up @@ -100,6 +101,12 @@ ___

___

### INSUFFICIENT\_BALANCE

**INSUFFICIENT\_BALANCE**: = "Signer has insufficient balance"

___

### JWT\_NOT\_INITIALIZED

**JWT\_NOT\_INITIALIZED**: = "JWT was not initialized"
Expand Down
3 changes: 2 additions & 1 deletion src/errors/ErrorMessages.ts
Expand Up @@ -29,5 +29,6 @@ export enum ERROR_MESSAGES {
ONCHAIN_ROLE_VERSION_NOT_SPECIFIED = "On-chain role version not specified",
CACHE_SERVER_NOT_REGISTERED = "Cache server for this chain is not registered",
WITHDRAWAL_WAS_NOT_REQUESTED = "Stake withdrawal was not requested",
STAKE_WAS_NOT_PUT = "Stake was not put"
STAKE_WAS_NOT_PUT = "Stake was not put",
INSUFFICIENT_BALANCE = "Signer has insufficient balance"
}
26 changes: 20 additions & 6 deletions src/staking/index.ts
Expand Up @@ -103,20 +103,22 @@ export class StakingPoolService {
* @param org ENS name of organization
*/
async getPool(org: string): Promise<StakingPool | null> {
const service = await this._stakingPoolFactory.services(namehash(org));
if (service.pool === emptyAddress) {
const { pool } = await this._stakingPoolFactory.services(namehash(org));
if (pool === emptyAddress) {
return null;
}
const pool = new StakingPool__factory(this._signer).attach(service.pool);
return new StakingPool(pool);
return new StakingPool(this._signer, pool);
}
}

/**
* Abstraction over staking pool smart contract
*/
export class StakingPool {
constructor(private pool: StakingPoolContract) { }
private pool: StakingPoolContract;
constructor(private patron: Required<Signer>, address: string) {
this.pool = new StakingPool__factory(patron).attach(address);
}

/**
* @description Locks stake and starts accumulating reward
Expand All @@ -129,6 +131,9 @@ export class StakingPool {
if (typeof stake === "number") {
stake = new BigNumber(stake);
}
if ((await this.getBalance()).lt(stake)) {
throw new Error(ERROR_MESSAGES.INSUFFICIENT_BALANCE);
}
await (await this.pool.putStake({
value: stake
})).wait();
Expand Down Expand Up @@ -206,7 +211,10 @@ export class StakingPool {
* @param signer Signer connected to provider
*/
connect(signer: Signer): StakingPool {
return new StakingPool(this.pool.connect(signer));
if (!signer.provider) {
throw new Error("StakingPoolService.init: Signer is not connected to provider");
}
return new StakingPool(signer as Required<Signer>, this.pool.address);
}

private async now(): Promise<utils.BigNumber> {
Expand All @@ -215,4 +223,10 @@ export class StakingPool {
(await this.pool.provider.getBlock(lastBlock)).timestamp
);
}

private async getBalance(): Promise<utils.BigNumber> {
return await this.patron.provider.getBalance(
await this.patron.getAddress()
);
}
}
15 changes: 12 additions & 3 deletions test/staking.ts
Expand Up @@ -3,7 +3,7 @@ import { StakingPoolFactory } from "@energyweb/iam-contracts/dist/ethers-v4/Stak
import { StakingPool as StakingPoolContract } from "@energyweb/iam-contracts/dist/ethers-v4/StakingPool";
import { EventFilter, Contract, Wallet, utils, providers } from "ethers";
import { Methods } from "@ew-did-registry/did";
import { IAM, RegistrationTypes, setChainConfig, StakingPool, StakingPoolService } from "../src/iam-client-lib";
import { ERROR_MESSAGES, IAM, RegistrationTypes, setChainConfig, StakingPool, StakingPoolService } from "../src/iam-client-lib";
import { claimManager, ensRegistry, replenish, provider, deployer } from "./setup_contracts";
import { createIam, root, rootOwner } from "./iam.test";
import { mockJsonCodec, mockNats } from "./testUtils/mocks";
Expand Down Expand Up @@ -306,21 +306,30 @@ export const stakingTests = (): void => {
)).rejects.toThrow("StakingPool: patron is not registered with patron role");
});

it("stake amount must be provided", async () => {
it("should reject when stake amount isn't provided", async () => {
return expect(
pool.putStake(parseEther("0"))
)
.rejects.toThrow("StakingPool: stake amount is not provided");
});

it("should not be able to stake with insufficient balance", async () => {
const balance = await patron.getBalance();

return expect(
pool.putStake(balance.add(1))
)
.rejects.toThrow(ERROR_MESSAGES.INSUFFICIENT_BALANCE);
});

it("stake should not be replenished", async () => {
await pool.putStake(parseEther("0.1"));

return expect(pool.putStake(parseEther("0.1")))
.rejects.toThrow("StakingPool: Replenishment of the stake is not allowed");
});

it("staker should be able to request withdraw", async () => {
it("staker should be able to request withdraw", async () => {
await pool.putStake(parseEther("0.1"));
const requestDelay = await pool.requestWithdrawDelay();
await new Promise((resolve) => setTimeout(resolve, 1000 * requestDelay.toNumber()));
Expand Down

0 comments on commit 0f049ee

Please sign in to comment.