Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TON Pool Staker #11

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 112 additions & 13 deletions packages/staking-cli/src/cmd/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { SignerType } from '../enums'
import { prompt, readConfig, getNetworkConfig, log, defaultLogger } from '../util'
import { SafeJSONStringify } from '@chorus-one/utils'
import { newSigner } from '../signer'
import { TonStaker } from '@chorus-one/ton'
import { TonPoolStaker, TonNominatorPoolStaker, TonSingleNominatorPoolStaker } from '@chorus-one/ton'

export interface CLINetworkConfig extends TonNetworkConfig {
// block explorer URL to display Transaction ID via Web UI. Example:
Expand All @@ -29,6 +29,24 @@ function makeTxCommand (): Command {
.option('-b, --broadcast', 'broadcast generated transaction', false)
.option('-j, --journal <value>', "write TX'es to the local journal log", 'true')

tx.command('delegate-pool')
.description('generate a delegate funds to TON pool contract transaction')
.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')
.description('generate a delegate funds to TON nominator pool contract transaction')
.argument('<amount>', 'amount of tokens to stake expressed in TON denom e.g 0.1')
Expand Down Expand Up @@ -70,11 +88,11 @@ async function init (

const networkConfig = getNetworkConfig<CLINetworkConfig>(config)
const signer = await newSigner(config, signerType as SignerType, {
addressDerivationFn: TonStaker.getAddressDerivationFn({
addressDerivationFn: TonNominatorPoolStaker.getAddressDerivationFn({
addressDerivationConfig: networkConfig.addressDerivationConfig
}),
mnemonicToSeedFn: TonStaker.getMnemonicToSeedFn(),
seedToKeypairFn: TonStaker.getSeedToKeypairFn(),
mnemonicToSeedFn: TonNominatorPoolStaker.getMnemonicToSeedFn(),
seedToKeypairFn: TonNominatorPoolStaker.getSeedToKeypairFn(),
keyType: KeyType.ED25519,
logger: defaultLogger
})
Expand All @@ -85,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 All @@ -95,9 +113,6 @@ async function runTx (
const [config, signer] = await init(cmd)

const networkConfig = getNetworkConfig<CLINetworkConfig>(config)
const tonStaker: TonStaker = new TonStaker({ ...networkConfig })
await tonStaker.init()

const logger = defaultLogger

logger.info(0, 3, 'inspect input data')
Expand All @@ -117,11 +132,65 @@ async function runTx (

let unsignedTx: UnsignedTx | undefined

let tonStaker: TonPoolStaker | TonNominatorPoolStaker | TonSingleNominatorPoolStaker

try {
switch (msgType) {
case 'delegate-pool': {
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({
validatorAddressPair,
amount: arg[0] // amount
})
).tx
break
}
case 'unstake-pool': {
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({
validatorAddress,
amount: arg[0] // amount
})
).tx
break
}
case 'delegate-nominator-pool': {
tonStaker = new TonNominatorPoolStaker({ ...networkConfig })
await tonStaker.init()

unsignedTx = (
await tonStaker.buildStakeNominatorPoolTx({
await tonStaker.buildStakeTx({
delegatorAddress: config.delegatorAddress,
validatorAddress: config.validatorAddress,
amount: arg[0] // amount
Expand All @@ -130,17 +199,23 @@ async function runTx (
break
}
case 'unstake-nominator-pool': {
tonStaker = new TonNominatorPoolStaker({ ...networkConfig })
await tonStaker.init()

unsignedTx = (
await tonStaker.buildUnstakeNominatorPoolTx({
await tonStaker.buildUnstakeTx({
delegatorAddress: config.delegatorAddress,
validatorAddress: config.validatorAddress
})
).tx
break
}
case 'delegate-single-nominator-pool': {
tonStaker = new TonSingleNominatorPoolStaker({ ...networkConfig })
await tonStaker.init()

unsignedTx = (
await tonStaker.buildStakeSingleNominatorPoolTx({
await tonStaker.buildStakeTx({
delegatorAddress: config.delegatorAddress,
validatorAddress: config.validatorAddress,
amount: arg[0] // amount
Expand All @@ -149,8 +224,11 @@ async function runTx (
break
}
case 'unstake-single-nominator-pool': {
tonStaker = new TonSingleNominatorPoolStaker({ ...networkConfig })
await tonStaker.init()

unsignedTx = (
await tonStaker.buildUnstakeSingleNominatorPoolTx({
await tonStaker.buildUnstakeTx({
delegatorAddress: config.delegatorAddress,
validatorAddress: config.validatorAddress,
amount: arg[0] // amount
Expand All @@ -159,6 +237,9 @@ async function runTx (
break
}
case 'transfer': {
tonStaker = new TonNominatorPoolStaker({ ...networkConfig })
await tonStaker.init()

unsignedTx = (
await tonStaker.buildTransferTx({
destinationAddress: config.validatorAddress,
Expand All @@ -168,16 +249,22 @@ async function runTx (
break
}
case 'deploy-wallet': {
tonStaker = new TonNominatorPoolStaker({ ...networkConfig })
await tonStaker.init()

unsignedTx = (
await tonStaker.buildDeployWalletTx({
address: config.delegatorAddress
})
).tx
break
}
default: {
cmd.error('unsupported message type', { exitCode: 1, code: `${msgType}.tx.unsupported` })
}
}
} 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 @@ -224,6 +311,18 @@ async function runTx (
}
}

async function getDelegatePoolTx (amount: string, options: any, cmd: Command<[string]>): Promise<void> {
await runTx('delegate-pool', options, cmd, [amount])
}

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

async function getDelegateNominatorPoolTx (amount: string, options: any, cmd: Command<[string]>): Promise<void> {
await runTx('delegate-nominator-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
Loading
Loading