Skip to content

Commit a22d9ec

Browse files
authored
feat: add withdrawable stake related methods (#1044)
* feat: add withdrawable stake related methods * chore: typo * ci: change order of code linting
1 parent 05a681b commit a22d9ec

3 files changed

Lines changed: 86 additions & 15 deletions

File tree

.github/workflows/check.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ jobs:
3131
- name: Commit linting
3232
uses: wagoid/commitlint-github-action@v2
3333

34-
- name: Code linting
35-
run: npm run lint
36-
env:
37-
CI: true
38-
3934
- name: Dependency check
4035
run: npm run depcheck
4136

@@ -45,6 +40,11 @@ jobs:
4540
- name: Check build
4641
run: npm run build
4742

43+
- name: Code linting
44+
run: npm run lint
45+
env:
46+
CI: true
47+
4848
- name: Trigger API Docs update PR
4949
uses: peter-evans/repository-dispatch@v1
5050
if: github.ref == 'refs/heads/master'

src/bee.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,7 +1954,7 @@ export class Bee {
19541954
}
19551955

19561956
/**
1957-
* Cancel currently pending transaction
1957+
* Cancels a currently pending transaction
19581958
* @param transactionHash
19591959
* @param gasPrice
19601960
*/
@@ -1975,21 +1975,52 @@ export class Bee {
19751975
}
19761976

19771977
/**
1978-
* Gets the staked amount of BZZ (in PLUR unit) as number string.
1978+
* Gets the amount of staked BZZ
19791979
*
1980-
* @param options
1980+
* @param options HTTP request options, such as `headers` or `timeout`
19811981
*/
19821982
async getStake(options?: BeeRequestOptions): Promise<BZZ> {
19831983
return stake.getStake(this.getRequestOptionsForCall(options))
19841984
}
19851985

19861986
/**
1987-
* Deposits given amount of BZZ token (in PLUR unit).
1987+
* Gets the amount of withdrawable staked BZZ.
1988+
*
1989+
* @param options HTTP request options, such as `headers` or `timeout`
1990+
*/
1991+
async getWithdrawableStake(options?: BeeRequestOptions): Promise<BZZ> {
1992+
return stake.getWithdrawableStake(this.getRequestOptionsForCall(options))
1993+
}
1994+
1995+
/**
1996+
* Withdraws ALL surplus staked BZZ to the node wallet.
1997+
*
1998+
* Use the `getWithdrawableStake` method to check how much surplus stake is available.
1999+
*
2000+
* @param options HTTP request options, such as `headers` or `timeout`
2001+
*/
2002+
async withdrawSurplusStake(options?: BeeRequestOptions): Promise<TransactionId> {
2003+
return stake.withdrawSurplusStake(this.getRequestOptionsForCall(options))
2004+
}
2005+
2006+
/**
2007+
* Withdraws all staked BZZ to the node wallet.
2008+
*
2009+
* **Only available when the staking contract is paused and is in the process of being migrated to a new contract!**
2010+
*
2011+
* @param options HTTP request options, such as `headers` or `timeout`
2012+
*/
2013+
async migrateStake(options?: BeeRequestOptions): Promise<TransactionId> {
2014+
return stake.migrateStake(this.getRequestOptionsForCall(options))
2015+
}
2016+
2017+
/**
2018+
* Stakes the given amount of BZZ. Initial deposit must be at least 10 BZZ.
19882019
*
19892020
* Be aware that staked BZZ tokens can **not** be withdrawn.
19902021
*
1991-
* @param amount Amount of BZZ token (in PLUR unit) to be staked. Minimum is 100_000_000_000_000_000 PLUR (10 BZZ).
1992-
* @param options
2022+
* @param amount Amount of BZZ tokens to be staked. If not providing a `BZZ` instance, the amount is denoted in PLUR.
2023+
* @param options HTTP request options, such as `headers` or `timeout`
19932024
*/
19942025
async depositStake(
19952026
amount: BZZ | NumberString | string | bigint,
@@ -2007,9 +2038,9 @@ export class Bee {
20072038
}
20082039

20092040
/**
2010-
* Get current status of node in redistribution game
2041+
* Gets current status of node in redistribution game
20112042
*
2012-
* @param options
2043+
* @param options HTTP request options, such as `headers` or `timeout`
20132044
*/
20142045
async getRedistributionState(options?: BeeRequestOptions): Promise<RedistributionState> {
20152046
return stake.getRedistributionState(this.getRequestOptionsForCall(options))

src/modules/debug/stake.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const STAKE_ENDPOINT = 'stake'
1010
const REDISTRIBUTION_ENDPOINT = 'redistributionstate'
1111

1212
/**
13-
* Gets the staked amount
13+
* Gets the amount of staked BZZ
1414
*
1515
* @param requestOptions Options for making requests
1616
*/
@@ -26,12 +26,52 @@ export async function getStake(requestOptions: BeeRequestOptions): Promise<BZZ>
2626
return BZZ.fromPLUR(asNumberString(body.stakedAmount, { name: 'stakedAmount' }))
2727
}
2828

29+
/**
30+
* Gets the amount of withdrawable staked BZZ
31+
*
32+
* @param requestOptions Options for making requests
33+
*/
34+
export async function getWithdrawableStake(requestOptions: BeeRequestOptions): Promise<BZZ> {
35+
const response = await http<unknown>(requestOptions, {
36+
method: 'get',
37+
responseType: 'json',
38+
url: `${STAKE_ENDPOINT}/withdrawable`,
39+
})
40+
41+
const body = Types.asObject(response.data, { name: 'response.data' })
42+
43+
return BZZ.fromPLUR(asNumberString(body.withdrawableAmount, { name: 'withdrawableAmount' }))
44+
}
45+
46+
export async function withdrawSurplusStake(requestOptions: BeeRequestOptions): Promise<TransactionId> {
47+
const response = await http<unknown>(requestOptions, {
48+
method: 'delete',
49+
responseType: 'json',
50+
url: `${STAKE_ENDPOINT}/withdrawable`,
51+
})
52+
53+
const body = Types.asObject(response.data, { name: 'response.data' })
54+
55+
return new TransactionId(Types.asHexString(body.txHash, { name: 'txHash' }))
56+
}
57+
58+
export async function migrateStake(requestOptions: BeeRequestOptions): Promise<TransactionId> {
59+
const response = await http<unknown>(requestOptions, {
60+
method: 'delete',
61+
responseType: 'json',
62+
url: STAKE_ENDPOINT,
63+
})
64+
65+
const body = Types.asObject(response.data, { name: 'response.data' })
66+
67+
return new TransactionId(Types.asHexString(body.txHash, { name: 'txHash' }))
68+
}
69+
2970
/**
3071
* Stake given amount of tokens.
3172
*
3273
* @param requestOptions Options for making requests
3374
* @param amount
34-
* @param options
3575
*/
3676
export async function stake(
3777
requestOptions: BeeRequestOptions,

0 commit comments

Comments
 (0)