Skip to content

Commit ac9b5ab

Browse files
committed
Add support for 2 TON pools
1 parent 7751ea9 commit ac9b5ab

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

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

+42-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ function makeTxCommand (): Command {
3434
.argument('<amount>', 'amount of tokens to stake expressed in TON denom e.g 0.1')
3535
.action(getDelegatePoolTx)
3636

37+
const validateIndex = (value: string): '1' | '2' => {
38+
if (value !== '1' && value !== '2') {
39+
throw new Error('validator index must be 1 or 2')
40+
}
41+
return value
42+
}
43+
3744
tx.command('unstake-pool')
3845
.description('generate a unstake funds to TON pool contract transaction')
3946
.argument('<amount>', 'amount of tokens to unstake expressed in TON denom e.g 0.1')
47+
.requiredOption('-I, --validator-index <value>', 'validator index to unstake from (1 or 2)', validateIndex)
4048
.action(getUnstakePoolTx)
4149

4250
tx.command('delegate-nominator-pool')
@@ -95,7 +103,7 @@ async function init (
95103

96104
async function runTx (
97105
msgType: string,
98-
_options: any,
106+
options: { validatorIndex: '1' | '2' } | undefined,
99107
cmd: Command<[string]> | Command<[string, string]> | Command<[string, string, string]> | Command<[]>,
100108
arg: string[]
101109
): Promise<void> {
@@ -132,10 +140,20 @@ async function runTx (
132140
tonStaker = new TonPoolStaker({ ...networkConfig })
133141
await tonStaker.init()
134142

143+
if (!config.validatorAddress2) {
144+
cmd.error('second validator address is required for TON Pool', { exitCode: 1, code: `${msgType}.tx.abort` })
145+
}
146+
const validatorAddressPair: [string, string] = [config.validatorAddress, config.validatorAddress2]
147+
148+
const validatorToDelegate = await tonStaker.getPoolAddressForStake({ validatorAddressPair })
149+
const validatorIndex = validatorToDelegate === config.validatorAddress ? 1 : 2
150+
151+
console.log('Delegating to validator #' + validatorIndex + ': ' + validatorToDelegate)
152+
135153
unsignedTx = (
136154
await tonStaker.buildStakeTx({
137155
delegatorAddress: config.delegatorAddress,
138-
validatorAddress: config.validatorAddress,
156+
validatorAddressPair,
139157
amount: arg[0] // amount
140158
})
141159
).tx
@@ -145,10 +163,25 @@ async function runTx (
145163
tonStaker = new TonPoolStaker({ ...networkConfig })
146164
await tonStaker.init()
147165

166+
if (!config.validatorAddress2) {
167+
cmd.error('second validator address is required for TON Pool', { exitCode: 1, code: `${msgType}.tx.abort` })
168+
}
169+
170+
if (!options?.validatorIndex) {
171+
cmd.error('validator index is required for TON Pool', { exitCode: 1, code: `${msgType}.tx.abort` })
172+
}
173+
const validatorAddressPair: [string, string] = [config.validatorAddress, config.validatorAddress2]
174+
175+
const validatorIndex = parseInt(options.validatorIndex)
176+
177+
const validatorAddress = validatorAddressPair[validatorIndex - 1]
178+
179+
console.log('Unstaking from validator #' + validatorIndex + ': ' + validatorAddress)
180+
148181
unsignedTx = (
149182
await tonStaker.buildUnstakeTx({
150183
delegatorAddress: config.delegatorAddress,
151-
validatorAddress: config.validatorAddress,
184+
validatorAddress,
152185
amount: arg[0] // amount
153186
})
154187
).tx
@@ -233,7 +266,7 @@ async function runTx (
233266
}
234267
}
235268
} catch (e: any) {
236-
cmd.error(e, { exitCode: 1, code: msgType + '.tx.sign' })
269+
cmd.error(e, { exitCode: 1, code: msgType + '.tx.abort' })
237270
}
238271

239272
if (unsignedTx === undefined) {
@@ -284,7 +317,11 @@ async function getDelegatePoolTx (amount: string, options: any, cmd: Command<[st
284317
await runTx('delegate-pool', options, cmd, [amount])
285318
}
286319

287-
async function getUnstakePoolTx (amount: string, options: any, cmd: Command<[string]>): Promise<void> {
320+
async function getUnstakePoolTx (
321+
amount: string,
322+
options: { validatorIndex: '1' | '2' },
323+
cmd: Command<[string]>
324+
): Promise<void> {
288325
await runTx('unstake-pool', options, cmd, [amount])
289326
}
290327

packages/staking-cli/src/types.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export interface Config {
2222
// define validator address to interact with (delegate, undelegate etc)
2323
validatorAddress: string
2424

25+
// second validator address, used only for TON Pool
26+
validatorAddress2?: string
27+
2528
// define the expected delegator account
2629
delegatorAddress: string
2730

packages/ton/src/TonPoolStaker.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ export class TonPoolStaker extends TonBaseStaker {
1717
*/
1818
async buildStakeTx (params: {
1919
delegatorAddress: string
20-
validatorAddress: string
20+
validatorAddressPair: [string, string]
2121
amount: string
2222
validUntil?: number
2323
}): Promise<{ tx: UnsignedTx }> {
24-
const { delegatorAddress, validatorAddress, amount, validUntil } = params
24+
const { delegatorAddress, validatorAddressPair, amount, validUntil } = params
25+
const validatorAddress = await this.getPoolAddressForStake({ validatorAddressPair })
2526

2627
const payload = beginCell()
2728
.storeUint(2077040623, 32)
@@ -123,4 +124,12 @@ export class TonPoolStaker extends TonBaseStaker {
123124
receiptPrice: result.receiptPrice
124125
}
125126
}
127+
128+
/** @ignore */
129+
async getPoolAddressForStake (params: { validatorAddressPair: [string, string] }) {
130+
const { validatorAddressPair } = params
131+
// The logic to be implemented, we return the first address for now
132+
133+
return validatorAddressPair[0]
134+
}
126135
}

packages/ton/src/constants.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Contains the TON validator addresses for Chorus One's validators.
3+
*/
4+
export const CHORUS_ONE_TON_VALIDATORS = {
5+
/**
6+
* TON mainnet validator addresses.
7+
*/
8+
mainnet: {
9+
/**
10+
* Chorus One's TON Pool Pair for the mainnet
11+
*/
12+
tonPoolPair: []
13+
},
14+
/**
15+
* TON testnet validator addresses.
16+
*/
17+
testnet: {
18+
/**
19+
* Chorus One's TON Pool Pair for the testnet
20+
*/
21+
tonPoolPair: [
22+
'kQAHBakDk_E7qLlNQZxJDsqj_ruyAFpqarw85tO-c03fK26F',
23+
'kQCltujow9Sq3ZVPPU6CYGfqwDxYwjlmFGZ1Wt0bAYebio4o'
24+
]
25+
}
26+
} as const

packages/ton/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export { TonSingleNominatorPoolStaker } from './TonSingleNominatorPoolStaker'
22
export { TonNominatorPoolStaker } from './TonNominatorPoolStaker'
33
export { TonPoolStaker } from './TonPoolStaker'
44

5+
export { CHORUS_ONE_TON_VALIDATORS } from './constants'
6+
57
export {
68
TonNetworkConfig,
79
TonSigningData,

0 commit comments

Comments
 (0)