From e615701a9aad2ec2390050dedf037d4c91313a54 Mon Sep 17 00:00:00 2001 From: apporc Date: Tue, 10 Sep 2024 18:29:08 +0800 Subject: [PATCH] Remove usage of exclamation in staking --- .../(staking)/staking/stake/state.svelte.ts | 40 ++++++++++-------- .../(staking)/staking/unstake/state.svelte.ts | 29 ++++++++----- .../(account)/(staking)/staking/utils.ts | 41 +++++++++--------- .../staking/withdraw/state.svelte.ts | 42 +++++++++++-------- 4 files changed, 85 insertions(+), 67 deletions(-) diff --git a/src/routes/[network]/(account)/(staking)/staking/stake/state.svelte.ts b/src/routes/[network]/(account)/(staking)/staking/stake/state.svelte.ts index 22f527bb0..8536fe673 100644 --- a/src/routes/[network]/(account)/(staking)/staking/stake/state.svelte.ts +++ b/src/routes/[network]/(account)/(staking)/staking/stake/state.svelte.ts @@ -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 ); @@ -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) { @@ -61,15 +61,17 @@ 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() { @@ -77,17 +79,21 @@ export class StakeState { } 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] }); diff --git a/src/routes/[network]/(account)/(staking)/staking/unstake/state.svelte.ts b/src/routes/[network]/(account)/(staking)/staking/unstake/state.svelte.ts index 83d26d36e..b31105805 100644 --- a/src/routes/[network]/(account)/(staking)/staking/unstake/state.svelte.ts +++ b/src/routes/[network]/(account)/(staking)/staking/unstake/state.svelte.ts @@ -25,7 +25,9 @@ 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) { @@ -33,7 +35,7 @@ export class UnstakeState { } 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) { @@ -52,15 +54,17 @@ 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() { @@ -68,13 +72,16 @@ export class UnstakeState { } 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] }); diff --git a/src/routes/[network]/(account)/(staking)/staking/utils.ts b/src/routes/[network]/(account)/(staking)/staking/utils.ts index f3b0fa01e..ddaa2f156 100644 --- a/src/routes/[network]/(account)/(staking)/staking/utils.ts +++ b/src/routes/[network]/(account)/(staking)/staking/utils.ts @@ -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); } @@ -41,10 +41,10 @@ export function getClaimableBalance( unstaking: Array ): 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 @@ -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); } } } @@ -74,8 +74,8 @@ export function getUnstakingBalances( ): Array { // matured_rex + claimable buckets let records: Array = []; - 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 @@ -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 + }); + } } } } @@ -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); diff --git a/src/routes/[network]/(account)/(staking)/staking/withdraw/state.svelte.ts b/src/routes/[network]/(account)/(staking)/staking/withdraw/state.svelte.ts index 34d942c84..1a57f2999 100644 --- a/src/routes/[network]/(account)/(staking)/staking/withdraw/state.svelte.ts +++ b/src/routes/[network]/(account)/(staking)/staking/withdraw/state.svelte.ts @@ -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( @@ -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 });