Skip to content

Commit

Permalink
Tests: Fixed some minor issues brought up by auditors. Added tests fr…
Browse files Browse the repository at this point in the history
…om auditors for issues they found.

Bootstrap: Changes for contract update support being removed
Contracts:
- Removed the ability for the ValidatorRegistry and StakingPool contracts to be updated as they will once again be immutable only.
- Moved many types and constants from validatorRegistry into validatorConfigs.algo.ts file to tidy up the registry contract file somewhat.
- Switched contracts to AVM 11 and updated placeholder methods for getting fee to go online, incentive eligibility, max allowed per pool, current online stake with new AVM opcodes to fetch the data from consensus params or current chain state.
- Uncommented and enabled arc28 events for reti operations, logging information which chain 'watchers' can easily watch for and parse.  These were disabled due to prior algosdk versions not supporting the json in the generated client files.
- Corrected code addressing 4 findings from audit, HI-01, ME-02, ME-03, MI-05. ME/MI-01 not addressed as behavior is intentional.
- Addressed a variety of helpful suggestions from auditors to tidy a few things up - de-duping/reusing of some code - fixing some incorrect comments, etc.
  • Loading branch information
pbennett committed Sep 5, 2024
1 parent fc374b5 commit f10632f
Show file tree
Hide file tree
Showing 27 changed files with 24,525 additions and 22,177 deletions.
594 changes: 578 additions & 16 deletions contracts/__test__/contracts.test.ts

Large diffs are not rendered by default.

117 changes: 35 additions & 82 deletions contracts/bootstrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { AlgoClientConfig } from '@algorandfoundation/algokit-utils/types/networ
import { ClientManager } from '@algorandfoundation/algokit-utils/types/client-manager'
import { StakingPoolClient } from '../contracts/clients/StakingPoolClient'
import { ValidatorRegistryClient } from '../contracts/clients/ValidatorRegistryClient'
import { getPools } from '../helpers/helpers'

function getNetworkConfig(network: string): [AlgoClientConfig, bigint, string] {
let registryAppID: bigint
Expand Down Expand Up @@ -79,21 +78,18 @@ function createViteEnvFileForLocalnet(validatorAppId: number | bigint): void {
}

async function main() {
const args = await yargs
.option('network', {
default: 'localnet',
choices: ['localnet', 'betanet', 'testnet', 'mainnet'],
demandOption: true,
})
.option('update', { type: 'boolean', default: false })
.option('id', { type: 'number', default: 0 }).argv
const args = await yargs.option('network', {
default: 'localnet',
choices: ['localnet', 'betanet', 'testnet', 'mainnet'],
demandOption: true,
}).argv

const [algodConfig, registryAppID, feeSink] = getNetworkConfig(args.network)

// default to localnet
let algorand: AlgorandClient = algokit.AlgorandClient.defaultLocalNet()
let algorand: AlgorandClient = AlgorandClient.defaultLocalNet()
if (args.network !== 'localnet') {
algorand = algokit.AlgorandClient.fromConfig({ algodConfig, indexerConfig: undefined, kmdConfig: undefined })
algorand = AlgorandClient.fromConfig({ algodConfig, indexerConfig: undefined, kmdConfig: undefined })
}
// const algorand = algokit.AlgorandClient.fromConfig({ algodConfig: algodconfig, kmdConfig })

Expand Down Expand Up @@ -137,72 +133,55 @@ async function main() {
sender: creatorAcct,
resolveBy: 'id',
id: 0,
deployTimeParams: {
nfdRegistryAppId: Number(registryAppID),
feeSinkAddr: decodeAddress(feeSink).publicKey,
},
},
algorand.client.algod,
)
const { approvalCompiled } = await poolClient.appClient.compile({
deployTimeParams: {
nfdRegistryAppId: registryAppID,
feeSinkAddr: decodeAddress(feeSink).publicKey,
},
})
const { approvalCompiled } = await poolClient.appClient.compile()

// first we have to deploy a staking pool contract instance for future use by the staking master contract which uses it as its
// 'reference' instance when creating new staking pool contract instances.
const validatorClient = new ValidatorRegistryClient(
{
sender: creatorAcct,
resolveBy: 'id',
id: args.id,
id: 0,
deployTimeParams: {
nfdRegistryAppId: registryAppID,
nfdRegistryAppId: Number(registryAppID),
},
},
algorand.client.algod,
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let validatorApp: any
if (!args.update) {
console.log(`creating application`)
if (args.network === 'localnet') {
console.log(`funding ${creatorAcct.addr}`)
await algokit.ensureFunded(
{
accountToFund: creatorAcct,
fundingSource: await algorand.account.localNetDispenser(),
minSpendingBalance: AlgoAmount.Algos(200),
},
algorand.client.algod,
)
}
validatorApp = await validatorClient.create.createApplication({}, { schema: { extraPages: 3 } })

console.log(`Validator registry app id is:${validatorApp.appId}`)
console.log(`Validator Contract HASH is:${validatorApp.compiledApproval.compiledHash}`)

// Fund the validator w/ 2 ALGO for contract mbr reqs.
await algokit.transferAlgos(
console.log(`creating application`)
if (args.network === 'localnet') {
console.log(`funding ${creatorAcct.addr}`)
await algokit.ensureFunded(
{
from: creatorAcct,
to: validatorApp.appAddress,
amount: AlgoAmount.Algos(2),
accountToFund: creatorAcct,
fundingSource: await algorand.account.localNetDispenser(),
minSpendingBalance: AlgoAmount.Algos(200),
},
algorand.client.algod,
)
} else {
if (args.id === 0) {
// error - id must be defined!
console.error('Error: id must be defined!')
process.exit(1)
}
console.log(`updating application ${args.id}`)
validatorApp = await validatorClient.update.updateApplication(
{},
{ sendParams: { populateAppCallResources: true } },
)
console.log(`application ${args.id} updated`)
console.log(`Validator Contract HASH is:${validatorApp.compiledApproval.compiledHash}`)
}
const validatorApp = await validatorClient.create.createApplication({}, { schema: { extraPages: 3 } })

console.log(`Validator registry app id is:${validatorApp.appId}`)
console.log(`Validator Contract HASH is:${validatorApp.compiledApproval!.compiledHash}`)

// Fund the validator w/ 2 ALGO for contract mbr reqs.
await algokit.transferAlgos(
{
from: creatorAcct,
to: validatorApp.appAddress,
amount: AlgoAmount.Algos(2),
},
algorand.client.algod,
)

console.log(
`loading the ${approvalCompiled.compiledBase64ToBytes.length} bytes of the staking contract into the validator contracts box storage`,
Expand Down Expand Up @@ -246,32 +225,6 @@ async function main() {
// Create a .env.localnet file in the ui directory with the validator app id
createViteEnvFileForLocalnet(validatorApp.appId ?? args.id)
}

if (args.update) {
// Fetch all validators, and all their pools - updating all of them.
const numV = (await validatorClient.getGlobalState()).numV!.asNumber()
for (let valId = 1; valId <= numV; valId += 1) {
// const state = await getValidatorState(validatorClient, valId)
const pools = await getPools(validatorClient, valId)
for (const pool of pools) {
const updPoolClient = new StakingPoolClient(
{
sender: creatorAcct,
resolveBy: 'id',
id: pool.poolAppId,
deployTimeParams: {
nfdRegistryAppId: registryAppID,
feeSinkAddr: decodeAddress(feeSink).publicKey,
},
},
algorand.client.algod,
)
console.log(`updating validator:${valId}, pool appid: ${pool.poolAppId}`)
// eslint-disable-next-line no-await-in-loop
await updPoolClient.update.updateApplication({})
}
}
}
}

main()
6 changes: 3 additions & 3 deletions contracts/bootstrap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bootstrap",
"version": "0.9.8",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
Expand All @@ -11,8 +11,8 @@
},
"license": "MIT",
"dependencies": {
"@algorandfoundation/algokit-utils": "^6.1.2",
"algosdk": "^2.8.0",
"@algorandfoundation/algokit-utils": "^6.2.1",
"algosdk": "^2.9.0",
"prompts": "^2.4.2",
"yargs": "^17.7.2"
},
Expand Down
Loading

0 comments on commit f10632f

Please sign in to comment.