Skip to content

Commit

Permalink
Fix staticcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Aug 26, 2021
1 parent cfad4df commit a7efa95
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 186 deletions.
1 change: 1 addition & 0 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ type Trade struct {
LedgerCloseTime time.Time `json:"ledger_close_time"`
OfferID string `json:"offer_id"`
TradeType string `json:"trade_type"`
LiquidityPoolFeeBP int32 `json:"liquidity_pool_fee_bp,omitempty"`
BaseLiquidityPoolID string `json:"base_liquidity_pool_id,omitempty"`
BaseOfferID string `json:"base_offer_id,omitempty"`
BaseAccount string `json:"base_account"`
Expand Down
139 changes: 114 additions & 25 deletions services/horizon/internal/actions_trade_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//lint:file-ignore U1001 Ignore all unused code, thinks the code is unused because of the test skips
package horizon

import (
"context"
"encoding/json"
"fmt"
"github.com/guregu/null"
"github.com/stellar/go/keypair"
"net/url"
"strconv"
"strings"
Expand All @@ -12,7 +16,6 @@ import (
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/history"
. "github.com/stellar/go/services/horizon/internal/db2/history"
. "github.com/stellar/go/services/horizon/internal/test/trades"
"github.com/stellar/go/support/render/hal"
stellarTime "github.com/stellar/go/support/time"
"github.com/stellar/go/xdr"
Expand Down Expand Up @@ -487,30 +490,6 @@ func TestTradeActions_AggregationOrdering(t *testing.T) {
}
}

func assertOfferType(ht *HTTPT, offerId string, idType OfferIDType) {
offerIdInt64, _ := strconv.ParseInt(offerId, 10, 64)
_, offerType := DecodeOfferID(offerIdInt64)
ht.Assert.Equal(offerType, idType)
}

// TestTradeActions_SyntheticOfferIds loads the offer_ids scenario and ensures that synthetic offer
// ids are created when necessary and not when unnecessary
func TestTradeActions_SyntheticOfferIds(t *testing.T) {
ht := StartHTTPTest(t, "offer_ids")
defer ht.Finish()
var records []horizon.Trade
w := ht.Get("/trades")
if ht.Assert.Equal(200, w.Code) {
if ht.Assert.PageOf(4, w.Body) {
ht.UnmarshalPage(w.Body, &records)
assertOfferType(ht, records[0].BaseOfferID, TOIDType)
assertOfferType(ht, records[1].BaseOfferID, TOIDType)
assertOfferType(ht, records[2].BaseOfferID, CoreOfferIDType)
assertOfferType(ht, records[3].BaseOfferID, CoreOfferIDType)
}
}
}

func TestTradeActions_AssetValidation(t *testing.T) {
ht := StartHTTPTest(t, "trades")
defer ht.Finish()
Expand Down Expand Up @@ -628,3 +607,113 @@ func TestTradeActions_AggregationOffset(t *testing.T) {
})
}
}

//GetTestAsset generates an issuer on the fly and creates a CreditAlphanum4 Asset with given code
func GetTestAsset(code string) xdr.Asset {
var codeBytes [4]byte
copy(codeBytes[:], []byte(code))
ca4 := xdr.AlphaNum4{Issuer: GetTestAccount(), AssetCode: codeBytes}
return xdr.Asset{Type: xdr.AssetTypeAssetTypeCreditAlphanum4, AlphaNum4: &ca4, AlphaNum12: nil}
}

//Get generates and returns an account on the fly
func GetTestAccount() xdr.AccountId {
var key xdr.Uint256
kp, _ := keypair.Random()
copy(key[:], kp.Address())
acc, _ := xdr.NewAccountId(xdr.PublicKeyTypePublicKeyTypeEd25519, key)
return acc
}

//IngestTestTrade mock ingests a trade
func IngestTestTrade(
q *Q,
assetSold xdr.Asset,
assetBought xdr.Asset,
seller xdr.AccountId,
buyer xdr.AccountId,
amountSold int64,
amountBought int64,
timestamp stellarTime.Millis,
opCounter int64) error {

trade := xdr.ClaimAtom{
Type: xdr.ClaimAtomTypeClaimAtomTypeOrderBook,
OrderBook: &xdr.ClaimOfferAtom{
AmountBought: xdr.Int64(amountBought),
SellerId: seller,
AmountSold: xdr.Int64(amountSold),
AssetBought: assetBought,
AssetSold: assetSold,
OfferId: 100,
},
}

price := xdr.Price{
N: xdr.Int32(amountBought),
D: xdr.Int32(amountSold),
}

ctx := context.Background()
accounts, err := q.CreateAccounts(ctx, []string{seller.Address(), buyer.Address()}, 2)
if err != nil {
return err
}
assets, err := q.CreateAssets(ctx, []xdr.Asset{assetBought, assetSold}, 2)
if err != nil {
return err
}

batch := q.NewTradeBatchInsertBuilder(0)
batch.Add(ctx, InsertTrade{
HistoryOperationID: opCounter,
Order: 0,
CounterAssetID: assets[assetBought.String()].ID,
CounterAccountID: null.IntFrom(accounts[buyer.Address()]),
CounterAmount: amountBought,

BaseAssetID: assets[assetSold.String()].ID,
BaseAccountID: null.IntFrom(accounts[seller.Address()]),
BaseAmount: amountSold,
BaseOfferID: null.IntFrom(int64(trade.OfferId())),
BaseIsSeller: true,
PriceN: int64(price.N),
PriceD: int64(price.D),
LedgerCloseTime: timestamp.ToTime(),
})
err = batch.Exec(ctx)
if err != nil {
return err
}

err = q.RebuildTradeAggregationTimes(context.Background(), timestamp, timestamp)
if err != nil {
return err
}

return nil
}

//PopulateTestTrades generates and ingests trades between two assets according to given parameters
func PopulateTestTrades(
q *Q,
startTs int64,
numOfTrades int,
delta int64,
opStart int64) (ass1 xdr.Asset, ass2 xdr.Asset, err error) {

acc1 := GetTestAccount()
acc2 := GetTestAccount()
ass1 = GetTestAsset("usd")
ass2 = GetTestAsset("euro")
for i := 1; i <= numOfTrades; i++ {
timestamp := stellarTime.MillisFromInt64(startTs + (delta * int64(i-1)))
err = IngestTestTrade(
q, ass1, ass2, acc1, acc2, int64(i*100), int64(i*100)*int64(i), timestamp, opStart+int64(i))
//tt.Assert.NoError(err)
if err != nil {
return
}
}
return
}
39 changes: 0 additions & 39 deletions services/horizon/internal/db2/history/synt_offer_id.go

This file was deleted.

1 change: 1 addition & 0 deletions services/horizon/internal/test/t.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//lint:file-ignore U1001 Ignore all unused code, thinks the code is unused because of the test skips
package test

import (
Expand Down
122 changes: 0 additions & 122 deletions services/horizon/internal/test/trades/main.go

This file was deleted.

0 comments on commit a7efa95

Please sign in to comment.