Skip to content

Commit

Permalink
Merge pull request #41 from vegaprotocol/release/v0.36.0
Browse files Browse the repository at this point in the history
Release v0.36.0
  • Loading branch information
ashleyvega authored May 15, 2021
2 parents 55f5978 + c9f7146 commit b310e83
Show file tree
Hide file tree
Showing 9 changed files with 781 additions and 450 deletions.
1 change: 1 addition & 0 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
type Bot interface {
Start() error
Stop()
GetTraderDetails() string
}

// PricingEngine is the source of price information from the price proxy.
Expand Down
139 changes: 139 additions & 0 deletions bot/normal/datarequests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package normal

import (
"fmt"

"github.com/pkg/errors"
"github.com/vegaprotocol/api/grpc/clients/go/generated/code.vegaprotocol.io/vega/proto"
"github.com/vegaprotocol/api/grpc/clients/go/generated/code.vegaprotocol.io/vega/proto/api"
)

// As the streaming service only gives us data when it changes
// we need to look up the initial data manually
func (b *Bot) lookupInitialValues() error {
// Collateral
err := b.getAccountGeneral()
if err != nil {
return fmt.Errorf("Failed to get general account details: %w", err)
}
err = b.getAccountMargin()
if err != nil {
return fmt.Errorf("Failed to get margin account details: %w", err)
}
err = b.getAccountBond()
if err != nil {
return fmt.Errorf("Failed to get bond account details: %w", err)
}

// Market data (currentPrice, auction etc)
err = b.getMarketData()
if err != nil {
return fmt.Errorf("Failed to get market data: %w", err)
}

// Position
positions, err := b.getPositions()
if err != nil {
return fmt.Errorf("Failed to get position details: %w", err)
}

// If we have not traded yet then we won't have a position
if positions == nil {
b.openVolume = 0
} else {
if len(positions) != 1 {
return errors.New("One position item required")
}
b.openVolume = positions[0].OpenVolume
}
return nil
}

// getAccountGeneral get this bot's general account balance.
func (b *Bot) getAccountGeneral() error {
response, err := b.node.PartyAccounts(&api.PartyAccountsRequest{
// MarketId: general account is not per market
PartyId: b.walletPubKeyHex,
Asset: b.settlementAssetID,
Type: proto.AccountType_ACCOUNT_TYPE_GENERAL,
})
if err != nil {
return errors.Wrap(err, "failed to get general account")
}
if len(response.Accounts) == 0 {
return errors.Wrap(err, "found zero general accounts for party")
}
if len(response.Accounts) > 1 {
return fmt.Errorf("found too many general accounts for party: %d", len(response.Accounts))
}
b.balanceGeneral = response.Accounts[0].Balance
return nil
}

// getAccountMargin get this bot's margin account balance.
func (b *Bot) getAccountMargin() error {
response, err := b.node.PartyAccounts(&api.PartyAccountsRequest{
PartyId: b.walletPubKeyHex,
MarketId: b.market.Id,
Asset: b.settlementAssetID,
Type: proto.AccountType_ACCOUNT_TYPE_MARGIN,
})
if err != nil {
return errors.Wrap(err, "failed to get margin account")
}
if len(response.Accounts) == 0 {
return errors.Wrap(err, "found zero margin accounts for party")
}
if len(response.Accounts) > 1 {
return fmt.Errorf("found too many margin accounts for party: %d", len(response.Accounts))
}
b.balanceMargin = response.Accounts[0].Balance
return nil
}

// getAccountBond get this bot's bond account balance.
func (b *Bot) getAccountBond() error {
b.balanceBond = 0
response, err := b.node.PartyAccounts(&api.PartyAccountsRequest{
PartyId: b.walletPubKeyHex,
MarketId: b.market.Id,
Asset: b.settlementAssetID,
Type: proto.AccountType_ACCOUNT_TYPE_BOND,
})
if err != nil {
return errors.Wrap(err, "failed to get bond account")
}
if len(response.Accounts) == 0 {
return errors.Wrap(err, "found zero bond accounts for party")
}
if len(response.Accounts) > 1 {
return fmt.Errorf("found too many bond accounts for party: %d", len(response.Accounts))
}
b.balanceBond = response.Accounts[0].Balance
return nil
}

// getPositions get this bot's positions.
func (b *Bot) getPositions() ([]*proto.Position, error) {
response, err := b.node.PositionsByParty(&api.PositionsByPartyRequest{
PartyId: b.walletPubKeyHex,
MarketId: b.market.Id,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get positions by party")
}
return response.Positions, nil
}

// getMarketData gets the latest info about the market
func (b *Bot) getMarketData() error {
response, err := b.node.MarketDataByID(&api.MarketDataByIDRequest{
MarketId: b.market.Id,
})
if err != nil {
return errors.Wrap(err, "Failed to get market data by id")
}
b.marketData = response.MarketData
b.currentPrice = response.MarketData.StaticMidPrice
return nil
}
58 changes: 58 additions & 0 deletions bot/normal/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package normal

import (
"encoding/hex"
"fmt"
"time"

"github.com/pkg/errors"
)

func hexToRaw(hexBytes []byte) ([]byte, error) {
raw := make([]byte, hex.DecodedLen(len(hexBytes)))
n, err := hex.Decode(raw, hexBytes)
if err != nil {
return nil, errors.Wrap(err, "failed to decode hex")
}
if n != len(raw) {
return nil, fmt.Errorf("failed to decode hex: decoded %d bytes, expected to decode %d bytes", n, len(raw))
}
return raw, nil
}

func max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}

func min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}

func abs(a int64) int64 {
if a < 0 {
return -a
}
return a
}

func doze(d time.Duration, stop chan bool) error {
interval := 100 * time.Millisecond
for d > interval {
select {
case <-stop:
return errors.New("doze interrupted")

default:
time.Sleep(interval)
d -= interval
}
}
time.Sleep(d)
return nil
}
Loading

0 comments on commit b310e83

Please sign in to comment.