Skip to content

Commit

Permalink
add query option to getTokens api
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Jul 18, 2019
1 parent 98a01c4 commit e913f53
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

## latest
IMPROVEMENTS
* [\#53](https://github.com/binance-chain/go-sdk/pull/53) [SOURCE] change the default source into 0
* [\#53](https://github.com/binance-chain/go-sdk/pull/53) [SOURCE] change the default source into 0

## BUF FIX
* [\#57](https://github.com/binance-chain/go-sdk/pull/57) [API] add query option to getTokens api
8 changes: 6 additions & 2 deletions client/query/get_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package query
import (
"encoding/json"

"github.com/binance-chain/go-sdk/common"
"github.com/binance-chain/go-sdk/common/types"
)

// GetTokens returns list of tokens
func (c *client) GetTokens() ([]types.Token, error) {
qp := map[string]string{}
func (c *client) GetTokens(query *types.OrdersQuery) ([]types.Token, error) {
qp, err := common.QueryParamToMap(*query)
if err != nil {
return nil, err
}
resp, _, err := c.baseClient.Get("/tokens", qp)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion client/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type QueryClient interface {
GetTrades(query *types.TradesQuery) (*types.Trades, error)
GetAccount(string) (*types.BalanceAccount, error)
GetTime() (*types.Time, error)
GetTokens() ([]types.Token, error)
GetTokens(query *types.OrdersQuery) ([]types.Token, error)
GetNodeInfo() (*types.ResultStatus, error)
}

Expand Down
27 changes: 27 additions & 0 deletions common/types/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,30 @@ func (param *MarketsQuery) Check() error {
}
return nil
}

// OrdersQuery definition
type OrdersQuery struct {
Offset *uint32 `json:"offset,omitempty,string"` //Option
Limit *uint32 `json:"limit,omitempty,string"` //Option
}

func NewOrdersQuery() *OrdersQuery {
return &OrdersQuery{}
}

func (param *OrdersQuery) WithOffset(offset uint32) *OrdersQuery {
param.Offset = &offset
return param
}

func (param *OrdersQuery) WithLimit(limit uint32) *OrdersQuery {
param.Limit = &limit
return param
}

func (param *OrdersQuery) Check() error {
if param.Limit != nil && *param.Limit <= 0 {
return LimitOutOfRangeError
}
return nil
}
34 changes: 17 additions & 17 deletions e2e/e2e_trans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestTransProcess(t *testing.T) {
assert.True(t, len(ticker24h) > 0)

//----- Get Tokens -----------
tokens, err := client.GetTokens()
tokens, err := client.GetTokens(ctypes.NewOrdersQuery().WithLimit(101))
assert.NoError(t, err)
fmt.Printf("GetTokens: %v \n", tokens)

Expand All @@ -84,35 +84,35 @@ func TestTransProcess(t *testing.T) {
fmt.Printf("Get time: %v \n", time)

//----- time lock -----------
lockResult,err:=client.TimeLock("test lock",ctypes.Coins{{"BNB",100000000}},int64(time2.Now().Add(65*time2.Second).Unix()),true)
assert.NoError(t,err)
fmt.Printf("timelock %d",lockResult.LockId)
lockResult, err := client.TimeLock("test lock", ctypes.Coins{{"BNB", 100000000}}, int64(time2.Now().Add(65*time2.Second).Unix()), true)
assert.NoError(t, err)
fmt.Printf("timelock %d", lockResult.LockId)

//----- time relock ---------
relockResult,err:=client.TimeReLock(lockResult.LockId,"test lock",ctypes.Coins{{"BNB",200000000}},int64(time2.Now().Add(65*time2.Second).Unix()),true)
assert.NoError(t,err)
fmt.Printf("timelock %d",relockResult.LockId)
relockResult, err := client.TimeReLock(lockResult.LockId, "test lock", ctypes.Coins{{"BNB", 200000000}}, int64(time2.Now().Add(65*time2.Second).Unix()), true)
assert.NoError(t, err)
fmt.Printf("timelock %d", relockResult.LockId)

//------ time unlock --------
time2.Sleep(70*time2.Second)
unlockResult,err:= client.TimeUnLock(relockResult.LockId,true)
assert.NoError(t,err)
fmt.Printf("timelock %d",unlockResult.LockId)
time2.Sleep(70 * time2.Second)
unlockResult, err := client.TimeUnLock(relockResult.LockId, true)
assert.NoError(t, err)
fmt.Printf("timelock %d", unlockResult.LockId)

//----- Create order -----------
createOrderResult, err := client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithSource(100),transaction.WithMemo("test memo"))
createOrderResult, err := client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithSource(100), transaction.WithMemo("test memo"))
assert.NoError(t, err)
assert.True(t, true, createOrderResult.Ok)

//---- Create order by sequence --
acc,err:=client.GetAccount(client.GetKeyManager().GetAddr().String())
assert.NoError(t,err)
acc, err := client.GetAccount(client.GetKeyManager().GetAddr().String())
assert.NoError(t, err)

_, err = client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithAcNumAndSequence(acc.Number,acc.Sequence))
_, err = client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithAcNumAndSequence(acc.Number, acc.Sequence))
assert.NoError(t, err)
_, err = client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithAcNumAndSequence(acc.Number,acc.Sequence+1))
_, err = client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithAcNumAndSequence(acc.Number, acc.Sequence+1))
assert.NoError(t, err)
_, err = client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithAcNumAndSequence(acc.Number,acc.Sequence+2))
_, err = client.CreateOrder(tradeSymbol, nativeSymbol, msg.OrderSide.BUY, 100000000, 100000000, true, transaction.WithAcNumAndSequence(acc.Number, acc.Sequence+2))
assert.NoError(t, err)

//---- Get Open Order ---------
Expand Down

0 comments on commit e913f53

Please sign in to comment.