-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ethereum: Add tests for the rest of transactions
- Loading branch information
Showing
10 changed files
with
1,515 additions
and
3,323 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Oops, something went wrong.