Skip to content

Commit

Permalink
Merge pull request #150 from greymass/fixing-ram-buying-calculation
Browse files Browse the repository at this point in the history
Fixing ram buying calculation and fixing type errors
  • Loading branch information
dafuga authored Oct 2, 2024
2 parents 04950b3 + 4dcc4fa commit 39220a7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/routes/[network]/(account)/ram/buy/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
return [
{
href: `/${network}/ram/buy/tokens`,
text: String(data.network.chain.systemToken.symbol.code)
text: String(data.network.chain.systemToken?.symbol.code || '')
},
{ href: `/${network}/ram/buy/bytes`, text: 'Bytes' }
];
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[network]/(account)/ram/buy/bytes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
{#if context.account}
{context.account.balance?.liquid}
{:else}
0.0000 {data.network.chain.systemToken.symbol.code}
0.0000 {data.network.chain.systemToken?.symbol.code || ''}
{/if}
</p>
</Stack>
Expand Down
25 changes: 18 additions & 7 deletions src/routes/[network]/(account)/ram/buy/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@ export class BuyRAMState {

public bytesValue: Asset = $derived(
this.format === 'asset' || !this.bytes || !this.pricePerKB.value
? this.tokens
: Asset.from((this.bytes * this.pricePerKB.value) / 1000, this.chain.systemToken.symbol)
? Asset.from(this.tokens.value * 0.995, this.chain.systemToken?.symbol || '0,UNKNOWN')
: Asset.from(
(this.bytes * this.pricePerKB.value) / 1000,
this.chain.systemToken?.symbol || '0,UNKNOWN'
)
);

public bytesToBuy: number = $derived(
this.bytes || !this.bytesValue.value || !this.pricePerKB.value
this.format === 'units' || !this.bytesValue.value || !this.pricePerKB.value
? this.bytes || 0
: Number(((this.bytesValue.value / this.pricePerKB.value) * 1000).toFixed(0))
);

public fee: Asset = $derived(
Asset.from(this.bytesValue.value * 0.005, this.chain.systemToken.symbol)
Asset.from(
(this.format === 'asset' ? this.tokens.value : this.bytesValue.value) * 0.005,
this.chain.systemToken?.symbol || '0,UNKNOWN'
)
);

public bytesCost: Asset = $derived(
Asset.fromUnits(this.bytesValue.units.adding(this.fee.units), this.chain.systemToken.symbol)
this.format === 'asset'
? this.tokens
: Asset.fromUnits(
this.bytesValue.units.adding(this.fee.units),
this.chain.systemToken?.symbol || '0,UNKNOWN'
)
);

public valid: boolean = $derived(
Expand All @@ -61,15 +72,15 @@ export class BuyRAMState {

reset() {
this.bytes = undefined;
this.tokens = Asset.fromUnits(0, this.chain.systemToken.symbol);
this.tokens = Asset.fromUnits(0, this.chain.systemToken?.symbol || '0,UNKNOWN');
}

toJSON() {
if (this.format === 'asset') {
return Serializer.objectify({
payer: this.payer,
receiver: this.receiver,
quant: this.bytesValue
quant: this.bytesCost
});
} else {
return Serializer.objectify({
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[network]/(account)/ram/buy/tokens/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
{#if context.account}
{context.account.balance?.liquid}
{:else}
0.0000 {data.network.chain.systemToken.symbol.code}
0.0000 {data.network.chain.systemToken?.symbol.code || ''}
{/if}
</p>
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<div class="grid grid-cols-2 gap-2">
<span>Price for 1000 bytes:</span>
<span>{sellRamState.pricePerKB} / KB</span>
<span>Estimated RAM to be sold:</span>
<span>RAM to be sold:</span>
<span>{sellRamState.bytesToSell} Bytes</span>
<span>RAM Value:</span>
<span>{sellRamState.tokens}</span>
Expand Down

0 comments on commit 39220a7

Please sign in to comment.