Skip to content

Commit

Permalink
Merge pull request #392 from shiatcb/ROSE-231
Browse files Browse the repository at this point in the history
ROSE-231: Call /account/coins endpoint to add account coin info in coin storage
  • Loading branch information
racbc authored Mar 25, 2022
2 parents de67541 + dcbcf2d commit 2bcdd1d
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 11 deletions.
43 changes: 43 additions & 0 deletions mocks/utils/fetcher_helper.go

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

23 changes: 15 additions & 8 deletions storage/modules/coin_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,21 +501,28 @@ func (c *CoinStorage) GetLargestCoin(
// This is used when importing prefunded addresses.
func (c *CoinStorage) SetCoinsImported(
ctx context.Context,
accountBalances []*utils.AccountBalance,
accts []*types.AccountIdentifier,
acctCoinsResp []*utils.AccountCoinsResponse,
) error {
var accountCoins []*types.AccountCoin
for _, accountBalance := range accountBalances {
for _, coin := range accountBalance.Coins {
accountCoin := &types.AccountCoin{
Account: accountBalance.Account,
// Request array length should always equal response array length.
// But we still check it for sure.
if len(accts) != len(acctCoinsResp) {
return errors.ErrCoinImportFailed
}

var acctCoins []*types.AccountCoin
for i, resp := range acctCoinsResp {
for _, coin := range resp.Coins {
acctCoin := &types.AccountCoin{
Account: accts[i],
Coin: coin,
}

accountCoins = append(accountCoins, accountCoin)
acctCoins = append(acctCoins, acctCoin)
}
}

if err := c.AddCoins(ctx, accountCoins); err != nil {
if err := c.AddCoins(ctx, acctCoins); err != nil {
return fmt.Errorf("%w: %v", errors.ErrCoinImportFailed, err)
}

Expand Down
14 changes: 11 additions & 3 deletions storage/modules/coin_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ var (
},
},
}

acctCoins = []*utils.AccountCoinsResponse{
{
Coins: accBalance1.Coins,
},
{
Coins: accBalance2.Coins,
},
}
)

func TestCoinStorage(t *testing.T) {
Expand Down Expand Up @@ -961,9 +970,8 @@ func TestCoinStorage(t *testing.T) {
})

t.Run("SetCoinsImported", func(t *testing.T) {
accBalances := []*utils.AccountBalance{accBalance1, accBalance2}

err := c.SetCoinsImported(ctx, accBalances)
accts := []*types.AccountIdentifier{accBalance1.Account, accBalance2.Account}
err := c.SetCoinsImported(ctx, accts, acctCoins)
assert.NoError(t, err)

mockHelper.On(
Expand Down
59 changes: 59 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ type FetcherHelper interface {
block *types.PartialBlockIdentifier,
currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Amount, map[string]interface{}, *fetcher.Error)

AccountCoinsRetry(
ctx context.Context,
network *types.NetworkIdentifier,
acct *types.AccountIdentifier,
includeMempool bool,
currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Coin, map[string]interface{}, *fetcher.Error)
}

type BlockStorageHelper interface {
Expand Down Expand Up @@ -491,6 +499,57 @@ func GetAccountBalances(
return accountBalances, nil
}

// -------------------------------------------------------------------------------
// ----------------- Helper struct for fetching account coins --------------------
// -------------------------------------------------------------------------------

// AccountCoinsRequest defines the required information to get an account's coins.
type AccountCoinsRequest struct {
Account *types.AccountIdentifier
Network *types.NetworkIdentifier
Currencies []*types.Currency
IncludeMempool bool
}

// AccountCoins defines an account's coins info at tip.
type AccountCoinsResponse struct {
Coins []*types.Coin
}

// GetAccountCoins calls /account/coins endpoint and returns an array of coins at tip.
func GetAccountCoins(
ctx context.Context,
fetcher FetcherHelper,
acctCoinsReqs []*AccountCoinsRequest,
) ([]*AccountCoinsResponse, error) {
var acctCoins []*AccountCoinsResponse
for _, req := range acctCoinsReqs {
_, coins, _, err := fetcher.AccountCoinsRetry(
ctx,
req.Network,
req.Account,
req.IncludeMempool,
req.Currencies,
)

if err != nil {
return nil, err.Err
}

resp := &AccountCoinsResponse{
Coins: coins,
}

acctCoins = append(acctCoins, resp)
}

return acctCoins, nil
}

// -------------------------------------------------------------------------------
// ------------------- End of helper struct for account coins --------------------
// -------------------------------------------------------------------------------

// AtTip returns a boolean indicating if a block timestamp
// is within tipDelay from the current time.
func AtTip(
Expand Down

0 comments on commit 2bcdd1d

Please sign in to comment.