Skip to content

Commit

Permalink
Merge pull request #133 from eqlabs/latenssi/token-balance-fix
Browse files Browse the repository at this point in the history
Refactor handling of a tokens balance
  • Loading branch information
latenssi authored Jul 9, 2021
2 parents 9842ae6 + 8f8bc9b commit a94f1fa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
10 changes: 4 additions & 6 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ components:
example: "FlowToken"
balance:
type: number
example: 9999999999.99700000
example: "1.0"

accountNonFungibleToken:
type: object
Expand All @@ -881,11 +881,9 @@ components:
type: string
example: "ExampleNFT"
balance:
type: object
properties:
Values:
type: array
example: [1,2,3]
type: array
example: [1,2,3]



parameters:
Expand Down
11 changes: 6 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,8 @@ func TestTokenHandlers(t *testing.T) {

aa0NftDetails, err := tokenService.Details(context.Background(), exampleNft.Name, testAccounts[0].Address)
fatal(t, err)
nftIDs := aa0NftDetails.Balance.(cadence.Array).Values

nftIDs := aa0NftDetails.Balance.CadenceValue.(cadence.Array).Values

_, testTransferNFT, err := tokenService.CreateWithdrawal(
context.Background(),
Expand All @@ -1161,31 +1162,31 @@ func TestTokenHandlers(t *testing.T) {
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s", testAccounts[1].Address, flowToken.Name),
expected: `(?m)^{"name":"FlowToken","balance":\d*\.?\d*}$`,
expected: `(?m)^{"name":"FlowToken","balance":"\d*\.?\d*"}$`,
status: http.StatusOK,
},
{
name: "FUSD details",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/fungible-tokens/%s", testAccounts[1].Address, fusd.Name),
expected: `(?m)^{"name":"FUSD\","balance":\d*\.?\d*}$`,
expected: `(?m)^{"name":"FUSD\","balance":"\d*\.?\d*"}$`,
status: http.StatusOK,
},
{
name: "ExampleNFT details",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/non-fungible-tokens/%s", testAccounts[1].Address, exampleNft.Name),
expected: `(?m)^{"name":"ExampleNFT\","balance":{"Values":\[\d+\]}}$`,
expected: `(?m)^{"name":"ExampleNFT\","balance":\[\d+\]}$`,
status: http.StatusOK,
},
{
name: "ExampleNFT details",
method: http.MethodGet,
contentType: "application/json",
url: fmt.Sprintf("/%s/non-fungible-tokens/%s", testAccounts[0].Address, exampleNft.Name),
expected: `(?m)^{"name":"ExampleNFT\","balance":{"Values":\[(\d+,)+\d+\]}}$`,
expected: `(?m)^{"name":"ExampleNFT\","balance":\[(\d+,)+\d+\]}$`,
status: http.StatusOK,
},
}
Expand Down
28 changes: 28 additions & 0 deletions tokens/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tokens

import (
"encoding/json"

"github.com/onflow/cadence"
)

type Balance struct {
CadenceValue cadence.Value
}

func (b *Balance) MarshalJSON() ([]byte, error) {
if b.CadenceValue == nil {
// Not able to omit the balance field as it is in a "parent" struct
// So using JSON null here
return json.Marshal(nil)
}

// Only handle fixed point numbers differently, rest can use the default
_, isUfix64 := b.CadenceValue.Type().(cadence.UFix64Type)
_, isFix64 := b.CadenceValue.Type().(cadence.Fix64Type)
if isUfix64 || isFix64 {
return json.Marshal(b.CadenceValue.String())
}

return json.Marshal(b.CadenceValue.ToGoValue())
}
4 changes: 2 additions & 2 deletions tokens/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ func (s *Service) Details(ctx context.Context, tokenName, address string) (*Deta
},
}

b, err := s.transactions.ExecuteScript(ctx, r)
res, err := s.transactions.ExecuteScript(ctx, r)
if err != nil {
return nil, err
}

return &Details{TokenName: token.Name, Balance: b}, nil
return &Details{TokenName: token.Name, Balance: &Balance{CadenceValue: res}}, nil
}

func (s *Service) CreateWithdrawal(ctx context.Context, runSync bool, sender string, request WithdrawalRequest) (*jobs.Job, *transactions.Transaction, error) {
Expand Down
7 changes: 3 additions & 4 deletions tokens/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"github.com/eqlabs/flow-wallet-api/accounts"
"github.com/eqlabs/flow-wallet-api/templates"
"github.com/eqlabs/flow-wallet-api/transactions"
"github.com/onflow/cadence"
"gorm.io/gorm"
)

type Details struct {
TokenName string `json:"name"`
Address string `json:"address,omitempty"`
Balance cadence.Value `json:"balance,omitempty"`
TokenName string `json:"name"`
Address string `json:"address,omitempty"`
Balance *Balance `json:"balance,omitempty"`
}

type WithdrawalRequest struct {
Expand Down

0 comments on commit a94f1fa

Please sign in to comment.