Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pur token #959

Merged
merged 6 commits into from
Jun 7, 2024
Merged
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
17 changes: 12 additions & 5 deletions internal/handler/wallet/get_all_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,28 @@ func (g *getAllAssets) Handle(params operations.GetAllAssetsParams) middleware.R

assetsWithBalance = append(assetsWithBalance, userAssetData...)

// sort AssetsWithBalance by name
sort.Slice(assetsWithBalance, func(i, j int) bool {
if assetsWithBalance[i].AssetInfo.Symbol == "MAS" {
return true
}
if assetsWithBalance[j].AssetInfo.Symbol == "MAS" {
return false
}
if assetsWithBalance[i].DollarValue == nil {
return false

valueI := assetsWithBalance[i].DollarValue
valueJ := assetsWithBalance[j].DollarValue

if (valueI == nil || *valueI == 0) && (valueJ == nil || *valueJ == 0) {
return assetsWithBalance[i].AssetInfo.Symbol < assetsWithBalance[j].AssetInfo.Symbol
}
if assetsWithBalance[j].DollarValue == nil {
if valueI == nil || *valueI == 0 {
return false
}
return *assetsWithBalance[i].DollarValue > *assetsWithBalance[j].DollarValue
if valueJ == nil || *valueJ == 0 {
return true
}

return *valueI > *valueJ
})

// Return the list of assets with balance
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/wallet/get_all_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGetAllAssetsHandler(t *testing.T) {
assetsWithBalance := getAssets(t, api, nickname)

// Assert that assetsWithBalance contains the expected data
assert.Len(t, assetsWithBalance, 8, "the assets list should have 8 items")
assert.Len(t, assetsWithBalance, 9, "the assets list should have 9 items")

assert.Equal(t, "1000000", assetsWithBalance[0].Balance)
assert.Equal(t, "Massa", assetsWithBalance[0].AssetInfo.Name)
Expand Down
46 changes: 36 additions & 10 deletions pkg/assets/default_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,30 @@ func (s *AssetsStore) InitDefault() error {
return err
}

if _, err := os.Stat(defaultAssetsJSONPath); os.IsNotExist(err) {
if err := s.createFileDefault(defaultAssetsJSONPath); err != nil {
// if the file does not exist, create the default assets JSON file
_, err = os.Stat(defaultAssetsJSONPath)
if os.IsNotExist(err) {
// Create the default assets JSON file
return s.createFileDefault(defaultAssetsJSONPath)
}

// if the file exists, read the content and compare it with the default assets
if err == nil {
// read the content of the default assets JSON file
content, err := os.ReadFile(defaultAssetsJSONPath)
if err != nil {
return err
}

// if the content is different, overwrite the default assets JSON file
if string(content) != assetsJSON {
if err := s.createFileDefault(defaultAssetsJSONPath); err != nil {
return err
}
}
}

return nil
return err
}

// getDefaultJSONPath returns the path to the default assets JSON file.
Expand All @@ -69,7 +86,14 @@ func getDefaultJSONPath(assetsJSONDir string) (string, error) {

// createFileDefault creates the default assets JSON file with the default assets.
func (s *AssetsStore) createFileDefault(path string) error {
if err := os.WriteFile(path, []byte(`[
if err := os.WriteFile(path, []byte(assetsJSON), permissionUrwGrOr); err != nil {
return err
}

return nil
}

const assetsJSON = `[
{
"address": "AS12k8viVmqPtRuXzCm6rKXjLgpQWqbuMjc37YHhB452KSUUb9FgL",
"name": "Sepolia USDC",
Expand Down Expand Up @@ -118,10 +142,12 @@ func (s *AssetsStore) createFileDefault(path string) error {
"symbol": "WETH.e",
"decimals": 18,
"MEXCSymbol": "ETHUSDT"
},
{
"address": "AS133eqPPaPttJ6hJnk3sfoG5cjFFqBDi1VGxdo2wzWkq8AfZnan",
"name": "Purrfect Universe",
"symbol": "PUR",
"decimals": 18,
"MEXCSymbol": ""
}
]`), permissionUrwGrOr); err != nil {
return err
}

return nil
}
]`
34 changes: 17 additions & 17 deletions wails-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions web-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions web-frontend/src/pages/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useState } from 'react';

import { Token, getAssetIcons } from '@massalabs/react-ui-kit';
import { Token } from '@massalabs/react-ui-kit';

import { useFTTransfer } from '@/custom/smart-contract/useFTTransfer';
import Intl from '@/i18n/i18n';
import { Asset } from '@/models/AssetModel';
import { DeleteAssetModal } from '@/pages/Assets/DeleteAssets';
import { symbolDict } from '@/utils/tokenIcon';
import { tokenIcon } from '@/utils/tokenIcon';

interface AssetsListProps {
assets: Asset[] | undefined;
Expand All @@ -23,20 +22,13 @@ export function AssetsList(props: AssetsListProps) {
setModal(true);
}

const { isMainnet } = useFTTransfer();

return (
<>
{assets
?.filter((a) => a.balance !== undefined && a.balance !== '')
.map((token: Asset, index: number) => (
<Token
logo={getAssetIcons(
symbolDict[token.symbol as keyof typeof symbolDict],
true,
isMainnet,
32,
)}
logo={tokenIcon(token.symbol, 32)}
name={token.name}
symbol={token.symbol}
decimals={token.decimals}
Expand Down
18 changes: 2 additions & 16 deletions web-frontend/src/pages/TransferCoins/ReceiveCoins/GenerateLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import {
PopupModalHeader,
Clipboard,
formatAmount,
getAssetIcons,
Selector,
Identicon,
} from '@massalabs/react-ui-kit';

import { useFTTransfer } from '@/custom/smart-contract/useFTTransfer';
import Intl from '@/i18n/i18n';
import { AccountObject } from '@/models/AccountModel';
import { Asset } from '@/models/AssetModel';
import { AssetSelector } from '@/pages/TransferCoins/SendCoins/AssetSelector';
import { parseForm } from '@/utils/';
import { symbolDict } from '@/utils/tokenIcon';
import { tokenIcon } from '@/utils/tokenIcon';
import { SendInputsErrors } from '@/validation/sendInputs';

interface MoneyForm {
Expand All @@ -37,8 +35,6 @@ interface GenerateLinkProps {
function GenerateLink(props: GenerateLinkProps) {
const { account, presetURL, setURL, setModal } = props;

const { isMainnet } = useFTTransfer();

const [amount, setAmount] = useState<string>('');
const [link, setLink] = useState('');
const [error, setError] = useState<SendInputsErrors | null>(null);
Expand Down Expand Up @@ -121,17 +117,7 @@ function GenerateLink(props: GenerateLinkProps) {
preIcon={<Identicon username={account.nickname} />}
content={account.nickname}
amount={formattedBalance}
posIcon={
getAssetIcons(
symbolDict[
selectedAsset?.symbol as keyof typeof symbolDict
],
true,
isMainnet,
24,
'mr-3',
) as JSX.Element
}
posIcon={tokenIcon(selectedAsset?.symbol || '', 24)}
variant="secondary"
/>
</div>
Expand Down
18 changes: 3 additions & 15 deletions web-frontend/src/pages/TransferCoins/SendCoins/AssetSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { useEffect } from 'react';

import {
Dropdown,
formatAmount,
getAssetIcons,
IOption,
} from '@massalabs/react-ui-kit';
import { Dropdown, formatAmount, IOption } from '@massalabs/react-ui-kit';
import { useParams } from 'react-router-dom';

import { useResource } from '@/custom/api';
import { useFTTransfer } from '@/custom/smart-contract/useFTTransfer';
import Intl from '@/i18n/i18n';
import { Asset } from '@/models/AssetModel';
import { symbolDict } from '@/utils/tokenIcon';
import { tokenIcon } from '@/utils/tokenIcon';

interface AssetSelectorProps {
selectedAsset: Asset | undefined;
Expand All @@ -23,7 +17,6 @@ interface AssetSelectorProps {
export function AssetSelector(props: AssetSelectorProps) {
const { selectedAsset, setSelectedAsset, selectSymbol } = props;
const { nickname } = useParams();
const { isMainnet } = useFTTransfer();

const { data: assets, isLoading: isAssetsLoading } = useResource<Asset[]>(
`accounts/${nickname}/assets`,
Expand Down Expand Up @@ -60,12 +53,7 @@ export function AssetSelector(props: AssetSelectorProps) {
</p>
</div>
),
icon: getAssetIcons(
symbolDict[asset.symbol as keyof typeof symbolDict],
true,
isMainnet,
28,
),
icon: tokenIcon(asset.symbol, 28),
onClick: () => setSelectedAsset(asset),
};
});
Expand Down
Loading
Loading