From 1577c696165ab1b9923cd2f6229e0d7b9634fa1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20S=C3=A9n=C3=A9chal?= <44696378+thomas-senechal@users.noreply.github.com> Date: Thu, 17 Nov 2022 11:50:38 +0100 Subject: [PATCH] 223 update wallet balance (#269) adds wallet balance display to front. --- api/swagger/server/models/wallet.go | 3 + api/swagger/server/restapi/embedded_spec.go | 68 +++++-------------- .../server/restapi/resource/swagger.yml | 3 + int/api/html/front/errors.js | 5 ++ int/api/html/front/wallet.js | 2 +- int/api/wallet/create.go | 1 + int/api/wallet/errors.go | 1 + int/api/wallet/get.go | 26 ++++++- pkg/node/ledger/address.go | 4 +- 9 files changed, 57 insertions(+), 56 deletions(-) diff --git a/api/swagger/server/models/wallet.go b/api/swagger/server/models/wallet.go index acd0102e6..98e100a1e 100644 --- a/api/swagger/server/models/wallet.go +++ b/api/swagger/server/models/wallet.go @@ -24,6 +24,9 @@ type Wallet struct { // Required: true Address *string `json:"address"` + // wallet's balance. + Balance float64 `json:"balance,omitempty"` + // wallet's key pairs. // Required: true KeyPairs []*WalletKeyPairsItems0 `json:"keyPairs"` diff --git a/api/swagger/server/restapi/embedded_spec.go b/api/swagger/server/restapi/embedded_spec.go index 4c2fc3819..facca9322 100644 --- a/api/swagger/server/restapi/embedded_spec.go +++ b/api/swagger/server/restapi/embedded_spec.go @@ -135,10 +135,7 @@ func init() { "default": { "args": "", "at": "A1MrqLgWq5XXDpTBH6fzXHUg7E8M5U2fYDAF3E1xnUSzyZuKpMh", - "coins": { - "parallel": 0, - "sequential": 0 - }, + "coins": 0, "expiry": 3, "fee": 0, "gaz": { @@ -166,20 +163,9 @@ func init() { "x-nullable": false }, "coins": { - "description": "Coins to be send from caller to smart contract address.", - "type": "object", - "properties": { - "parallel": { - "description": "Number of parallel coins to transfer from the caller to the smart contract address.", - "type": "number", - "default": 0 - }, - "sequential": { - "description": "Number of sequential coins to transfer from the caller to the smart contract address.", - "type": "number", - "default": 0 - } - } + "description": "Set the fee amount (in massa) that will be given to the block creator.", + "type": "number", + "default": 0 }, "expiry": { "description": "Set the expiry duration (in number of slots) of the transaction.", @@ -1024,6 +1010,10 @@ func init() { "description": "wallet's address.", "type": "string" }, + "balance": { + "description": "wallet's balance.", + "type": "number" + }, "keyPairs": { "description": "wallet's key pairs.", "type": "array", @@ -1206,10 +1196,7 @@ func init() { "default": { "args": "", "at": "A1MrqLgWq5XXDpTBH6fzXHUg7E8M5U2fYDAF3E1xnUSzyZuKpMh", - "coins": { - "parallel": 0, - "sequential": 0 - }, + "coins": 0, "expiry": 3, "fee": 0, "gaz": { @@ -1237,20 +1224,9 @@ func init() { "x-nullable": false }, "coins": { - "description": "Coins to be send from caller to smart contract address.", - "type": "object", - "properties": { - "parallel": { - "description": "Number of parallel coins to transfer from the caller to the smart contract address.", - "type": "number", - "default": 0 - }, - "sequential": { - "description": "Number of sequential coins to transfer from the caller to the smart contract address.", - "type": "number", - "default": 0 - } - } + "description": "Set the fee amount (in massa) that will be given to the block creator.", + "type": "number", + "default": 0 }, "expiry": { "description": "Set the expiry duration (in number of slots) of the transaction.", @@ -1979,22 +1955,6 @@ func init() { } }, "definitions": { - "CmdExecuteFunctionParamsBodyCoins": { - "description": "Coins to be send from caller to smart contract address.", - "type": "object", - "properties": { - "parallel": { - "description": "Number of parallel coins to transfer from the caller to the smart contract address.", - "type": "number", - "default": 0 - }, - "sequential": { - "description": "Number of sequential coins to transfer from the caller to the smart contract address.", - "type": "number", - "default": 0 - } - } - }, "CmdExecuteFunctionParamsBodyGaz": { "description": "Gaz attibutes. Gaz is a virtual resource consumed by node while running smart contract.", "type": "object", @@ -2126,6 +2086,10 @@ func init() { "description": "wallet's address.", "type": "string" }, + "balance": { + "description": "wallet's balance.", + "type": "number" + }, "keyPairs": { "description": "wallet's key pairs.", "type": "array", diff --git a/api/swagger/server/restapi/resource/swagger.yml b/api/swagger/server/restapi/resource/swagger.yml index 1ac9fb3bf..b1e2b547b 100644 --- a/api/swagger/server/restapi/resource/swagger.yml +++ b/api/swagger/server/restapi/resource/swagger.yml @@ -655,6 +655,9 @@ definitions: address: description: wallet's address. type: string + balance: + description: wallet's balance. + type: number keyPairs: description: wallet's key pairs. type: array diff --git a/int/api/html/front/errors.js b/int/api/html/front/errors.js index 4469e52a3..9784db8d3 100644 --- a/int/api/html/front/errors.js +++ b/int/api/html/front/errors.js @@ -19,6 +19,11 @@ const errorCodes = new Map([ "Wallet-4001", "Error while connecting all your wallets. Reconnect all your wallets and try again", ], + [ + "Wallet-4002", + "Error while fetching your balance. Reconnect all your wallets and try again", + ], + ["Wallet-5001", "Please select a wallet to be able to see your domains"], ["Wallet-5002", "Please select a wallet to be able to perform that action"], diff --git a/int/api/html/front/wallet.js b/int/api/html/front/wallet.js index 3cfaa97e0..74edc9534 100644 --- a/int/api/html/front/wallet.js +++ b/int/api/html/front/wallet.js @@ -76,7 +76,7 @@ function tableInsert(resp) { cell0.innerHTML = addressInnerHTML(resp.address); cell1.innerHTML = resp.nickname; - cell2.innerHTML = 0; + cell2.innerHTML = resp.balance ?? 0; cell3.innerHTML = ' '; } diff --git a/int/api/wallet/create.go b/int/api/wallet/create.go index ba8d458d4..6fc2eaf79 100644 --- a/int/api/wallet/create.go +++ b/int/api/wallet/create.go @@ -101,5 +101,6 @@ func (c *walletCreate) Handle(params operations.MgmtWalletCreateParams) middlewa Salt: &salt, Nonce: &nonce, }}, + Balance: 0, }) } diff --git a/int/api/wallet/errors.go b/int/api/wallet/errors.go index 089a80209..a339b814a 100644 --- a/int/api/wallet/errors.go +++ b/int/api/wallet/errors.go @@ -12,4 +12,5 @@ const ( errorCodeWalletDeleteFile = "Wallet-2002" errorCodeWalletImportNew = "Wallet-3001" errorCodeWalletGetWallets = "Wallet-4001" + errorCodeWalletGetBalance = "Wallet-4002" ) diff --git a/int/api/wallet/get.go b/int/api/wallet/get.go index c07a6eaa1..8334f0ce6 100644 --- a/int/api/wallet/get.go +++ b/int/api/wallet/get.go @@ -1,11 +1,14 @@ package wallet import ( + "strconv" "sync" "github.com/go-openapi/runtime/middleware" "github.com/massalabs/thyra/api/swagger/server/models" "github.com/massalabs/thyra/api/swagger/server/restapi/operations" + "github.com/massalabs/thyra/pkg/node" + "github.com/massalabs/thyra/pkg/node/ledger" "github.com/massalabs/thyra/pkg/wallet" ) @@ -20,6 +23,8 @@ type walletGet struct { //nolint:nolintlint,ireturn func (c *walletGet) Handle(params operations.MgmtWalletGetParams) middleware.Responder { + client := node.NewDefaultClient() + wallets, err := wallet.LoadAll() if err != nil { return operations.NewMgmtWalletGetInternalServerError().WithPayload( @@ -31,11 +36,30 @@ func (c *walletGet) Handle(params operations.MgmtWalletGetParams) middleware.Res var wal []*models.Wallet - for i := 0; i < len(wallets); i++ { + for i := 0; i < len(wallets); i++ { //nolint:varnamelen + address, err := ledger.Addresses(client, []string{wallets[i].Address}) + if err != nil { + return operations.NewMgmtWalletGetInternalServerError().WithPayload( + &models.Error{ + Code: errorCodeWalletGetBalance, + Message: err.Error(), + }) + } + + balance, err := strconv.ParseFloat(address[0].CandidateBalance, 64) + if err != nil { + return operations.NewMgmtWalletGetInternalServerError().WithPayload( + &models.Error{ + Code: errorCodeWalletGetBalance, + Message: err.Error(), + }) + } + walletss := &models.Wallet{ Nickname: &wallets[i].Nickname, Address: &wallets[i].Address, KeyPairs: []*models.WalletKeyPairsItems0{}, + Balance: balance, } wal = append(wal, walletss) diff --git a/pkg/node/ledger/address.go b/pkg/node/ledger/address.go index 2c5159337..72ab95ba6 100644 --- a/pkg/node/ledger/address.go +++ b/pkg/node/ledger/address.go @@ -12,9 +12,9 @@ type Address struct { Address string `json:"address"` BlockDraws []string `json:"block_draws"` BlocksCreated []string `json:"blocks_created"` - CandidateBalanceInfo string `json:"candidate_balance_info"` + CandidateBalance string `json:"candidate_balance"` CandidateDatastoreKeys [][]byte `json:"candidate_datastore_keys"` - FinalBalanceInfo string `json:"final_balance_info"` + FinalBalance string `json:"final_balance"` FinalDatastoreKeys [][]byte `json:"final_datastore_keys"` }