Skip to content

Commit 8a2b476

Browse files
committed
Add getters and validation
1 parent ac9b5ab commit 8a2b476

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

packages/staking-cli/src/cmd/ton.ts

-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ async function runTx (
152152

153153
unsignedTx = (
154154
await tonStaker.buildStakeTx({
155-
delegatorAddress: config.delegatorAddress,
156155
validatorAddressPair,
157156
amount: arg[0] // amount
158157
})
@@ -180,7 +179,6 @@ async function runTx (
180179

181180
unsignedTx = (
182181
await tonStaker.buildUnstakeTx({
183-
delegatorAddress: config.delegatorAddress,
184182
validatorAddress,
185183
amount: arg[0] // amount
186184
})

packages/ton/src/TonPoolStaker.ts

+47-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Address, beginCell, toNano } from '@ton/ton'
1+
import { Address, beginCell, fromNano, toNano } from '@ton/ton'
22
import { defaultValidUntil, getDefaultGas, getRandomQueryId, TonBaseStaker } from './TonBaseStaker'
33
import { UnsignedTx } from './types'
44

@@ -8,22 +8,32 @@ export class TonPoolStaker extends TonBaseStaker {
88
* For more information see: https://github.com/ton-blockchain/nominator-pool
99
*
1010
* @param params - Parameters for building the transaction
11-
* @param params.delegatorAddress - The delegator address to stake from
1211
* @param params.validatorAddress - The validator address to stake to
1312
* @param params.amount - The amount to stake, specified in `TON`
1413
* @param params.validUntil - (Optional) The Unix timestamp when the transaction expires
1514
*
1615
* @returns Returns a promise that resolves to a TON nominator pool staking transaction.
1716
*/
1817
async buildStakeTx (params: {
19-
delegatorAddress: string
2018
validatorAddressPair: [string, string]
2119
amount: string
2220
validUntil?: number
2321
}): Promise<{ tx: UnsignedTx }> {
24-
const { delegatorAddress, validatorAddressPair, amount, validUntil } = params
22+
const { validatorAddressPair, amount, validUntil } = params
2523
const validatorAddress = await this.getPoolAddressForStake({ validatorAddressPair })
2624

25+
// ensure the address is for the right network
26+
this.checkIfAddressTestnetFlagMatches(validatorAddress)
27+
28+
// ensure the validator address is bounceable.
29+
// NOTE: TEP-002 specifies that the address bounceable flag should match both the internal message and the address.
30+
// This has no effect as we force the bounce flag anyway. However it is a good practice to be consistent
31+
if (!Address.parseFriendly(validatorAddress).isBounceable) {
32+
throw new Error(
33+
'validator address is not bounceable! It is required for nominator pool contract operations to use bounceable addresses'
34+
)
35+
}
36+
2737
const payload = beginCell()
2838
.storeUint(2077040623, 32)
2939
.storeUint(getRandomQueryId(), 64) // Query ID
@@ -56,14 +66,25 @@ export class TonPoolStaker extends TonBaseStaker {
5666
* @returns Returns a promise that resolves to a TON nominator pool staking transaction.
5767
*/
5868
async buildUnstakeTx (params: {
59-
delegatorAddress: string
6069
validatorAddress: string
6170
amount: string
6271
validUntil?: number
6372
}): Promise<{ tx: UnsignedTx }> {
64-
const { delegatorAddress, validatorAddress, amount, validUntil } = params
73+
const { validatorAddress, amount, validUntil } = params
74+
75+
// ensure the address is for the right network
76+
this.checkIfAddressTestnetFlagMatches(validatorAddress)
77+
78+
// ensure the validator address is bounceable.
79+
// NOTE: TEP-002 specifies that the address bounceable flag should match both the internal message and the address.
80+
// This has no effect as we force the bounce flag anyway. However it is a good practice to be consistent
81+
if (!Address.parseFriendly(validatorAddress).isBounceable) {
82+
throw new Error(
83+
'validator address is not bounceable! It is required for nominator pool contract operations to use bounceable addresses'
84+
)
85+
}
6586

66-
const data = await this.getOnePoolParams({ validatorAddress })
87+
const data = await this.getPoolParamsUnformatted({ validatorAddress })
6788

6889
const payload = beginCell()
6990
.storeUint(3665837821, 32)
@@ -85,7 +106,7 @@ export class TonPoolStaker extends TonBaseStaker {
85106
return { tx }
86107
}
87108

88-
private async getOnePoolStake (params: { delegatorAddress: string; validatorAddress: string }) {
109+
async getStake (params: { delegatorAddress: string; validatorAddress: string }) {
89110
const { delegatorAddress, validatorAddress } = params
90111
const client = this.getClient()
91112

@@ -94,19 +115,31 @@ export class TonPoolStaker extends TonBaseStaker {
94115
])
95116

96117
return {
97-
balance: response.stack.readBigNumber(),
98-
pendingDeposit: response.stack.readBigNumber(),
99-
pendingWithdraw: response.stack.readBigNumber(),
100-
withdraw: response.stack.readBigNumber()
118+
balance: fromNano(response.stack.readBigNumber()),
119+
pendingDeposit: fromNano(response.stack.readBigNumber()),
120+
pendingWithdraw: fromNano(response.stack.readBigNumber()),
121+
withdraw: fromNano(response.stack.readBigNumber())
122+
}
123+
}
124+
125+
async getPoolParams (params: { validatorAddress: string }) {
126+
const result = await this.getPoolParamsUnformatted(params)
127+
128+
return {
129+
minStake: fromNano(result.minStake),
130+
depositFee: fromNano(result.depositFee),
131+
withdrawFee: fromNano(result.withdrawFee),
132+
poolFee: fromNano(result.poolFee),
133+
receiptPrice: fromNano(result.receiptPrice)
101134
}
102135
}
103136

104-
private async getOnePoolParams (params: { validatorAddress: string }) {
137+
private async getPoolParamsUnformatted (params: { validatorAddress: string }) {
105138
const { validatorAddress } = params
106139
const client = this.getClient()
107140
const response = await client.runMethod(Address.parse(validatorAddress), 'get_params', [])
108141

109-
const result = {
142+
return {
110143
enabled: response.stack.readBoolean(),
111144
updatesEnables: response.stack.readBoolean(),
112145
minStake: response.stack.readBigNumber(),
@@ -115,14 +148,6 @@ export class TonPoolStaker extends TonBaseStaker {
115148
poolFee: response.stack.readBigNumber(),
116149
receiptPrice: response.stack.readBigNumber()
117150
}
118-
119-
return {
120-
minStake: result.minStake,
121-
depositFee: result.depositFee,
122-
withdrawFee: result.withdrawFee,
123-
poolFee: result.poolFee,
124-
receiptPrice: result.receiptPrice
125-
}
126151
}
127152

128153
/** @ignore */

0 commit comments

Comments
 (0)