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

Max mint formula has been changed #22

Merged
merged 2 commits into from
Oct 18, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ Maximum number of **shares** for minting
|------|------|-------------|---------|
| ltvPercent | `bigint` | **Require** | [getBaseData](#sdkostokengetbasedata) |
| stakedAssets | `bigint` | **Require** | [getStakeBalance](#sdkvaultgetstakebalance) |
| mintedShares | `bigint` | **Require** | [getPosition](#sdkostokengetposition) |
| mintedAssets | `bigint` | **Require** | [getPosition](#sdkostokengetposition) |

#### Returns:

Expand All @@ -602,7 +602,7 @@ type Output = bigint
```ts
await sdk.osToken.getMaxMint({
ltvPercent: 0n,
mintedShares: 0n,
mintedAssets: 0n,
stakedAssets: 0n,
})
```
Expand Down
19 changes: 10 additions & 9 deletions src/methods/osToken/requests/getMaxMint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('getMaxMint', () => {
it('should return 0n if ltvPercent is <= 0', async () => {
const result = await getMaxMint({
ltvPercent: 0n,
mintedShares: 100n,
mintedAssets: 100n,
stakedAssets: 100n,
contracts: mockContracts,
})
Expand All @@ -29,7 +29,7 @@ describe('getMaxMint', () => {
it('should return 0n if stakedAssets is <= 0', async () => {
const result = await getMaxMint({
ltvPercent: 5000n,
mintedShares: 100n,
mintedAssets: 100n,
stakedAssets: 0n,
contracts: mockContracts,
})
Expand All @@ -39,16 +39,17 @@ describe('getMaxMint', () => {

it('should return max mint shares based on calculations', async () => {
mockContracts.tokens.mintToken.avgRewardPerSecond.mockResolvedValue(2n)
mockContracts.tokens.mintToken.convertToShares.mockResolvedValue(1500n)

const result = await getMaxMint({
ltvPercent: 5000n,
mintedShares: 1000n,
stakedAssets: 2000n,
await getMaxMint({
ltvPercent: 9000n,
mintedAssets: 2001547526745735889n,
stakedAssets: 15001277163464317262n,
contracts: mockContracts,
})

expect(result).toEqual(500n)
const result = mockContracts.tokens.mintToken.convertToShares.mock.calls[0][0]

expect(result).toEqual(11499601920372052438n)
})

it('should return 0n if max mint shares is less than or equal to minted shares', async () => {
Expand All @@ -57,7 +58,7 @@ describe('getMaxMint', () => {

const result = await getMaxMint({
ltvPercent: 5000n,
mintedShares: 1000n,
mintedAssets: 1000n,
stakedAssets: 2000n,
contracts: mockContracts,
})
Expand Down
28 changes: 11 additions & 17 deletions src/methods/osToken/requests/getMaxMint.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
import { BigDecimal, validateArgs } from '../../../utils'
import { constants, validateArgs } from '../../../utils'


type GetMaxMintInput = {
ltvPercent: bigint
mintedShares: bigint
mintedAssets: bigint
stakedAssets: bigint
contracts: StakeWise.Contracts
}

const getMaxMint = async (values: GetMaxMintInput) => {
const { contracts, ltvPercent, mintedShares, stakedAssets } = values
const { contracts, ltvPercent, mintedAssets, stakedAssets } = values

validateArgs.bigint({ ltvPercent, mintedShares, stakedAssets })
validateArgs.bigint({ ltvPercent, mintedAssets, stakedAssets })

if (ltvPercent <= 0 || stakedAssets <= 0) {
return 0n
}

const rewardPerSecond = await contracts.tokens.mintToken.avgRewardPerSecond()
const avgRewardPerHour = rewardPerSecond * 60n * 60n
const avgRewardPerSecond = await contracts.tokens.mintToken.avgRewardPerSecond()

const result = new BigDecimal(stakedAssets)
.multiply(ltvPercent)
.divide(10_000)
.minus(avgRewardPerHour)
.decimals(0)
.toString()
const maxMintedAssets = stakedAssets * ltvPercent / 10_000n
const maxMintedAssetsHourReward = (maxMintedAssets * avgRewardPerSecond * 3600n) / constants.blockchain.amount1
const canMintAssets = maxMintedAssets - maxMintedAssetsHourReward - mintedAssets

let maxMintShares = await contracts.tokens.mintToken.convertToShares(result)
if (canMintAssets > 0) {
const maxMintShares2 = await contracts.tokens.mintToken.convertToShares(canMintAssets)

if (maxMintShares > mintedShares) {
maxMintShares = maxMintShares - mintedShares

return maxMintShares
return maxMintShares2
}

return 0n
Expand Down
Loading