Skip to content

Commit

Permalink
feat: stake whole balance
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Sep 2, 2021
1 parent 16d7505 commit 07ad380
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
12 changes: 5 additions & 7 deletions docs/api/classes/GnosisIam.GnosisIam-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ ___

**closeConnection**(): `Promise`<`void`\>

Close connection to wallet

**`description`** closes the connection between dApp and the wallet
**`description`** Closes the connection between application and the signer's wallet

#### Returns

Expand All @@ -310,7 +308,7 @@ ___

**connectToCacheServer**(): `Promise`<`void`\>

**`description`** established connection to cache server and logins in signing authentication token
**`description`** Establishes connection to the cache serverand sets public key and identity token

#### Returns

Expand All @@ -326,7 +324,7 @@ ___

**connectToDIDRegistry**(): `Promise`<`void`\>

**`description`** creates users DID document if it is not yet exist
**`description`** Creates the signer's DID document if it does not exist

#### Returns

Expand Down Expand Up @@ -1292,7 +1290,7 @@ ___

**isSessionActive**(): `boolean`

Check if session is active
**`description`** Checks if the session is active

#### Returns

Expand Down Expand Up @@ -1407,7 +1405,7 @@ ___

**on**(`event`, `eventHandler`): `void`

Add event handler for certain events
**`description`** Defines event handlers for change of account, change of network, disconnection

**`requires`** to be called after the connection to wallet was initialized

Expand Down
12 changes: 5 additions & 7 deletions docs/api/classes/iam.IAM.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ ___

**closeConnection**(): `Promise`<`void`\>

Close connection to wallet

**`description`** closes the connection between dApp and the wallet
**`description`** Closes the connection between application and the signer's wallet

#### Returns

Expand All @@ -273,7 +271,7 @@ ___

**connectToCacheServer**(): `Promise`<`void`\>

**`description`** established connection to cache server and logins in signing authentication token
**`description`** Establishes connection to the cache serverand sets public key and identity token

#### Returns

Expand All @@ -289,7 +287,7 @@ ___

**connectToDIDRegistry**(): `Promise`<`void`\>

**`description`** creates users DID document if it is not yet exist
**`description`** Creates the signer's DID document if it does not exist

#### Returns

Expand Down Expand Up @@ -1109,7 +1107,7 @@ ___

**isSessionActive**(): `boolean`

Check if session is active
**`description`** Checks if the session is active

#### Returns

Expand Down Expand Up @@ -1208,7 +1206,7 @@ ___

**on**(`event`, `eventHandler`): `void`

Add event handler for certain events
**`description`** Defines event handlers for change of account, change of network, disconnection

**`requires`** to be called after the connection to wallet was initialized

Expand Down
12 changes: 5 additions & 7 deletions docs/api/classes/iam_iam_base.IAMBase.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ IAM Constructor

**closeConnection**(): `Promise`<`void`\>

Close connection to wallet

**`description`** closes the connection between dApp and the wallet
**`description`** Closes the connection between application and the signer's wallet

#### Returns

Expand All @@ -70,7 +68,7 @@ ___

**connectToCacheServer**(): `Promise`<`void`\>

**`description`** established connection to cache server and logins in signing authentication token
**`description`** Establishes connection to the cache serverand sets public key and identity token

#### Returns

Expand All @@ -82,7 +80,7 @@ ___

**connectToDIDRegistry**(): `Promise`<`void`\>

**`description`** creates users DID document if it is not yet exist
**`description`** Creates the signer's DID document if it does not exist

#### Returns

Expand All @@ -94,7 +92,7 @@ ___

**isSessionActive**(): `boolean`

Check if session is active
**`description`** Checks if the session is active

#### Returns

Expand All @@ -108,7 +106,7 @@ ___

**on**(`event`, `eventHandler`): `void`

Add event handler for certain events
**`description`** Defines event handlers for change of account, change of network, disconnection

**`requires`** to be called after the connection to wallet was initialized

Expand Down
2 changes: 1 addition & 1 deletion src/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1893,4 +1893,4 @@ export class IAM extends IAMBase {
}
throw new Error(ERROR_MESSAGES.CACHE_CLIENT_NOT_PROVIDED);
}
}
}
35 changes: 23 additions & 12 deletions src/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ export class StakingPoolService {
* Abstraction over staking pool smart contract
*/
export class StakingPool {
private overrides = {
private overrides: Record<TransactionSpeed, { gasPrice?: BigNumber; gasLimit?: BigNumber }> = {
[TransactionSpeed.BASE]: {},
[TransactionSpeed.FAST]: {
gasPrice: parseUnits("0.01", "gwei"),
gasLimit: 490000,
gasLimit: BigNumber.from(490000),
},
};
private pool: StakingPoolContract;
Expand All @@ -160,18 +160,29 @@ export class StakingPool {
stake: BigNumber | number,
transactionSpeed = TransactionSpeed.FAST,
): Promise<void> {
if (typeof stake === "number") {
stake = BigNumber.from(stake);
}
if ((await this.getBalance()).lt(stake)) {
stake = BigNumber.from(stake);
const tx = {
to: this.pool.address,
from: await this.patron.getAddress(),
data: this.pool.interface.encodeFunctionData("putStake"),
value: stake,
...this.overrides[transactionSpeed],
};

const balance = await this.getBalance();
if (balance.lt(stake)) {
throw new Error(ERROR_MESSAGES.INSUFFICIENT_BALANCE);
} else if (balance.eq(stake)) {
const gasPrice = this.overrides[transactionSpeed].gasPrice || (await this.patron.provider.getGasPrice());
const gas = this.overrides[transactionSpeed].gasLimit || (await this.patron.provider.estimateGas(tx));
// multiplier 2 chosen arbitrarily because it is not known how reasonably to choose it
const fee = gasPrice.mul(gas).mul(2);
if (balance.lte(fee)) {
throw new Error(ERROR_MESSAGES.INSUFFICIENT_BALANCE);
}
tx.value = balance.sub(fee);
}
await (
await this.pool.putStake({
value: stake,
...this.overrides[transactionSpeed],
})
).wait();
await (await this.patron.sendTransaction(tx)).wait();
}

/**
Expand Down

0 comments on commit 07ad380

Please sign in to comment.