Skip to content

Commit ab1b331

Browse files
committed
Fix rest of the tests for ethereum
1 parent 624eb58 commit ab1b331

13 files changed

+233
-37
lines changed

packages/ethereum/src/lib/methods/getMint.ts

+1-20
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@ import { StakewiseConnector } from '../connector'
66
export const getMint = async (params: { connector: StakewiseConnector; userAccount: Hex; vaultAddress: Hex }) => {
77
const { connector, vaultAddress, userAccount } = params
88

9-
const gqlMintedSharesJson = await connector.graphqlRequest({
10-
op: 'OsTokenPositions',
11-
type: 'graph',
12-
query: `
13-
query OsTokenPositions($address: Bytes, $vaultAddress: String) { osTokenPositions(where: { address: $address, vault: $vaultAddress }) { shares }}
14-
`,
15-
variables: {
16-
vaultAddress,
17-
address: userAccount
18-
}
19-
})
20-
21-
if (!gqlMintedSharesJson.data.osTokenPositions) {
22-
throw new Error(`Minted shares data is missing the osTokenPositions field`)
23-
}
24-
const gqlMintedShares = BigInt(gqlMintedSharesJson.data.osTokenPositions[0]?.shares || 0)
25-
269
const mintedShares = await connector.eth.readContract({
2710
abi: VaultABI,
2811
functionName: 'osTokenPositions',
@@ -48,9 +31,7 @@ export const getMint = async (params: { connector: StakewiseConnector; userAccou
4831
return {
4932
minted: {
5033
assets: mintedAssets,
51-
shares: mintedShares,
52-
fee: mintedShares - gqlMintedShares,
53-
mintedWithoutFee: gqlMintedShares
34+
shares: mintedShares
5435
},
5536
protocolFeePercent
5637
}

packages/ethereum/src/lib/methods/getMintHealth.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const getMintHealth = async (params: {
77
connector: StakewiseConnector
88
mintedShares: bigint
99
stakedAssets: bigint
10-
}): Promise<'healthy' | 'moderate' | 'risky' | 'unhealthy'> => {
10+
}): Promise<'healthy' | 'risky'> => {
1111
const { connector, mintedShares, stakedAssets } = params
1212

1313
const mintedAssets = await (connector.eth.readContract({
@@ -33,10 +33,7 @@ export const getMintHealth = async (params: {
3333
// a small gap to notify the user in advance of problems with the position
3434
if (healthFactor >= 1.02) {
3535
return 'healthy'
36-
} else if (healthFactor >= 1.01) {
37-
return 'moderate'
38-
} else if (healthFactor >= 1.0) {
39-
return 'risky'
4036
}
41-
return 'unhealthy'
37+
38+
return 'risky'
4239
}

packages/ethereum/src/lib/methods/getVault.ts

-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ async function extractVaultProperties (connector: StakewiseConnector, vault: Hex
4141
displayName
4242
description
4343
whitelister
44-
keysManager
4544
tokenSymbol
4645
feeRecipient
47-
validatorsRoot
4846
blocklistCount
4947
whitelistCount
5048
blocklistManager

packages/ethereum/src/staker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ export class EthereumStaker {
379379
})
380380

381381
return {
382-
balance: formatEther(mint.minted.mintedWithoutFee),
382+
balance: formatEther(mint.minted.shares),
383383
maxMint: formatEther(maxMint)
384384
}
385385
}

packages/ethereum/test/buildMintTx.spec.ts

+69-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { assert } from 'chai'
44
import { mint, prepareTests, stake } from './lib/utils'
55

66
const amountToStake = parseEther('20')
7-
const amountToMint = parseEther('1')
87

98
describe('EthereumStaker.buildMintTx', () => {
109
let delegatorAddress: Hex
@@ -34,11 +33,12 @@ describe('EthereumStaker.buildMintTx', () => {
3433
})
3534
})
3635

37-
it('should build a minting tx', async () => {
36+
it('builds a minting tx', async () => {
3837
const { maxMint } = await staker.getMint({
3938
delegatorAddress,
4039
validatorAddress
4140
})
41+
const amountToMint = parseEther('2')
4242

4343
assert(parseEther(maxMint) > 0n)
4444
assert(parseEther(maxMint) > amountToMint)
@@ -72,4 +72,71 @@ describe('EthereumStaker.buildMintTx', () => {
7272
})
7373
assert.equal(osEthBalance, amountToMint)
7474
})
75+
76+
it('allows minting max value', async () => {
77+
const { maxMint } = await staker.getMint({
78+
delegatorAddress,
79+
validatorAddress
80+
})
81+
82+
assert(parseEther(maxMint) > 0n)
83+
const amountToMint = parseEther(maxMint)
84+
85+
await mint({
86+
delegatorAddress,
87+
validatorAddress,
88+
amountToMint,
89+
publicClient,
90+
walletClient,
91+
staker
92+
})
93+
94+
const { maxMint: maxMintAfter } = await staker.getMint({
95+
delegatorAddress,
96+
validatorAddress
97+
})
98+
99+
// Take into account vault fees
100+
assert.closeTo(
101+
Number(parseEther(maxMintAfter)),
102+
Number(parseEther(maxMint) - amountToMint),
103+
Number(parseEther('0.001'))
104+
)
105+
106+
const osEthBalance = await publicClient.readContract({
107+
abi: erc20Abi,
108+
address: osEthTokenAddress,
109+
functionName: 'balanceOf',
110+
args: [delegatorAddress]
111+
})
112+
assert.equal(osEthBalance, amountToMint)
113+
})
114+
115+
it('does not allow minting more than max value', async () => {
116+
const { maxMint } = await staker.getMint({
117+
delegatorAddress,
118+
validatorAddress
119+
})
120+
121+
assert(parseEther(maxMint) > 0n)
122+
// Add 2 wei above max
123+
// 2 instead of 1 is to avoid conversion issues
124+
const amountToMint = parseEther(maxMint) + 2n
125+
126+
let error = undefined
127+
try {
128+
await mint({
129+
delegatorAddress,
130+
validatorAddress,
131+
amountToMint,
132+
publicClient,
133+
walletClient,
134+
staker
135+
})
136+
} catch (e) {
137+
error = e
138+
}
139+
140+
assert(error !== undefined)
141+
})
75142
})

packages/ethereum/test/buildStakeTx.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('EthereumStaker.buildStakeTx', () => {
2222
staker = setup.staker
2323
})
2424

25-
it('should build a staking tx', async () => {
25+
it('builds a staking tx', async () => {
2626
const balanceBefore = await publicClient.getBalance({
2727
address: delegatorAddress
2828
})
@@ -50,7 +50,7 @@ describe('EthereumStaker.buildStakeTx', () => {
5050
assert.equal(parseEther(stakeAfter), amountToStake)
5151
})
5252

53-
it('should build a staking tx with referrer', async () => {
53+
it('builds a staking tx with referrer', async () => {
5454
await stake({
5555
delegatorAddress,
5656
validatorAddress,

packages/ethereum/test/buildUnstakeTx.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('EthereumStaker.buildUnstakeTx', () => {
3232
})
3333
})
3434

35-
it('should build an unstaking tx', async () => {
35+
it('builds an unstaking tx', async () => {
3636
const { maxUnstake: maxUnstake, balance: assetsAfterStaking } = await staker.getStake({
3737
delegatorAddress,
3838
validatorAddress
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { EthereumStaker } from '@chorus-one/ethereum'
2+
import { Hex, parseEther, PublicClient, WalletClient } from 'viem'
3+
import { mint, prepareTests, stake } from './lib/utils'
4+
import { assert } from 'chai'
5+
6+
const amountToStake = parseEther('20')
7+
const amountToMint = parseEther('1')
8+
9+
describe('EthereumStaker.getMint', () => {
10+
let validatorAddress: Hex
11+
let delegatorAddress: Hex
12+
let walletClient: WalletClient
13+
let publicClient: PublicClient
14+
let staker: EthereumStaker
15+
16+
beforeEach(async () => {
17+
const setup = await prepareTests()
18+
validatorAddress = setup.validatorAddress
19+
delegatorAddress = setup.walletClient.account.address
20+
walletClient = setup.walletClient
21+
publicClient = setup.publicClient
22+
staker = setup.staker
23+
})
24+
25+
it('returns minted balance and maxMint', async () => {
26+
await stake({
27+
delegatorAddress,
28+
validatorAddress,
29+
amountToStake,
30+
publicClient,
31+
walletClient,
32+
staker
33+
})
34+
35+
await mint({
36+
delegatorAddress,
37+
validatorAddress,
38+
amountToMint,
39+
publicClient,
40+
walletClient,
41+
staker
42+
})
43+
44+
const { balance, maxMint } = await staker.getMint({
45+
delegatorAddress,
46+
validatorAddress
47+
})
48+
49+
assert(parseEther(maxMint) > parseEther('10'))
50+
assert(parseEther(maxMint) < amountToStake - amountToMint)
51+
assert.equal(parseEther(balance), amountToMint)
52+
})
53+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { EthereumStaker } from '@chorus-one/ethereum'
2+
import { Hex, PublicClient, WalletClient, formatEther, parseEther } from 'viem'
3+
import { assert } from 'chai'
4+
import { prepareTests, stake } from './lib/utils'
5+
6+
const amountToStake = parseEther('20')
7+
8+
describe('EthereumStaker.getMintHealth', () => {
9+
let delegatorAddress: Hex
10+
let validatorAddress: Hex
11+
let publicClient: PublicClient
12+
let walletClient: WalletClient
13+
let staker: EthereumStaker
14+
15+
beforeEach(async () => {
16+
const setup = await prepareTests()
17+
18+
delegatorAddress = setup.walletClient.account.address
19+
validatorAddress = setup.validatorAddress
20+
publicClient = setup.publicClient
21+
walletClient = setup.walletClient
22+
staker = setup.staker
23+
24+
await stake({
25+
delegatorAddress,
26+
validatorAddress,
27+
amountToStake,
28+
publicClient,
29+
walletClient,
30+
staker
31+
})
32+
})
33+
34+
it('returns health of mint', async () => {
35+
const { maxMint } = await staker.getMint({
36+
delegatorAddress,
37+
validatorAddress
38+
})
39+
40+
const amountToMintHealthy = parseEther(maxMint)
41+
42+
const { health: healthy } = await staker.getMintHealth({
43+
mintAmount: formatEther(amountToMintHealthy),
44+
stakeAmount: formatEther(amountToStake)
45+
})
46+
47+
const amountToMintRisky = parseEther(maxMint) + parseEther('1')
48+
49+
const { health: risky } = await staker.getMintHealth({
50+
mintAmount: formatEther(amountToMintRisky),
51+
stakeAmount: formatEther(amountToStake)
52+
})
53+
54+
assert.equal(healthy, 'healthy')
55+
assert.equal(risky, 'risky')
56+
})
57+
})
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { EthereumStaker } from '@chorus-one/ethereum'
2+
import { Hex, parseEther, PublicClient, WalletClient } from 'viem'
3+
import { prepareTests, stake } from './lib/utils'
4+
import { assert } from 'chai'
5+
6+
const amountToStake = parseEther('2')
7+
8+
describe('EthereumStaker.getStake', () => {
9+
let validatorAddress: Hex
10+
let delegatorAddress: Hex
11+
let walletClient: WalletClient
12+
let publicClient: PublicClient
13+
let staker: EthereumStaker
14+
15+
beforeEach(async () => {
16+
const setup = await prepareTests()
17+
validatorAddress = setup.validatorAddress
18+
delegatorAddress = setup.walletClient.account.address
19+
walletClient = setup.walletClient
20+
publicClient = setup.publicClient
21+
staker = setup.staker
22+
})
23+
24+
it('returns staked balance and maxUnstake', async () => {
25+
await stake({
26+
delegatorAddress,
27+
validatorAddress,
28+
amountToStake,
29+
publicClient,
30+
walletClient,
31+
staker
32+
})
33+
34+
const { balance: stakeAfter, maxUnstake } = await staker.getStake({
35+
delegatorAddress,
36+
validatorAddress
37+
})
38+
39+
// Take into account gas fees
40+
assert.equal(parseEther(stakeAfter), amountToStake)
41+
assert.equal(parseEther(maxUnstake), amountToStake)
42+
})
43+
})

packages/ethereum/test/getTxHistory.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('EthereumStaker.getTxHistory', () => {
1515
staker = setup.staker
1616
})
1717

18-
it('returns correct interaction history for given period of time for Chorus mainnet stakers', async () => {
18+
it('returns correct transaction history for given period of time', async () => {
1919
const txHistory = await staker.getTxHistory({
2020
validatorAddress,
2121
delegatorAddress

packages/ethereum/test/getUnstakeQueue.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('EthereumStaker.getUnstakeQueue', () => {
1616
staker = setup.staker
1717
})
1818

19-
it('should return the unstake queue', async () => {
19+
it('returns the unstake queue', async () => {
2020
const unstakeQueue = await staker.getUnstakeQueue({
2121
validatorAddress,
2222
delegatorAddress

packages/ethereum/test/getVault.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('EthereumStaker.getVault', () => {
1313
staker = setup.staker
1414
})
1515

16-
it('returns correct details and stake for Chorus One vault by default', async () => {
16+
it('returns vault details', async () => {
1717
const { vault } = await staker.getVault({
1818
validatorAddress
1919
})

0 commit comments

Comments
 (0)