Skip to content

Commit

Permalink
Fix eos rex convert
Browse files Browse the repository at this point in the history
  • Loading branch information
apporc committed Jul 6, 2024
1 parent 96bf165 commit 71ab787
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
56 changes: 30 additions & 26 deletions src/pages/earn/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,55 @@
}
)
const convertEosToRex = (eos: number) => {
const pool = $stateREX
const S0 = pool!.total_lendable.value
const R0 = pool!.total_rex.value
const S1 = S0 + eos
const R1 = (S1 * R0) / S0
return Asset.from(R1 - R0, $currentAccount!.rex_info!.rex_balance.symbol)
}
const convertRexToEos = (rex: number) => {
const pool = $stateREX
const S0 = pool!.total_lendable.value
const R0 = pool!.total_rex.value
const R1 = R0 + rex
const S1 = (S0 * R1) / R0
return Asset.from(S1 - S0, $systemToken!.symbol)
}
const rexInfo: Readable<REXInfo> = derived(
[currentAccount, stateREX, systemToken],
([$currentAccount, $stateREX, $systemToken]) => {
let defaultZero = Asset.from(0, $systemToken!.symbol)
let total = defaultZero
let savings = defaultZero
let matured = defaultZero
let rexPrice = 0
const fiveYearsFromNow = new Date().getTime() + 1000 * 60 * 60 * 24 * 365 * 5
if ($currentAccount && $currentAccount.rex_info && $stateREX && $stateREX.value) {
if ($stateREX.value === 0.0001) {
rexPrice = $stateREX.total_lendable.value / $stateREX.total_rex.value
} else {
rexPrice = $stateREX.value
}
total = Asset.from(
$currentAccount.rex_info.rex_balance.value * rexPrice,
$systemToken!.symbol
)
matured = Asset.from(
total = convertRexToEos($currentAccount.rex_info.rex_balance.value)
matured = convertRexToEos(
Asset.fromUnits(
$currentAccount.rex_info.matured_rex,
$currentAccount.rex_info.rex_balance.symbol
).value * rexPrice,
$systemToken!.symbol
).value
)
let savingsBucket = $currentAccount.rex_info.rex_maturities.find(
(maturity) => +new Date(maturity.first!.toString()) > +fiveYearsFromNow
)
if (savingsBucket) {
savings = Asset.from(
savings = convertRexToEos(
Asset.fromUnits(
savingsBucket.second!,
$currentAccount.rex_info.rex_balance.symbol
).value * rexPrice,
$systemToken!.symbol
).value
)
}
}
return {total, savings, matured, price: rexPrice}
return {total, savings, matured}
}
)
Expand Down Expand Up @@ -195,33 +201,31 @@
}
function getUnstakeAction() {
let rexNumber = Number(selectedAmount) / $rexInfo.price
let rexAsset = Asset.from(rexNumber, $currentAccount!.rex_info!.rex_balance.symbol)
let rexAmount = convertEosToRex(Number(selectedAmount))
return [
{
authorization: [$activeSession!.auth],
account: 'eosio',
name: 'mvfrsavings',
data: REXWithdraw.from({
owner: $activeSession!.auth.actor,
amount: rexAsset,
amount: rexAmount,
}),
},
]
}
function getClaimAction() {
let rexNumber = Number(selectedAmount) / $rexInfo.price
let rexAsset = Asset.from(rexNumber, $currentAccount!.rex_info!.rex_balance.symbol)
let finalAmount = Asset.from(rexAsset.value * $rexInfo.price, $systemToken!.symbol)
let rexAmount = convertEosToRex(Number(selectedAmount))
let eosAmount = convertRexToEos(rexAmount.value)
return [
{
authorization: [$activeSession!.auth],
account: 'eosio',
name: 'sellrex',
data: REXSELLREX.from({
from: $activeSession!.auth.actor,
rex: rexAsset,
rex: rexAmount,
}),
},
{
Expand All @@ -230,7 +234,7 @@
name: 'withdraw',
data: REXWithdraw.from({
owner: $activeSession!.auth.actor,
amount: finalAmount,
amount: eosAmount,
}),
},
]
Expand Down
1 change: 0 additions & 1 deletion src/pages/earn/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ export interface REXInfo {
total: Asset
savings: Asset
matured: Asset
price: number
}

0 comments on commit 71ab787

Please sign in to comment.