Skip to content

Commit

Permalink
Ethereum: Add tests for the rest of transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
welldan97 committed Aug 1, 2024
1 parent 456b99f commit 53d0702
Show file tree
Hide file tree
Showing 10 changed files with 1,515 additions and 3,323 deletions.
4,380 changes: 1,145 additions & 3,235 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.7",
"@types/node": "^20.9.0",
"chai": "^4.0.0",
"chai-promised": "^1.0.6",
"chai-spies": "^1.1.0",
"chai": "^4.0.0",
"globals": "^15.4.0",
"mocha": "^10.6.0",
"prettier": "^3.3.2",
"tsx": "^4.16.2",
"turbo": "^2.0.6",
"typedoc-plugin-markdown": "^3.17.1",
"typedoc": "^0.25.3",
"typescript-eslint": "^7.13.0",
"typescript": "^5.4.0"
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "^5.4.0",
"typescript-eslint": "^7.13.0"
},
"workspaces": [
"packages/*"
Expand Down
1 change: 0 additions & 1 deletion packages/ethereum/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable */
/** @type import('hardhat/config').HardhatUserConfig */
require('@nomicfoundation/hardhat-toolbox-viem')
const config = require('./test/lib/hardhat.json')
module.exports = config
10 changes: 4 additions & 6 deletions packages/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "All-in-one toolkit for building staking dApps on Ethereum network",
"scripts": {
"build": "rm -fr dist/* && tsc -p tsconfig.mjs.json --outDir dist/mjs && tsc -p tsconfig.cjs.json --outDir dist/cjs && bash ../../scripts/fix-package-json",
"hardhat:start": "hardhat node > hardhat.log",
"hardhat:start": "hardhat node",
"hardhat:stop": "kill $(lsof -t -i:8545) > /dev/null 2>&1",
"test:integration": "mocha --timeout 10000 '**/*.spec.ts'",
"test": "npm run hardhat:start & (sleep 5; npm run test:integration; status=$?; npm run hardhat:stop; exit $status)"
"test:integration": "mocha --timeout 20000 '**/*.spec.ts'",
"test": "npm run hardhat:start > hardhat.log & (sleep 5; npm run test:integration; status=$?; npm run hardhat:stop; exit $status)"
},
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand Down Expand Up @@ -42,8 +42,6 @@
"viem": "^2.17.4"
},
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"@nomicfoundation/hardhat-toolbox-viem": "^3.0.0",
"hardhat": "^2.22.7"
"hardhat": "2.19.4"
}
}
92 changes: 92 additions & 0 deletions packages/ethereum/test/buildBurnTx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { EthereumStaker } from '@chorus-one/ethereum'
import { Hex, PublicClient, WalletClient, erc20Abi, formatEther, parseEther } from 'viem'
import { assert } from 'chai'
import { mint, prepareTests, stake } from './lib/utils'
import { describe } from 'mocha'

const amountToStake = parseEther('20')
const amountToMint = parseEther('15')
const amountToBurn = parseEther('10')

describe('EthStaker.buildBurnTx', () => {
let delegatorAddress: Hex
let validatorAddress: Hex
let publicClient: PublicClient
let walletClient: WalletClient
let staker: EthereumStaker
let osEthTokenAddress: Hex

beforeEach(async () => {
const setup = await prepareTests()

delegatorAddress = setup.walletClient.account.address
validatorAddress = setup.validatorAddress
publicClient = setup.publicClient
walletClient = setup.walletClient
staker = setup.staker
osEthTokenAddress = setup.osEthTokenAddress

await stake({
delegatorAddress,
validatorAddress,
amountToStake,
publicClient,
walletClient,
staker
})
})

it('should build a burning tx', async () => {
await mint({
delegatorAddress,
validatorAddress,
amountToMint,
publicClient,
walletClient,
staker
})

const { maxMint: maxMintAfterMint } = await staker.getMint({
delegatorAddress,
validatorAddress
})

const { tx } = await staker.buildBurnTx({
delegatorAddress,
validatorAddress,
amount: formatEther(amountToBurn)
})

const request = await walletClient.prepareTransactionRequest({
...tx,
chain: undefined
})
const hash = await walletClient.sendTransaction({
...request,
account: delegatorAddress
})

const receipt = await publicClient.waitForTransactionReceipt({ hash })
assert.equal(receipt.status, 'success')

const { maxMint: maxMintAfterBurn } = await staker.getMint({
delegatorAddress,
validatorAddress
})

// Take into account vault fees
assert.closeTo(
Number(parseEther(maxMintAfterBurn)),
Number(parseEther(maxMintAfterMint) + amountToBurn),
Number(parseEther('0.001'))
)

const osEthBalance = await publicClient.readContract({
abi: erc20Abi,
address: osEthTokenAddress,
functionName: 'balanceOf',
args: [delegatorAddress]
})
assert.equal(osEthBalance, amountToMint - amountToBurn)
})
})
75 changes: 75 additions & 0 deletions packages/ethereum/test/buildMintTx.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { EthereumStaker } from '@chorus-one/ethereum'
import { Hex, PublicClient, WalletClient, erc20Abi, parseEther } from 'viem'
import { assert } from 'chai'
import { mint, prepareTests, stake } from './lib/utils'

const amountToStake = parseEther('20')
const amountToMint = parseEther('1')

describe('EthereumStaker.buildMintTx', () => {
let delegatorAddress: Hex
let validatorAddress: Hex
let publicClient: PublicClient
let walletClient: WalletClient
let staker: EthereumStaker
let osEthTokenAddress: Hex

beforeEach(async () => {
const setup = await prepareTests()

delegatorAddress = setup.walletClient.account.address
validatorAddress = setup.validatorAddress
publicClient = setup.publicClient
walletClient = setup.walletClient
staker = setup.staker
osEthTokenAddress = setup.osEthTokenAddress

await stake({
delegatorAddress,
validatorAddress,
amountToStake,
publicClient,
walletClient,
staker
})
})

it('should build a minting tx', async () => {
const { maxMint } = await staker.getMint({
delegatorAddress,
validatorAddress
})

assert(parseEther(maxMint) > 0n)
assert(parseEther(maxMint) > amountToMint)

await mint({
delegatorAddress,
validatorAddress,
amountToMint,
publicClient,
walletClient,
staker
})

const { maxMint: maxMintAfter } = await staker.getMint({
delegatorAddress,
validatorAddress
})

// Take into account vault fees
assert.closeTo(
Number(parseEther(maxMintAfter)),
Number(parseEther(maxMint) - amountToMint),
Number(parseEther('0.001'))
)

const osEthBalance = await publicClient.readContract({
abi: erc20Abi,
address: osEthTokenAddress,
functionName: 'balanceOf',
args: [delegatorAddress]
})
assert.equal(osEthBalance, amountToMint)
})
})
116 changes: 42 additions & 74 deletions packages/ethereum/test/buildStakeTx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,72 @@
import { EthereumStaker } from '@chorus-one/ethereum'
import { Hex, PublicClient, WalletClient, formatEther, parseEther } from 'viem'
import { mine } from '@nomicfoundation/hardhat-toolbox/network-helpers'
import { assert } from 'chai'
import { prepareTests } from './lib/utils'
import { Hex, PublicClient, WalletClient, parseEther } from 'viem'
import { assert, assert } from 'chai'
import { prepareTests, stake } from './lib/utils'

const AMOUNT_TO_STAKE = parseEther('2')
const amountToStake = parseEther('2')

describe('EthereumStaker.buildStakeTx', () => {
let USER_ADDRESS: Hex
let VAULT_ADDRESS: Hex
let walletClientWithBalance: WalletClient
let delegatorAddress: Hex
let validatorAddress: Hex
let walletClient: WalletClient
let publicClient: PublicClient
let staker: EthereumStaker

beforeEach(async () => {
const {
validatorAddress,
walletClient,
publicClient: initialPublicClient,
staker: initialStaker
} = await prepareTests()
USER_ADDRESS = walletClient.account.address
VAULT_ADDRESS = validatorAddress
walletClientWithBalance = walletClient
publicClient = initialPublicClient
staker = initialStaker
const setup = await prepareTests()

delegatorAddress = setup.walletClient.account.address
validatorAddress = setup.validatorAddress
walletClient = setup.walletClient
publicClient = setup.publicClient
staker = setup.staker
})

it('Should build staking tx', async () => {
it('should build a staking tx', async () => {
const balanceBefore = await publicClient.getBalance({
address: USER_ADDRESS
})

const { balance: stakeBefore } = await staker.getStake({
delegatorAddress: USER_ADDRESS,
validatorAddress: VAULT_ADDRESS
address: delegatorAddress
})

const { tx } = await staker.buildStakeTx({
delegatorAddress: USER_ADDRESS,
validatorAddress: VAULT_ADDRESS,
amount: formatEther(AMOUNT_TO_STAKE)
})

const request = await walletClientWithBalance.prepareTransactionRequest({
...tx,
chain: undefined
})
const hash = await walletClientWithBalance.sendTransaction({
...request,
account: USER_ADDRESS
await stake({
delegatorAddress,
validatorAddress,
amountToStake,
publicClient,
walletClient,
staker
})
await mine(10)

const receipt = await publicClient.getTransactionReceipt({ hash })
assert.equal(receipt.status, 'success')

const balanceAfter = await publicClient.getBalance({
address: USER_ADDRESS
address: delegatorAddress
})

const { balance: stakeAfter } = await staker.getStake({
delegatorAddress: USER_ADDRESS,
validatorAddress: VAULT_ADDRESS
delegatorAddress,
validatorAddress
})

// Take into account gas fees
assert.isTrue(balanceAfter < balanceBefore - AMOUNT_TO_STAKE)
assert.equal(parseEther(stakeAfter), parseEther(stakeBefore) + AMOUNT_TO_STAKE)
assert.closeTo(Number(balanceAfter), Number(balanceBefore - amountToStake), Number(parseEther('0.001')))
assert.equal(parseEther(stakeAfter), amountToStake)
})

it('Should build stake tx with referrer', async () => {
const { balance: stakeBefore } = await staker.getStake({
delegatorAddress: USER_ADDRESS,
validatorAddress: VAULT_ADDRESS
})

const { tx } = await staker.buildStakeTx({
delegatorAddress: USER_ADDRESS,
validatorAddress: VAULT_ADDRESS,
amount: formatEther(AMOUNT_TO_STAKE),
referrer: '0x4242424242424242424242424242424242424242'
})
const request = await walletClientWithBalance.prepareTransactionRequest({
...tx,
chain: undefined
})
const hash = await walletClientWithBalance.sendTransaction({
...request,
account: USER_ADDRESS
it('should build a staking tx with referrer', async () => {
await stake({
delegatorAddress,
validatorAddress,
referrer: '0x4242424242424242424242424242424242424242',
amountToStake,
publicClient,
walletClient,
staker
})
await mine(10)

const receipt = await publicClient.waitForTransactionReceipt({ hash })
assert.equal(receipt.status, 'success')

const { balance: stakeAfter } = await staker.getStake({
delegatorAddress: USER_ADDRESS,
validatorAddress: VAULT_ADDRESS
delegatorAddress,
validatorAddress
})
assert.equal(parseEther(stakeAfter), parseEther(stakeBefore) + AMOUNT_TO_STAKE)

// Take into account 1 wei assets conversion issues on the contract
assert.closeTo(Number(parseEther(stakeAfter)), Number(amountToStake), 1)
})
})
Loading

0 comments on commit 53d0702

Please sign in to comment.