Skip to content

Commit

Permalink
prevent removing default assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed May 7, 2024
1 parent d83b30b commit 9dfe79d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
11 changes: 11 additions & 0 deletions api/server/models/asset_info_with_balance.go

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

6 changes: 6 additions & 0 deletions api/server/restapi/embedded_spec.go

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

16 changes: 9 additions & 7 deletions api/walletApi-V0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ paths:
operationId: SignMessage
description: Sign a message using the account associated with the provided nickname in the path.
parameters:
- $ref: "#/parameters/nickname"
- in: body
name: body
required: true
x-nullable: false
schema:
$ref: "#/definitions/SignMessageRequest"
- $ref: "#/parameters/nickname"
- in: body
name: body
required: true
x-nullable: false
schema:
$ref: "#/definitions/SignMessageRequest"
produces:
- application/json
responses:
Expand Down Expand Up @@ -753,3 +753,5 @@ definitions:
properties:
balance:
type: string
isDefault:
type: boolean
30 changes: 18 additions & 12 deletions internal/handler/wallet/get_all_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (g *getAllAssets) getMASAsset(acc *account.Account) (*models.AssetInfoWithB
massaAsset := &models.AssetInfoWithBalance{
AssetInfo: assets.MASInfo(),
Balance: fmt.Sprint(infos[0].CandidateBalance),
IsDefault: true,
}

return massaAsset, nil
Expand All @@ -90,31 +91,41 @@ func (g *getAllAssets) getAssetsData(acc *account.Account) ([]*models.AssetInfoW
logger.Errorf("Failed to get default assets: %s", err.Error())
}

assetsInfo := make([]models.AssetInfo, 0)
assetsInfo := make([]*models.AssetInfoWithBalance, 0)

// Initialize map to track addressed already added
includedAddresses := map[string]bool{}

for _, asset := range defaultAssets {
assetsInfo = append(assetsInfo, asset)
completeAsset := &models.AssetInfoWithBalance{
AssetInfo: asset,
Balance: "",
IsDefault: true,
}
assetsInfo = append(assetsInfo, completeAsset)
includedAddresses[asset.Address] = true
}

// Append default assets ensuring no duplication
for _, asset := range g.AssetsStore.Assets[acc.Nickname].ContractAssets {
// Append the asset info to the result slice if it is not already in the list
if _, exists := includedAddresses[asset.Address]; !exists {
assetsInfo = append(assetsInfo, asset)
completeAsset := &models.AssetInfoWithBalance{
AssetInfo: asset,
Balance: "",
IsDefault: false,
}
assetsInfo = append(assetsInfo, completeAsset)
includedAddresses[asset.Address] = true
}
}

assetsWithBalance := make([]*models.AssetInfoWithBalance, 0)

// Retrieve all assets from the selected nickname
for _, assetInfo := range assetsInfo {
for _, asset := range assetsInfo {
// Fetch the balance for the current asset
balance, resp := g.fetchAssetData(assetInfo.Address, acc)
balance, resp := g.fetchAssetData(asset.Address, acc)
if resp != nil {
return nil, resp
}
Expand All @@ -124,13 +135,8 @@ func (g *getAllAssets) getAssetsData(acc *account.Account) ([]*models.AssetInfoW
continue
}

// Create the asset info with balance and append it to the result slice
assetWithBalance := &models.AssetInfoWithBalance{
AssetInfo: assetInfo,
Balance: *balance,
}

assetsWithBalance = append(assetsWithBalance, assetWithBalance)
asset.Balance = *balance
assetsWithBalance = append(assetsWithBalance, asset)
}

return assetsWithBalance, nil
Expand Down
1 change: 1 addition & 0 deletions web-frontend/src/models/AssetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface Asset {
symbol: string;
decimals: number;
balance: string;
isDefault: boolean;
}
2 changes: 1 addition & 1 deletion web-frontend/src/pages/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function AssetsList(props: AssetsListProps) {
decimals={token.decimals}
balance={token.balance}
key={index}
disable={token?.symbol === MAS ? true : false}
disable={token?.isDefault}
onDelete={() => {
handleDelete(token.address);
}}
Expand Down

0 comments on commit 9dfe79d

Please sign in to comment.