Skip to content
Open
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 src/app/bridge-btc/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const BridgeBtc = () => {

const selected = selectedAccount?.address;
if (!selected) {
console.error("SelectedAccount undefined");
console.warn("SelectedAccount undefined");
return;
}
const entries = await api.query.tokens.accounts.entries(selected);
Expand Down Expand Up @@ -301,7 +301,7 @@ const BridgeBtc = () => {
return;
}
setSelectedToken(tokenObj);
console.log("tokenObj:", tokenObj);
console.log("selectedToken and tokenObj:", tokenObj);
//connectWallet();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering what's the reason to keep it commented. THough it's not a part of pr.

};
run();
Expand Down
33 changes: 20 additions & 13 deletions src/app/dex/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import Contract, { errorHandler } from "@/services/api";
import GGxNetwork from "@/services/api/ggx";
import GgxNetworkMock from "@/services/api/mock";
import GGXWallet from "@/services/ggx";
import { count_decimals, fixDP, formatPrice } from "@/services/utils";
import {
big,
bigZero,
count_decimals,
fixDP,
formatPrice,
} from "@/services/utils";
import { MAX_DP } from "@/settings";
import TokenDecimals from "@/tokenDecimalsConverter";
import type { Amount, DetailedOrder } from "@/types";
Expand Down Expand Up @@ -194,6 +200,7 @@ export default function Dex({ params, searchParams }: PageProps) {
toast.warn(mesg);
return;
}
console.log("-------== makeOrder");
contract
.makeOrder(
pair,
Expand Down Expand Up @@ -269,13 +276,13 @@ export default function Dex({ params, searchParams }: PageProps) {
const reverseRate = rate > 0 ? 1 / rate : 0;
const sellPriceRate = reverseRate * (sell?.price ?? 0);

const buyPrice = amountConverter.BNToFloat(buyAmount) * sellPriceRate;
const sellPrice = amountConverter.BNToFloat(sellAmount) * buyPriceRate;
const buyPrice = big(buyAmount.toString()).multipliedBy(sellPriceRate);
const sellPrice = big(sellAmount.toString()).multipliedBy(buyPriceRate);

const comparedToMarket =
!isTokenNotSelected && !isAmountZero && buyPrice > 0
? ((sellPrice - buyPrice) * 100) / buyPrice
: 0;
!isTokenNotSelected && !isAmountZero && buyPrice.gt(0)
? sellPrice.minus(buyPrice).multipliedBy(100).dividedBy(buyPrice)
: bigZero;

return (
<div className="text-GGx-gray flex flex-col w-full items-center">
Expand Down Expand Up @@ -372,12 +379,12 @@ export default function Dex({ params, searchParams }: PageProps) {
{rate > 0 && !isTokenNotSelected && !isTokenSame ? (
<div className="flex flex-col text-GGx-light">
<p className="font-semibold">
1 {sell.name} = {formatPrice(rate)} {buy.name} ≈ $
{formatPrice(buyPriceRate)}
1 {sell.name} = {formatPrice(big(rate))} {buy.name} ≈ $
{formatPrice(big(buyPriceRate))}
</p>
<p className="text-base text-right text-GGx-gray">
1 {buy.name} = {formatPrice(reverseRate)} {sell.name} ≈ $
{formatPrice(sellPriceRate)}
1 {buy.name} = {formatPrice(big(reverseRate))} {sell.name}{" "}
≈ ${formatPrice(big(sellPriceRate))}
</p>
</div>
) : (
Expand All @@ -389,14 +396,14 @@ export default function Dex({ params, searchParams }: PageProps) {
<p className="font-semibold">COMPARED TO CEX:</p>
<p
className={`${
comparedToMarket < 0
comparedToMarket.lt(bigZero)
? "text-GGx-red"
: comparedToMarket > 0
: comparedToMarket.gt(bigZero)
? "text-GGx-green"
: ""
}`}
>
{formatPrice(Math.abs(comparedToMarket))}%
{formatPrice(comparedToMarket.abs())}%
</p>
</div>

Expand Down
12 changes: 8 additions & 4 deletions src/app/transfer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { toast } from "react-toastify";

import { Button } from "@/components/common/button";
import Ruler from "@/components/common/ruler";
import { formatter, strFloatToBN } from "@/services/utils";
import { bigZero, formatter, fromBig, strFloatToBN } from "@/services/utils";
import TokenDecimals from "@/tokenDecimalsConverter";
import Loading from "./loading";

Expand Down Expand Up @@ -323,10 +323,14 @@ export default function Transfer() {

const walletIsNotInitialized = !account?.address || !client;
const isGGxWalletNotConnected = modalGGxAccount === undefined;

const total = tokens.reduce((acc, token) => {
const balance = new TokenDecimals(token.decimals).BNToFloat(token.balance);
return acc + balance * (prices.get(token.symbol) ?? 0);
}, 0);
const balcBn = token.balance;
const balance = fromBig(balcBn, token.decimals);
const priceNum = prices.get(token.symbol) ?? 0;

return acc.plus(balance.multipliedBy(priceNum));
}, bigZero);

let price = 0;
if (selectedToken) {
Expand Down
31 changes: 16 additions & 15 deletions src/app/wallet/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ import CexService from "@/services/cex";
import GGXWallet, { type Account } from "@/services/ggx";
import {
BNtoDisplay,
bigZero,
bn,
checkBnStr,
count_decimals,
fixDP,
formatter,
fromBig,
numFloatToBN,
strFloatToBN,
} from "@/services/utils";
import { MAX_DP, PRICE_DP } from "@/settings";
import TokenDecimals from "@/tokenDecimalsConverter";
import type { Amount, Token, TokenId } from "@/types";
import { BN, BN_TEN, BN_ZERO } from "@polkadot/util";
import type BigNumber from "bignumber.js";
import { type ChangeEvent, Suspense, useEffect, useRef, useState } from "react";
import { toast } from "react-toastify";
import Loading from "./loading";
Expand Down Expand Up @@ -165,26 +167,25 @@ export default function Wallet({ params, searchParams }: PageProps) {
const filteredTokens = tokens.filter((token) => filter(token));
const isTokenNotSelected = selectedToken === undefined;

const totalOnChain = tokens.reduce<number>((total, token) => {
const balance = new TokenDecimals(token.decimals).BNToFloat(
chainBalances.get(token.id) ?? BN_ZERO,
);
const price = tokenPrices.get(token.id) ?? 0;
return total + balance * price;
}, 0);
const totalOnChain = tokens.reduce<BigNumber>((total, token) => {
const balcBn = chainBalances.get(token.id) ?? BN_ZERO;
const balance = fromBig(balcBn, token.decimals);
const priceNum = tokenPrices.get(token.id) ?? 0;

return total.plus(balance.multipliedBy(priceNum));
}, bigZero);

const total = dexOwnedTokens.reduce<number>((total, tokenId) => {
const totalDexOwned = dexOwnedTokens.reduce<BigNumber>((total, tokenId) => {
const token = tokenMap.get(tokenId);
if (token === undefined) {
return total;
}

const balance = new TokenDecimals(token.decimals).BNToFloat(
dexBalances.get(tokenId) ?? BN_ZERO,
);
const price = tokenPrices.get(tokenId) ?? 0;
const balcBn = dexBalances.get(tokenId) ?? BN_ZERO;
const balance = fromBig(balcBn, token.decimals);
const priceNum = tokenPrices.get(tokenId) ?? 0;

return total + balance * price;
return total.plus(balance.multipliedBy(priceNum));
}, totalOnChain);

const omModalSubmit = () => {
Expand Down Expand Up @@ -313,7 +314,7 @@ export default function Wallet({ params, searchParams }: PageProps) {
<div className="w-full h-full flex flex-col">
<div className="flex w-full justify-between items-center">
<h1 className="text-xl md:text-3xl break-words w-[40%] text-GGx-yellow font-telegraf">
{formatter().format(total)}
{formatter().format(totalDexOwned.toString())}
</h1>
<div className="flex xl:flex-row flex-col gap-5">
<Button
Expand Down
6 changes: 3 additions & 3 deletions src/components/dex/orderBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OrderUtils } from "@/order";
import type Pair from "@/pair";
import type Contract from "@/services/api";
import { errorHandler } from "@/services/api";
import { formatPrice } from "@/services/utils";
import { big, formatPrice } from "@/services/utils";
import TokenDecimals from "@/tokenDecimalsConverter";
import type { Amount, DetailedOrder, Token } from "@/types";
import { BN_ZERO } from "@polkadot/util";
Expand Down Expand Up @@ -179,7 +179,7 @@ export default function OrderBook({
className="relative w-full text-GGx-red"
>
<td className="text-left font-medium text-GGx-red">
{formatPrice(orderPrice)}
{formatPrice(big(orderPrice))}
</td>
<td className="text-left">
<span className="text-GGx-light font-medium bg-GGx-red/50 rounded-[4px] px-[6px]">
Expand Down Expand Up @@ -243,7 +243,7 @@ export default function OrderBook({
>
<td className="relative text-left font-medium text-GGx-green">
{selected && <p className="absolute h-full left-0">↠</p>}
<p className="pl-3">{formatPrice(orderPrice)}</p>
<p className="pl-3">{formatPrice(big(orderPrice))}</p>
</td>
<td className="text-left">
<span className="text-GGx-light font-medium bg-GGx-green/50 rounded-[4px] px-[6px]">
Expand Down
40 changes: 24 additions & 16 deletions src/components/tokenList.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { formatPrice } from "@/services/utils";
import { big, bnToBig, formatPrice } from "@/services/utils";
import TokenDecimals from "@/tokenDecimalsConverter";
import type { Amount, Token } from "@/types";
import { BN_ZERO } from "@polkadot/util";
import type BigNumber from "bignumber.js";
import Image from "next/image";
import Spinner from "./common/spinner";

Expand Down Expand Up @@ -95,24 +96,30 @@ export default function TokenList({
</td>
<td>
<Balance
amountConverter={amountConverter}
symbol={token.symbol}
balance={token.balance}
balance={bnToBig(token.balance)}
decimal={token.decimals}
estimatedPrice={token.estimatedPrice}
toDisplay={amountConverter.BNtoDisplay(
token.balance,
token.symbol,
)}
/>
</td>
{onChain && (
<td>
<Balance
amountConverter={amountConverter}
symbol={token.symbol}
balance={token.onChainBalance ?? BN_ZERO}
balance={bnToBig(token.onChainBalance)}
decimal={token.decimals}
estimatedPrice={token.estimatedPrice}
toDisplay={amountConverter.BNtoDisplay(
token.onChainBalance ?? BN_ZERO,
token.symbol,
)}
/>
</td>
)}
<td data-testid={`price-${token.symbol}`}>
{formatPrice(token.estimatedPrice)}
{formatPrice(big(token.estimatedPrice))}
</td>
</tr>
);
Expand All @@ -123,25 +130,26 @@ export default function TokenList({
}

interface BalanceProperties {
balance: Amount;
balance: BigNumber;
decimal: number;
estimatedPrice: number;
symbol: string;
amountConverter: TokenDecimals;
toDisplay: string;
}

function Balance({
balance,
decimal,
estimatedPrice,
symbol,
amountConverter,
toDisplay,
}: BalanceProperties) {
const estimatedPriceWithPrecision =
amountConverter.BNToFloat(balance) * estimatedPrice;
const estimatedPriceWithPrecision = balance
.shiftedBy(-1 * decimal)
.multipliedBy(estimatedPrice);

return (
<div className="text-[18px] font-medium text-left break-words">
<p className="mt-1">
{amountConverter.BNtoDisplay(balance, symbol)}
{toDisplay}
<sup className="ml-1 font- text-[10px]">
({formatPrice(estimatedPriceWithPrecision)})
</sup>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/augment-api-consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ declare module '@polkadot/api-base/types/consts' {
};
dex: {
palletId: FrameSupportPalletId & AugmentedConst<ApiType>;
unsignedPriority: u64 & AugmentedConst<ApiType>;
/**
* Generic const
**/
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/augment-api-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,18 @@ declare module '@polkadot/api-base/types/errors' {
dex: {
AssetIdNotInTokenIndex: AugmentedError<ApiType>;
AssetIdNotInTokenInfoes: AugmentedError<ApiType>;
DivOverflow: AugmentedError<ApiType>;
ExpirationMustBeInFuture: AugmentedError<ApiType>;
InsufficientBalance: AugmentedError<ApiType>;
InvalidOrderIndex: AugmentedError<ApiType>;
MulOverflow: AugmentedError<ApiType>;
NotEnoughBalance: AugmentedError<ApiType>;
NotOwner: AugmentedError<ApiType>;
OffchainUnsignedTxError: AugmentedError<ApiType>;
OrderIndexOverflow: AugmentedError<ApiType>;
PairAssetIdMustNotEqual: AugmentedError<ApiType>;
PairOrderNotFound: AugmentedError<ApiType>;
PriceDoNotMatchOfferedRequestedAmount: AugmentedError<ApiType>;
TokenBalanceOverflow: AugmentedError<ApiType>;
UserAssetNotExist: AugmentedError<ApiType>;
WithdrawBalanceMustKeepOrderSellAmount: AugmentedError<ApiType>;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/augment-api-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ declare module '@polkadot/api-base/types/events' {
NativeWithdrawed: AugmentedEvent<ApiType, [amount: u128], { amount: u128 }>;
OrderCanceled: AugmentedEvent<ApiType, [orderIndex: u64], { orderIndex: u64 }>;
OrderCreated: AugmentedEvent<ApiType, [orderIndex: u64, order: PalletDexOrder], { orderIndex: u64, order: PalletDexOrder }>;
OrderMatched: AugmentedEvent<ApiType, [quantityBase: u128, quantityQuote: u128, takerOrder: PalletDexOrder, makerOrder: PalletDexOrder], { quantityBase: u128, quantityQuote: u128, takerOrder: PalletDexOrder, makerOrder: PalletDexOrder }>;
OrderTaken: AugmentedEvent<ApiType, [account: AccountId32, orderIndex: u64, order: PalletDexOrder], { account: AccountId32, orderIndex: u64, order: PalletDexOrder }>;
SubmitProcessedReceipts: AugmentedEvent<ApiType, [blockNumber: u64], { blockNumber: u64 }>;
Withdrawed: AugmentedEvent<ApiType, [assetId: u32, amount: u128], { assetId: u32, amount: u128 }>;
Expand Down
Loading