1
- import { Address , beginCell , toNano } from '@ton/ton'
1
+ import { Address , beginCell , fromNano , toNano } from '@ton/ton'
2
2
import { defaultValidUntil , getDefaultGas , getRandomQueryId , TonBaseStaker } from './TonBaseStaker'
3
3
import { UnsignedTx } from './types'
4
4
@@ -8,22 +8,32 @@ export class TonPoolStaker extends TonBaseStaker {
8
8
* For more information see: https://github.com/ton-blockchain/nominator-pool
9
9
*
10
10
* @param params - Parameters for building the transaction
11
- * @param params.delegatorAddress - The delegator address to stake from
12
11
* @param params.validatorAddress - The validator address to stake to
13
12
* @param params.amount - The amount to stake, specified in `TON`
14
13
* @param params.validUntil - (Optional) The Unix timestamp when the transaction expires
15
14
*
16
15
* @returns Returns a promise that resolves to a TON nominator pool staking transaction.
17
16
*/
18
17
async buildStakeTx ( params : {
19
- delegatorAddress : string
20
18
validatorAddressPair : [ string , string ]
21
19
amount : string
22
20
validUntil ?: number
23
21
} ) : Promise < { tx : UnsignedTx } > {
24
- const { delegatorAddress , validatorAddressPair, amount, validUntil } = params
22
+ const { validatorAddressPair, amount, validUntil } = params
25
23
const validatorAddress = await this . getPoolAddressForStake ( { validatorAddressPair } )
26
24
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
+
27
37
const payload = beginCell ( )
28
38
. storeUint ( 2077040623 , 32 )
29
39
. storeUint ( getRandomQueryId ( ) , 64 ) // Query ID
@@ -56,14 +66,25 @@ export class TonPoolStaker extends TonBaseStaker {
56
66
* @returns Returns a promise that resolves to a TON nominator pool staking transaction.
57
67
*/
58
68
async buildUnstakeTx ( params : {
59
- delegatorAddress : string
60
69
validatorAddress : string
61
70
amount : string
62
71
validUntil ?: number
63
72
} ) : 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
+ }
65
86
66
- const data = await this . getOnePoolParams ( { validatorAddress } )
87
+ const data = await this . getPoolParamsUnformatted ( { validatorAddress } )
67
88
68
89
const payload = beginCell ( )
69
90
. storeUint ( 3665837821 , 32 )
@@ -85,7 +106,7 @@ export class TonPoolStaker extends TonBaseStaker {
85
106
return { tx }
86
107
}
87
108
88
- private async getOnePoolStake ( params : { delegatorAddress : string ; validatorAddress : string } ) {
109
+ async getStake ( params : { delegatorAddress : string ; validatorAddress : string } ) {
89
110
const { delegatorAddress, validatorAddress } = params
90
111
const client = this . getClient ( )
91
112
@@ -94,19 +115,31 @@ export class TonPoolStaker extends TonBaseStaker {
94
115
] )
95
116
96
117
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 )
101
134
}
102
135
}
103
136
104
- private async getOnePoolParams ( params : { validatorAddress : string } ) {
137
+ private async getPoolParamsUnformatted ( params : { validatorAddress : string } ) {
105
138
const { validatorAddress } = params
106
139
const client = this . getClient ( )
107
140
const response = await client . runMethod ( Address . parse ( validatorAddress ) , 'get_params' , [ ] )
108
141
109
- const result = {
142
+ return {
110
143
enabled : response . stack . readBoolean ( ) ,
111
144
updatesEnables : response . stack . readBoolean ( ) ,
112
145
minStake : response . stack . readBigNumber ( ) ,
@@ -115,14 +148,6 @@ export class TonPoolStaker extends TonBaseStaker {
115
148
poolFee : response . stack . readBigNumber ( ) ,
116
149
receiptPrice : response . stack . readBigNumber ( )
117
150
}
118
-
119
- return {
120
- minStake : result . minStake ,
121
- depositFee : result . depositFee ,
122
- withdrawFee : result . withdrawFee ,
123
- poolFee : result . poolFee ,
124
- receiptPrice : result . receiptPrice
125
- }
126
151
}
127
152
128
153
/** @ignore */
0 commit comments