Skip to content

Commit

Permalink
Remove usage of exclamation in staking
Browse files Browse the repository at this point in the history
  • Loading branch information
apporc committed Sep 10, 2024
1 parent bdfa470 commit e615701
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export class StakeState {
public txid: string = $state('');

public stakable: Asset = $derived(
this.account && this.network ? getStakableBalance(this.network!, this.account) : defaultQuantity
this.account && this.network ? getStakableBalance(this.network, this.account) : defaultQuantity
);
public apy: string = $derived(getAPY(this.network));
public apy: string = $derived(this.network ? getAPY(this.network) : '0');
public estimateYield: Asset = $derived(
this.network
? Asset.from(
(this.assetValue.value * parseFloat(this.apy)) / 100,
this.network!.chain.systemToken!.symbol
this.network.chain.systemToken!.symbol
)
: defaultQuantity
);
Expand All @@ -42,7 +42,7 @@ export class StakeState {
}

get zeroValue() {
return Asset.from(0, this.network!.chain.systemToken!.symbol);
return this.network ? Asset.from(0, this.network.chain.systemToken!.symbol) : defaultQuantity;
}

sync(network: NetworkState, account: AccountState, wharf: WharfState) {
Expand All @@ -61,33 +61,39 @@ export class StakeState {
this.txid = '';
}

if (this.assetValue.symbol !== this.network!.chain.systemToken!.symbol) {
if (this.network && this.assetValue.symbol !== this.network.chain.systemToken!.symbol) {
this.input?.set(this.zeroValue);
}
if (wharf !== this.wharf) {
this.wharf = wharf;
}

this.minValue = Asset.fromUnits(1, this.network!.chain.systemToken!.symbol).value;
this.maxValue = this.account ? getStakableBalance(this.network!, this.account).value : 0;
if (this.network) {
this.minValue = Asset.fromUnits(1, this.network.chain.systemToken!.symbol).value;
this.maxValue = this.account ? getStakableBalance(this.network, this.account).value : 0;
}
}

setMaxValue() {
this.input?.set(this.stakable);
}

async transact() {
const deposit = this.network!.contracts.system.action('deposit', {
owner: this.account!.name!,
amount: this.assetValue!
});
const buyrex = this.network!.contracts.system.action('buyrex', {
from: this.account!.name!,
amount: this.assetValue!
});

try {
const result = await this.wharf!.transact({
if (!this.network || !this.account || !this.account.name || !this.wharf || !this.assetValue) {
throw new Error("Can't sign, data not ready");
}

const deposit = this.network.contracts.system.action('deposit', {
owner: this.account.name,
amount: this.assetValue
});
const buyrex = this.network.contracts.system.action('buyrex', {
from: this.account.name,
amount: this.assetValue
});

const result = await this.wharf.transact({
actions: [deposit, buyrex]
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ export class UnstakeState {
public txid: string = $state('');

public unstakable: Asset = $derived(
this.account ? getUnstakableBalance(this.network!, this.account) : defaultQuantity
this.account && this.network
? getUnstakableBalance(this.network, this.account)
: defaultQuantity
);

constructor(network: NetworkState) {
this.network = network;
}

get zeroValue() {
return Asset.from(0, this.network!.chain.systemToken!.symbol);
return this.network ? Asset.from(0, this.network.chain.systemToken!.symbol) : defaultQuantity;
}

sync(network: NetworkState, account: AccountState, wharf: WharfState) {
Expand All @@ -52,29 +54,34 @@ export class UnstakeState {
this.txid = '';
}

if (this.assetValue.symbol !== this.network!.chain.systemToken!.symbol) {
if (this.network && this.assetValue.symbol !== this.network.chain.systemToken!.symbol) {
this.input?.set(this.zeroValue);
}
if (wharf !== this.wharf) {
this.wharf = wharf;
}

this.minValue = Asset.fromUnits(1, this.network!.chain.systemToken!.symbol).value;
this.maxValue = this.account ? getUnstakableBalance(this.network!, this.account).value : 0;
if (this.network) {
this.minValue = Asset.fromUnits(1, this.network.chain.systemToken!.symbol).value;
this.maxValue = this.account ? getUnstakableBalance(this.network, this.account).value : 0;
}
}

setMaxValue() {
this.input?.set(this.unstakable);
}

async transact() {
const mvfrsavings = this.network!.contracts.system.action('mvfrsavings', {
owner: this.account!.name!,
rex: this.network!.tokenToRex(this.assetValue!)
});

try {
const result = await this.wharf!.transact({
if (!this.network || !this.account || !this.account.name || !this.assetValue || !this.wharf) {
throw new Error("Can't sign, data not ready");
}
const mvfrsavings = this.network.contracts.system.action('mvfrsavings', {
owner: this.account.name,
rex: this.network.tokenToRex(this.assetValue)
});

const result = await this.wharf.transact({
actions: [mvfrsavings]
});

Expand Down
41 changes: 20 additions & 21 deletions src/routes/[network]/(account)/(staking)/staking/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export function getStakableBalance(network: NetworkState, account: AccountState)

export function getStakedBalance(network: NetworkState, account: AccountState): Asset {
let staked = Asset.fromUnits(0, network.chain.systemToken!.symbol);
if (account && account.loaded) {
const rexInfo = account.account!.data.rex_info;
if (account && account.loaded && account.account) {
const rexInfo = account.account.data.rex_info;
if (rexInfo) {
staked = network.rexToToken(rexInfo.rex_balance);
}
Expand All @@ -41,10 +41,10 @@ export function getClaimableBalance(
unstaking: Array<UnstakingRecord>
): Asset {
// withdrawable(rex_fund) + claimable
let claimable: Asset = Asset.from(0, network!.chain.systemToken!.symbol);
let claimable: Asset = Asset.from(0, network.chain.systemToken!.symbol);

if (account && account.loaded) {
const rexInfo = account.account!.data.rex_info;
if (account && account.loaded && account.account) {
const rexInfo = account.account.data.rex_info;
if (rexInfo) {
let matured = Int64.from(0);
const sum: Int64 = unstaking
Expand All @@ -55,7 +55,7 @@ export function getClaimableBalance(
}

if (matured.gt(Int64.from(0))) {
claimable = Asset.fromUnits(matured, network!.chain.systemToken!.symbol);
claimable = Asset.fromUnits(matured, network.chain.systemToken!.symbol);
}
}
}
Expand All @@ -74,8 +74,8 @@ export function getUnstakingBalances(
): Array<UnstakingRecord> {
// matured_rex + claimable buckets
let records: Array<UnstakingRecord> = [];
if (account && account.loaded) {
const rexInfo = account.account!.data.rex_info;
if (account && account.loaded && account.account) {
const rexInfo = account.account.data.rex_info;
if (rexInfo) {
if (rexInfo.matured_rex && rexInfo.matured_rex.gt(Int64.from(0))) {
// add matured into balances
Expand All @@ -92,15 +92,17 @@ export function getUnstakingBalances(
const fiveYearsFromNow = new Date().getTime() + 1000 * 60 * 60 * 24 * 365 * 5;
const now = new Date();
for (const maturity of rexInfo.rex_maturities) {
const date = new Date(maturity.first!.toString());
records.push({
date,
balance: network.rexToToken(
Asset.fromUnits(maturity.second!, rexInfo.rex_balance.symbol)
),
claimable: +date < +now,
savings: +date > +fiveYearsFromNow
});
if (maturity.first && maturity.second) {
const date = new Date(maturity.first.toString());
records.push({
date,
balance: network.rexToToken(
Asset.fromUnits(maturity.second, rexInfo.rex_balance.symbol)
),
claimable: +date < +now,
savings: +date > +fiveYearsFromNow
});
}
}
}
}
Expand All @@ -112,10 +114,7 @@ export function getUnstakableBalance(network: NetworkState, account: AccountStat
return savings ? savings.balance : Asset.from(0, network.chain.systemToken!.symbol);
}

export function getAPY(network: NetworkState | undefined): string {
if (!network) {
return '0';
}
export function getAPY(network: NetworkState): string {
const annualReward = 31250000;
const totalStaked = Number(network.rexstate!.total_lendable.value);
return ((annualReward / totalStaked) * 100).toFixed(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class WithdrawState {
);
public withdrawable: Asset = $derived(
this.account && this.network
? getWithdrawableBalance(this.network!, this.account)
? getWithdrawableBalance(this.network, this.account)
: defaultQuantity
);
public total: Asset = $derived(
Expand Down Expand Up @@ -67,24 +67,30 @@ export class WithdrawState {
}

async transact() {
const actions = [];
if (this.claimable) {
actions.push(
this.network!.contracts.system.action('sellrex', {
from: this.account!.name!,
rex: this.network!.tokenToRex(this.claimable)
})
);
}
actions.push(
this.network!.contracts.system.action('withdraw', {
owner: this.account!.name!,
amount: this.withdrawable!
})
);

try {
const result = await this.wharf!.transact({
if (!this.network || !this.account || !this.account.name || !this.wharf) {
throw new Error("Can't sign, data not ready");
}

const actions = [];
if (this.claimable) {
actions.push(
this.network.contracts.system.action('sellrex', {
from: this.account.name,
rex: this.network.tokenToRex(this.claimable)
})
);
}
if (this.withdrawable) {
actions.push(
this.network.contracts.system.action('withdraw', {
owner: this.account.name,
amount: this.withdrawable
})
);
}

const result = await this.wharf.transact({
actions
});

Expand Down

0 comments on commit e615701

Please sign in to comment.