Skip to content

Commit

Permalink
Add support for 2 TON pools
Browse files Browse the repository at this point in the history
  • Loading branch information
welldan97 committed Nov 14, 2024
1 parent 7751ea9 commit ac9b5ab
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
47 changes: 42 additions & 5 deletions packages/staking-cli/src/cmd/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ function makeTxCommand (): Command {
.argument('<amount>', 'amount of tokens to stake expressed in TON denom e.g 0.1')
.action(getDelegatePoolTx)

const validateIndex = (value: string): '1' | '2' => {
if (value !== '1' && value !== '2') {
throw new Error('validator index must be 1 or 2')
}
return value
}

tx.command('unstake-pool')
.description('generate a unstake funds to TON pool contract transaction')
.argument('<amount>', 'amount of tokens to unstake expressed in TON denom e.g 0.1')
.requiredOption('-I, --validator-index <value>', 'validator index to unstake from (1 or 2)', validateIndex)
.action(getUnstakePoolTx)

tx.command('delegate-nominator-pool')
Expand Down Expand Up @@ -95,7 +103,7 @@ async function init (

async function runTx (
msgType: string,
_options: any,
options: { validatorIndex: '1' | '2' } | undefined,
cmd: Command<[string]> | Command<[string, string]> | Command<[string, string, string]> | Command<[]>,
arg: string[]
): Promise<void> {
Expand Down Expand Up @@ -132,10 +140,20 @@ async function runTx (
tonStaker = new TonPoolStaker({ ...networkConfig })
await tonStaker.init()

if (!config.validatorAddress2) {
cmd.error('second validator address is required for TON Pool', { exitCode: 1, code: `${msgType}.tx.abort` })
}
const validatorAddressPair: [string, string] = [config.validatorAddress, config.validatorAddress2]

const validatorToDelegate = await tonStaker.getPoolAddressForStake({ validatorAddressPair })
const validatorIndex = validatorToDelegate === config.validatorAddress ? 1 : 2

console.log('Delegating to validator #' + validatorIndex + ': ' + validatorToDelegate)

unsignedTx = (
await tonStaker.buildStakeTx({
delegatorAddress: config.delegatorAddress,
validatorAddress: config.validatorAddress,
validatorAddressPair,
amount: arg[0] // amount
})
).tx
Expand All @@ -145,10 +163,25 @@ async function runTx (
tonStaker = new TonPoolStaker({ ...networkConfig })
await tonStaker.init()

if (!config.validatorAddress2) {
cmd.error('second validator address is required for TON Pool', { exitCode: 1, code: `${msgType}.tx.abort` })
}

if (!options?.validatorIndex) {
cmd.error('validator index is required for TON Pool', { exitCode: 1, code: `${msgType}.tx.abort` })
}
const validatorAddressPair: [string, string] = [config.validatorAddress, config.validatorAddress2]

const validatorIndex = parseInt(options.validatorIndex)

const validatorAddress = validatorAddressPair[validatorIndex - 1]

console.log('Unstaking from validator #' + validatorIndex + ': ' + validatorAddress)

unsignedTx = (
await tonStaker.buildUnstakeTx({
delegatorAddress: config.delegatorAddress,
validatorAddress: config.validatorAddress,
validatorAddress,
amount: arg[0] // amount
})
).tx
Expand Down Expand Up @@ -233,7 +266,7 @@ async function runTx (
}
}
} catch (e: any) {
cmd.error(e, { exitCode: 1, code: msgType + '.tx.sign' })
cmd.error(e, { exitCode: 1, code: msgType + '.tx.abort' })
}

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

async function getUnstakePoolTx (amount: string, options: any, cmd: Command<[string]>): Promise<void> {
async function getUnstakePoolTx (
amount: string,
options: { validatorIndex: '1' | '2' },
cmd: Command<[string]>
): Promise<void> {
await runTx('unstake-pool', options, cmd, [amount])
}

Expand Down
3 changes: 3 additions & 0 deletions packages/staking-cli/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export interface Config {
// define validator address to interact with (delegate, undelegate etc)
validatorAddress: string

// second validator address, used only for TON Pool
validatorAddress2?: string

// define the expected delegator account
delegatorAddress: string

Expand Down
13 changes: 11 additions & 2 deletions packages/ton/src/TonPoolStaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export class TonPoolStaker extends TonBaseStaker {
*/
async buildStakeTx (params: {
delegatorAddress: string
validatorAddress: string
validatorAddressPair: [string, string]
amount: string
validUntil?: number
}): Promise<{ tx: UnsignedTx }> {
const { delegatorAddress, validatorAddress, amount, validUntil } = params
const { delegatorAddress, validatorAddressPair, amount, validUntil } = params
const validatorAddress = await this.getPoolAddressForStake({ validatorAddressPair })

const payload = beginCell()
.storeUint(2077040623, 32)
Expand Down Expand Up @@ -123,4 +124,12 @@ export class TonPoolStaker extends TonBaseStaker {
receiptPrice: result.receiptPrice
}
}

/** @ignore */
async getPoolAddressForStake (params: { validatorAddressPair: [string, string] }) {
const { validatorAddressPair } = params
// The logic to be implemented, we return the first address for now

return validatorAddressPair[0]
}
}
26 changes: 26 additions & 0 deletions packages/ton/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Contains the TON validator addresses for Chorus One's validators.
*/
export const CHORUS_ONE_TON_VALIDATORS = {
/**
* TON mainnet validator addresses.
*/
mainnet: {
/**
* Chorus One's TON Pool Pair for the mainnet
*/
tonPoolPair: []
},
/**
* TON testnet validator addresses.
*/
testnet: {
/**
* Chorus One's TON Pool Pair for the testnet
*/
tonPoolPair: [
'kQAHBakDk_E7qLlNQZxJDsqj_ruyAFpqarw85tO-c03fK26F',
'kQCltujow9Sq3ZVPPU6CYGfqwDxYwjlmFGZ1Wt0bAYebio4o'
]
}
} as const
2 changes: 2 additions & 0 deletions packages/ton/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export { TonSingleNominatorPoolStaker } from './TonSingleNominatorPoolStaker'
export { TonNominatorPoolStaker } from './TonNominatorPoolStaker'
export { TonPoolStaker } from './TonPoolStaker'

export { CHORUS_ONE_TON_VALIDATORS } from './constants'

export {
TonNetworkConfig,
TonSigningData,
Expand Down

0 comments on commit ac9b5ab

Please sign in to comment.