Skip to content

Commit

Permalink
Merge pull request ethereum#708 from thanhnguyennguyen/tomox-apis
Browse files Browse the repository at this point in the history
More tomoX rpc apis
  • Loading branch information
ngtuna authored Sep 17, 2019
2 parents cb350be + e2de39e commit c919f3c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
32 changes: 31 additions & 1 deletion internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,37 @@ web3._extend({
call: 'tomoX_getOrderNonce',
params: 1,
inputFormatter: [web3._extend.formatters.inputAddressFormatter]
})
}),
new web3._extend.Method({
name: 'getBestBid',
call: 'tomoX_getBestBid',
params: 1
}),
new web3._extend.Method({
name: 'getBestAsk',
call: 'tomoX_getBestAsk',
params: 1
}),
new web3._extend.Method({
name: 'GetBidTree',
call: 'tomoX_getBidTree',
params: 1
}),
new web3._extend.Method({
name: 'GetAskTree',
call: 'tomoX_getAskTree',
params: 1
}),
new web3._extend.Method({
name: 'getPendingOrders',
call: 'tomoX_getPendingOrders',
params: 1
}),
new web3._extend.Method({
name: 'getProcessedHashes',
call: 'tomoX_getProcessedHashes',
params: 0
}),
]
});
`
Expand Down
64 changes: 63 additions & 1 deletion tomox/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"math/big"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -371,8 +372,69 @@ func (api *PublicTomoXAPI) NewTopic(req Criteria) (string, error) {
return id, nil
}


// GetOrderNonce returns the latest orderNonce of the given address
func (api *PublicTomoXAPI) GetOrderNonce(address common.Address) (*big.Int, error) {
return api.t.GetOrderNonce(address)
}

// GetBestBid returns the bestBid price of the given pair
func (api *PublicTomoXAPI) GetBestBid(pairName string) (*big.Int, error) {
ob, err := api.t.getAndCreateIfNotExisted(pairName, false, common.Hash{})
if err != nil {
return big.NewInt(0), err
}
return ob.BestBid(false, common.Hash{}), nil
}

// GetBestAsk returns the bestAsk price of the given pair
func (api *PublicTomoXAPI) GetBestAsk(pairName string) (*big.Int, error) {
ob, err := api.t.getAndCreateIfNotExisted(pairName, false, common.Hash{})
if err != nil {
return big.NewInt(0), err
}
return ob.BestAsk(false, common.Hash{}), nil
}

// GetBidTree returns the bidTreeItem of the given pair
func (api *PublicTomoXAPI) GetBidTree(pairName string) (*OrderTreeItem, error) {
ob, err := api.t.getAndCreateIfNotExisted(pairName, false, common.Hash{})
if err != nil {
return nil, err
}
return ob.Bids.Item, nil
}

// GetAskTree returns the askTreeItem of the given pair
func (api *PublicTomoXAPI) GetAskTree(pairName string) (*OrderTreeItem, error) {
ob, err := api.t.getAndCreateIfNotExisted(pairName, false, common.Hash{})
if err != nil {
return nil, err
}
return ob.Asks.Item, nil
}

// GetPendingOrders returns pending orders of the given pair
func (api *PublicTomoXAPI) GetPendingOrders(pairName string) ([]*OrderItem, error) {
result := []*OrderItem{}
pending := api.t.getPendingOrders()
for _, p := range pending {
hash := p.hash
order := api.t.getOrderPending(hash)
if order != nil && strings.ToLower(order.PairName) == strings.ToLower(pairName) {
result = append(result, order)
}
}
return result, nil
}

// GetProcessedHashes returns hashes which already processed
func (api *PublicTomoXAPI) GetProcessedHashes() ([]common.Hash, error) {
result := []common.Hash{}
for _, val := range api.t.processedOrderCache.Keys() {
hash, ok := val.(common.Hash)
if ok {
result = append(result, hash)
}
}
return result, nil
}
2 changes: 1 addition & 1 deletion tomox/tomox.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func (tomox *TomoX) verifyOrderNonce(order *OrderItem) error {
func (tomox *TomoX) GetOrderNonce(address common.Address) (*big.Int, error) {
if len(tomox.orderNonce) == 0 {
if err := tomox.loadOrderNonce(); err != nil {
return big.NewInt(-1), err
return big.NewInt(0), nil
}
}
orderNonce, ok := tomox.orderNonce[address]
Expand Down

0 comments on commit c919f3c

Please sign in to comment.