-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add basic stake function * docs: update docs
- Loading branch information
Showing
10 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { getClient, address } from "./utils" | ||
|
||
describe("stake management", () => { | ||
beforeEach(() => { | ||
jest.setTimeout(50000) | ||
}) | ||
|
||
it("bsc delegate", async () => { | ||
const client = await getClient(true) | ||
const validatorAddress = "bva10npy5809y303f227g4leqw7vs3s6ep5ul26sq2" | ||
|
||
try { | ||
const res = await client.stake.bscDelegate({ | ||
delegateAddress: address, | ||
validatorAddress, | ||
amount: 10, | ||
}) | ||
expect(res.status).toBe(200) | ||
} catch (err) { | ||
if (err.message.includes("insufficient fund")) { | ||
expect(1).toBeTruthy() | ||
} | ||
throw err | ||
} | ||
}) | ||
|
||
it("bsc undelegate", async () => { | ||
const client = await getClient(true) | ||
const validatorAddress = "bva10npy5809y303f227g4leqw7vs3s6ep5ul26sq2" | ||
|
||
try { | ||
const res = await client.stake.bscUndelegate({ | ||
delegateAddress: address, | ||
validatorAddress, | ||
amount: 10, | ||
}) | ||
expect(res.status).toBe(200) | ||
} catch (err) { | ||
if (err.message.includes("insufficient fund")) { | ||
expect(1).toBeTruthy() | ||
} | ||
throw err | ||
} | ||
}) | ||
|
||
it("bsc redelegate", async () => { | ||
const client = await getClient(true) | ||
const validatorSrcAddress = "bva10npy5809y303f227g4leqw7vs3s6ep5ul26sq2" | ||
const validatorDstAddress = "bva1pcd6muhehuz6fy05wfhq9sd5fww6ggdap3adxg" | ||
try { | ||
const res = await client.stake.bscReDelegate({ | ||
delegateAddress: address, | ||
validatorSrcAddress, | ||
validatorDstAddress, | ||
amount: 10, | ||
}) | ||
expect(res.status).toBe(200) | ||
} catch (err) { | ||
if (err.message.includes("insufficient fund")) { | ||
expect(1).toBeTruthy() | ||
} | ||
throw err | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# Class: Stake | ||
|
||
Stake | ||
|
||
## Hierarchy | ||
|
||
* **Stake** | ||
|
||
## Index | ||
|
||
### Constructors | ||
|
||
* [constructor](stake.md#constructor) | ||
|
||
## Constructors | ||
|
||
### constructor | ||
|
||
\+ **new Stake**(`bncClient`: [BncClient](bncclient.md)): *[Stake](stake.md)* | ||
|
||
**Parameters:** | ||
|
||
Name | Type | Description | | ||
------ | ------ | ------ | | ||
`bncClient` | [BncClient](bncclient.md) | | | ||
|
||
**Returns:** *[Stake](stake.md)* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import Big from "big.js" | ||
|
||
import { BncClient } from "../" | ||
import * as crypto from "../../crypto" | ||
import { | ||
BscDelegateMsg, | ||
BaseMsg, | ||
BscUndelegateMsg, | ||
BscReDelegateMsg, | ||
} from "../../types" | ||
|
||
/** | ||
* Stake | ||
*/ | ||
export class Stake { | ||
private _bncClient!: BncClient | ||
|
||
/** | ||
* @param {BncClient} bncClient | ||
*/ | ||
constructor(bncClient: BncClient) { | ||
this._bncClient = bncClient | ||
} | ||
|
||
public async bscDelegate({ | ||
delegateAddress, | ||
validatorAddress, | ||
amount, | ||
symbol = "BNB", | ||
sideChainId = "chapel", //default value is ganges(testnet) | ||
}: { | ||
delegateAddress: string | ||
validatorAddress: string | ||
amount: number | ||
symbol?: string | ||
sideChainId?: string | ||
}) { | ||
if (!amount) { | ||
throw new Error("amount should not be empty") | ||
} | ||
|
||
if (!delegateAddress) { | ||
throw new Error("delegate address should not be null") | ||
} | ||
|
||
if (!crypto.checkAddress(validatorAddress, "bva")) { | ||
throw new Error("validator address is not valid") | ||
} | ||
|
||
amount = Number(new Big(amount).mul(Math.pow(10, 8)).toString()) | ||
|
||
const bscDelegateMsg = new BscDelegateMsg({ | ||
delegator_addr: delegateAddress, | ||
validator_addr: validatorAddress, | ||
delegation: { denom: symbol, amount }, | ||
side_chain_id: sideChainId, | ||
}) | ||
|
||
return await this.broadcast(bscDelegateMsg, delegateAddress) | ||
} | ||
|
||
public async bscUndelegate({ | ||
delegateAddress, | ||
validatorAddress, | ||
amount, | ||
symbol = "BNB", | ||
sideChainId = "chapel", //default value is ganges(testnet) | ||
}: { | ||
delegateAddress: string | ||
validatorAddress: string | ||
amount: number | ||
symbol?: string | ||
sideChainId?: string | ||
}) { | ||
if (!amount) { | ||
throw new Error("amount should not be empty") | ||
} | ||
|
||
if (!delegateAddress) { | ||
throw new Error("delegate address should not be null") | ||
} | ||
|
||
if (!crypto.checkAddress(validatorAddress, "bva")) { | ||
throw new Error("validator address is not valid") | ||
} | ||
|
||
amount = Number(new Big(amount).mul(Math.pow(10, 8)).toString()) | ||
|
||
const unDelegateMsg = new BscUndelegateMsg({ | ||
delegator_addr: delegateAddress, | ||
validator_addr: validatorAddress, | ||
amount: { denom: symbol, amount }, | ||
side_chain_id: sideChainId, | ||
}) | ||
|
||
return await this.broadcast(unDelegateMsg, delegateAddress) | ||
} | ||
|
||
public async bscReDelegate({ | ||
delegateAddress, | ||
validatorSrcAddress, | ||
validatorDstAddress, | ||
amount, | ||
symbol = "BNB", | ||
sideChainId = "chapel", //default value is ganges(testnet) | ||
}: { | ||
delegateAddress: string | ||
validatorSrcAddress: string | ||
validatorDstAddress: string | ||
amount: number | ||
symbol?: string | ||
sideChainId?: string | ||
}) { | ||
if (!amount) { | ||
throw new Error("amount should not be empty") | ||
} | ||
|
||
if (!delegateAddress) { | ||
throw new Error("delegate address should not be null") | ||
} | ||
|
||
if (!crypto.checkAddress(validatorSrcAddress, "bva")) { | ||
throw new Error("validator source address is not valid") | ||
} | ||
|
||
if (!crypto.checkAddress(validatorDstAddress, "bva")) { | ||
throw new Error("validator dest address is not valid") | ||
} | ||
|
||
amount = Number(new Big(amount).mul(Math.pow(10, 8)).toString()) | ||
|
||
const bscReDelegateMsg = new BscReDelegateMsg({ | ||
delegator_addr: delegateAddress, | ||
validator_src_addr: validatorSrcAddress, | ||
validator_dst_addr: validatorDstAddress, | ||
amount: { denom: symbol, amount }, | ||
side_chain_id: sideChainId, | ||
}) | ||
|
||
return await this.broadcast(bscReDelegateMsg, delegateAddress) | ||
} | ||
|
||
private async broadcast( | ||
msg: BaseMsg, | ||
fromAddress: string, | ||
sequence?: number | ||
) { | ||
const signedTx = await this._bncClient._prepareTransaction( | ||
msg.getMsg(), | ||
msg.getSignMsg(), | ||
fromAddress, | ||
sequence | ||
) | ||
return this._bncClient._broadcastDelegate(signedTx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { BaseMsg, Msg, SignMsg, Coin } from ".." | ||
import * as crypto from "../../../crypto" | ||
import { AminoPrefix } from "../../tx" | ||
|
||
export interface SignedBscDelegate extends SignMsg { | ||
delegator_addr: string | ||
validator_addr: string | ||
delegation: Coin | ||
side_chain_id: string | ||
} | ||
|
||
export interface BscDelegateData extends Msg { | ||
delegator_addr: Buffer | ||
validator_addr: Buffer | ||
delegation: Coin | ||
side_chain_id: string | ||
aminoPrefix: AminoPrefix | ||
} | ||
|
||
export class BscDelegateMsg extends BaseMsg { | ||
private delegator_addr: string | ||
private validator_addr: string | ||
private delegation: Coin | ||
private side_chain_id: string | ||
|
||
constructor({ | ||
delegator_addr, | ||
validator_addr, | ||
delegation, | ||
side_chain_id, | ||
}: { | ||
delegator_addr: string | ||
validator_addr: string | ||
delegation: Coin | ||
side_chain_id: string | ||
}) { | ||
super() | ||
this.delegator_addr = delegator_addr | ||
this.validator_addr = validator_addr | ||
this.delegation = delegation | ||
this.side_chain_id = side_chain_id | ||
} | ||
|
||
getSignMsg() { | ||
const { denom, amount } = this.delegation | ||
const signMsg: SignedBscDelegate = { | ||
delegator_addr: this.delegator_addr, | ||
validator_addr: this.validator_addr, | ||
delegation: { denom, amount: String(amount) }, | ||
side_chain_id: this.side_chain_id, | ||
} | ||
|
||
return { | ||
type: "cosmos-sdk/MsgSideChainDelegate", | ||
value: signMsg, | ||
} | ||
} | ||
|
||
getMsg() { | ||
const data: BscDelegateData = { | ||
delegator_addr: crypto.decodeAddress(this.delegator_addr), | ||
validator_addr: crypto.decodeAddress(this.validator_addr), | ||
delegation: this.delegation, | ||
side_chain_id: this.side_chain_id, | ||
aminoPrefix: AminoPrefix.MsgSideChainDelegate, | ||
} | ||
|
||
return data | ||
} | ||
|
||
static defaultMsg() { | ||
return { | ||
delegator_addr: Buffer.from(""), | ||
validator_addr: Buffer.from(""), | ||
delegation: [{ denom: "", amount: 0 }], | ||
side_chain_id: "", | ||
aminoPrefix: AminoPrefix.MsgSideChainDelegate, | ||
} | ||
} | ||
} |
Oops, something went wrong.