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

add : added 'StakeUpdate' event #161

Merged
merged 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ The `express-cli` also comes with additional utility commands, listed below. Som

- `../../bin/express-cli --send-staked-event`

- Create a `staked` transaction on the remote network and adds a new validator.
- Create a `Staked` transaction on the remote network and adds a new validator.

- `../../bin/express-cli --send-stakeupdate-event`

- Create a `StakeUpdate` transaction on the remote network and increase stake of 1st validator by 100 MATIC.

- ` ../../bin/express-cli --monitor [exit]`

Expand Down
15 changes: 15 additions & 0 deletions src/express-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { terraformDestroy } from './express/commands/destroy'
import { startStressTest } from './express/commands/stress'
import { sendStateSyncTx } from './express/commands/send-state-sync'
import { sendStakedEvent } from './express/commands/send-staked-event'
import { sendStakeUpdateEvent } from './express/commands/send-stake-update'
import { monitor } from './express/commands/monitor'
import {
restartAll,
Expand Down Expand Up @@ -62,6 +63,10 @@ program
)
.option('-ss, --send-state-sync', 'Send state sync tx')
.option('-sstake, --send-staked-event', 'Send staked event')
.option(
'-sstakeupdate, --send-stakedupdate-event',
'Send staked-update event'
)
.option(
'-e1559, --eip-1559-test [index]',
'Test EIP 1559 txs. In case of a non-dockerized devnet, if an integer [index] is specified, it will use that VM to send the tx. Otherwise, it will target the first VM.'
Expand Down Expand Up @@ -269,6 +274,16 @@ export async function cli() {
}
await timer(3000)
await sendStakedEvent()
} else if (options.sendStakedupdateEvent) {
console.log('📍Command --send-stakeupdate-event ')
if (!checkDir(false)) {
console.log(
'❌ The command is not called from the appropriate devnet directory!'
)
process.exit(1)
}
await timer(3000)
await sendStakeUpdateEvent()
} else if (options.eip1559Test) {
console.log('📍Command --eip-1559-test')
if (!checkDir(false)) {
Expand Down
141 changes: 141 additions & 0 deletions src/express/commands/send-stake-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// noinspection JSUnresolvedVariable

import { loadDevnetConfig } from '../common/config-utils'
import stakeManagerABI from '../../abi/StakeManagerABI.json'
import ERC20ABI from '../../abi/ERC20ABI.json'
import Web3 from 'web3'
import { timer } from '../common/time-utils'

const {
runScpCommand,
runSshCommandWithReturn,
maxRetries
} = require('../common/remote-worker')

export async function sendStakeUpdateEvent() {
require('dotenv').config({ path: `${process.cwd()}/.env` })
const devnetType =
process.env.TF_VAR_DOCKERIZED === 'yes' ? 'docker' : 'remote'

const doc = await loadDevnetConfig(devnetType)

if (doc.devnetBorHosts.length > 0) {
console.log('📍Monitoring the first node', doc.devnetBorHosts[0])
} else {
console.log('📍No nodes to monitor, please check your configs! Exiting...')
process.exit(1)
}

const machine0 = doc.devnetBorHosts[0]
const rootChainWeb3 = new Web3(`http://${machine0}:9545`)

let src = `${doc.ethHostUser}@${machine0}:~/matic-cli/devnet/devnet/signer-dump.json`
let dest = './signer-dump.json'
await runScpCommand(src, dest, maxRetries)

src = `${doc.ethHostUser}@${machine0}:~/matic-cli/devnet/code/contracts/contractAddresses.json`
dest = './contractAddresses.json'
await runScpCommand(src, dest, maxRetries)

const contractAddresses = require(`${process.cwd()}/contractAddresses.json`)

const StakeManagerProxyAddress = contractAddresses.root.StakeManagerProxy

const MaticTokenAddr = contractAddresses.root.tokens.TestToken
const MaticTokenContract = new rootChainWeb3.eth.Contract(
ERC20ABI,
MaticTokenAddr
)

const signerDump = require(`${process.cwd()}/signer-dump.json`)
const pkey = signerDump[0].priv_key
const validatorAccount = signerDump[0].address
const validatorIDForTest = '1'

const stakeManagerContract = new rootChainWeb3.eth.Contract(
stakeManagerABI,
StakeManagerProxyAddress
)

let tx = MaticTokenContract.methods.approve(
StakeManagerProxyAddress,
rootChainWeb3.utils.toWei('1000')
)
let signedTx = await getSignedTx(
rootChainWeb3,
MaticTokenAddr,
tx,
validatorAccount,
pkey
)
const approvalReceipt = await rootChainWeb3.eth.sendSignedTransaction(
signedTx.rawTransaction
)
console.log(
'\n\nApproval Receipt txHash: ' + approvalReceipt.transactionHash
)

const oldValidatarPower = await getValidatorPower(doc, validatorIDForTest)
console.log('Old Validator Power: ' + oldValidatarPower)

// Adding 100 MATIC stake
tx = stakeManagerContract.methods.restake(
validatorIDForTest,
rootChainWeb3.utils.toWei('100'),
false
)
signedTx = await getSignedTx(
rootChainWeb3,
StakeManagerProxyAddress,
tx,
validatorAccount,
pkey
)
const Receipt = await rootChainWeb3.eth.sendSignedTransaction(
signedTx.rawTransaction
)
console.log('Restake Receipt txHash: ' + Receipt.transactionHash)

let newValidatarPower = await getValidatorPower(doc, validatorIDForTest)

while (parseInt(newValidatarPower) !== parseInt(oldValidatarPower) + 100) {
console.log('Waiting 3 secs for stakeupdate')
await timer(3000) // waiting 10 seconds for the validator to be added
newValidatarPower = await getValidatorPower(doc, validatorIDForTest)
console.log('newValidatarPower : ', newValidatarPower)
}

console.log('✅ Stake Updated')
console.log(
'✅ Stake-Update event Sent from Rootchain and Received and processed on Heimdall'
)
}

async function getValidatorPower(doc, validatorID) {
const machine0 = doc.devnetBorHosts[0]
const command = `curl localhost:1317/staking/validator/${validatorID}`
const out = await runSshCommandWithReturn(
`${doc.ethHostUser}@${machine0}`,
command,
maxRetries
)
const outobj = JSON.parse(out)
return outobj.result.power
}

async function getSignedTx(web3object, to, tx, validatorAccount, privateKey) {
const gas = await tx.estimateGas({ from: validatorAccount })
const data = tx.encodeABI()

const signedTx = await web3object.eth.accounts.signTransaction(
{
from: validatorAccount,
to,
data,
gas
},
privateKey
)

return signedTx
}