Skip to content

Commit

Permalink
test: get the assets amount dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed Jun 7, 2024
1 parent 5994edb commit 9a0c244
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
25 changes: 24 additions & 1 deletion internal/handler/wallet/get_all_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"testing"

"github.com/massalabs/station-massa-wallet/api/server/models"
"github.com/massalabs/station-massa-wallet/api/server/restapi/operations"
"github.com/massalabs/station-massa-wallet/pkg/assets"
"github.com/massalabs/station-massa-wallet/pkg/network"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,7 +26,8 @@ func TestGetAllAssetsHandler(t *testing.T) {
assetsWithBalance := getAssets(t, api, nickname)

// Assert that assetsWithBalance contains the expected data
assert.Len(t, assetsWithBalance, 9, "the assets list should have 9 items")
counter := getExpectedAssetsCount(t)
assert.Len(t, assetsWithBalance, counter+1) // +1 for native MAS

assert.Equal(t, "1000000", assetsWithBalance[0].Balance)
assert.Equal(t, "Massa", assetsWithBalance[0].AssetInfo.Name)
Expand Down Expand Up @@ -57,3 +61,22 @@ func assertAssetInfoWithBalanceEqual(t *testing.T, actual, expected *models.Asse
assert.Equal(t, expected.AssetInfo.Decimals, actual.AssetInfo.Decimals)
assert.Equal(t, expected.AssetInfo.ChainID, actual.AssetInfo.ChainID)
}

func getExpectedAssetsCount(t *testing.T) int {
tempDir, err := os.MkdirTemp(os.TempDir(), "*-wallet-dir")
assert.NoError(t, err)
nodeFetcher := network.NewNodeFetcher()
store, err := assets.NewAssetsStore(tempDir, nodeFetcher)
assert.NoError(t, err)
defaultAssets, err := store.Default()
assert.NoError(t, err)
counter := 0

for _, asset := range defaultAssets {
if asset.ChainID == 77658377 {
counter += 1
}
}

return counter
}
48 changes: 21 additions & 27 deletions pkg/assets/default_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *AssetsStore) InitDefault() error {
}

// if the content is different, overwrite the default assets JSON file
if string(content) != assetsJSON {
if string(content) != AssetsJSON {
if err := s.createFileDefault(defaultAssetsJSONPath); err != nil {
return err
}
Expand All @@ -87,14 +87,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(assetsJSON), permissionUrwGrOr); err != nil {
if err := os.WriteFile(path, []byte(AssetsJSON), permissionUrwGrOr); err != nil {
return err
}

return nil
}

const assetsJSON = `[
const AssetsJSON = `[
{
"address": "AS12k8viVmqPtRuXzCm6rKXjLgpQWqbuMjc37YHhB452KSUUb9FgL",
"name": "Sepolia USDC",
Expand Down Expand Up @@ -151,30 +151,6 @@ const assetsJSON = `[
"MEXCSymbol": "ETHUSDT",
"ChainID": 77658377
},
{
"address": "AS133eqPPaPttJ6hJnk3sfoG5cjFFqBDi1VGxdo2wzWkq8AfZnan",
"name": "Purrfect Universe",
"symbol": "PUR",
"decimals": 18,
"MEXCSymbol": "",
"ChainID": 77658377
},
{
"address": "",
"name": "Wrapped Ether",
"symbol": "WETH.b",
"decimals": 18,
"MEXCSymbol": "ETHUSDT",
"ChainID": 77658377
},
{
"address": "",
"name": "Wrapped Binance USD",
"symbol": "USDT.b",
"decimals": 18,
"MEXCSymbol": "USD",
"ChainID": 77658377
},
{
"address": "AS12RmCXTA9NZaTBUBnRJuH66AGNmtEfEoqXKxLdmrTybS6GFJPFs",
"name": "Wrapped Ether",
Expand All @@ -192,3 +168,21 @@ const assetsJSON = `[
"ChainID": 77658366
}
]`

// TODO: add the following assets with the addresses
// {
// "address": "",
// "name": "Wrapped Ether",
// "symbol": "WETH.b",
// "decimals": 18,
// "MEXCSymbol": "ETHUSDT",
// "ChainID": 77658377
// },
// {
// "address": "",
// "name": "Wrapped Binance USD",
// "symbol": "USDT.b",
// "decimals": 18,
// "MEXCSymbol": "USD",
// "ChainID": 77658377
// },

0 comments on commit 9a0c244

Please sign in to comment.