Skip to content

Commit

Permalink
Merge e63735b into b03109e
Browse files Browse the repository at this point in the history
  • Loading branch information
loongy committed Aug 10, 2018
2 parents b03109e + e63735b commit 2fa023b
Show file tree
Hide file tree
Showing 30 changed files with 1,273 additions and 333 deletions.
3 changes: 2 additions & 1 deletion cmd/darknode/darknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ func main() {
}

// New database for persistent storage
store, err := leveldb.NewStore(*dataParam, 72*time.Hour)
store, err := leveldb.NewStore(*dataParam, 25*time.Hour, time.Hour)
if err != nil {
log.Fatalf("cannot open leveldb: %v", err)
}
defer store.Release()
store.Prune()

midpointPriceStorer := leveldb.NewMidpointPriceStorer()

Expand Down
3 changes: 1 addition & 2 deletions cmd/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"math/big"
"os"

"github.com/republicprotocol/republic-go/contract"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/jbenet/go-base58"
"github.com/republicprotocol/republic-go/cmd/darknode/config"
"github.com/republicprotocol/republic-go/contract"
"github.com/republicprotocol/republic-go/crypto"
"github.com/republicprotocol/republic-go/identity"
"github.com/republicprotocol/republic-go/stackint"
Expand Down
19 changes: 16 additions & 3 deletions contract/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Binder struct {
republicToken *bindings.RepublicToken
darknodeRegistry *bindings.DarknodeRegistry
orderbook *bindings.Orderbook
renExSettlement *bindings.Settlement
renExSettlement *bindings.RenExSettlement
renExBalance *bindings.RenExBalances
erc20 *bindings.ERC20
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func NewBinder(auth *bind.TransactOpts, conn Conn) (Binder, error) {
return Binder{}, err
}

renExSettlement, err := bindings.NewSettlement(common.HexToAddress(conn.Config.RenExSettlementAddress), bind.ContractBackend(conn.Client))
renExSettlement, err := bindings.NewRenExSettlement(common.HexToAddress(conn.Config.RenExSettlementAddress), bind.ContractBackend(conn.Client))
if err != nil {
fmt.Println(fmt.Errorf("cannot bind to RenExSettlement: %v", err))
return Binder{}, err
Expand Down Expand Up @@ -217,6 +217,13 @@ func (binder *Binder) submitMatch(buy, sell order.ID) (*types.Transaction, error
return binder.renExSettlement.SubmitMatch(binder.transactOpts, buy, sell)
}

func (binder *Binder) GetMatchDetails(id order.ID) ([32]byte, [32]byte, *big.Int, *big.Int, uint32, uint32, error) {
binder.mu.RLock()
defer binder.mu.RUnlock()

return binder.renExSettlement.GetMatchDetails(binder.callOpts, id)
}

// Settle the order pair which gets confirmed by the Orderbook
func (binder *Binder) Settle(buy order.Order, sell order.Order) error {
binder.mu.Lock()
Expand All @@ -234,6 +241,11 @@ func (binder *Binder) Settle(buy order.Order, sell order.Order) error {
logger.Warn(fmt.Sprintf("cannot settle sell = %v: %v", sell.ID, sendTxErr))
}

// Check if it's already submitted
if _, _, highVol, lowVol, _, _, _ := binder.renExSettlement.GetMatchDetails(binder.callOpts, buy.ID); highVol.Cmp(big.NewInt(0)) != 0 || lowVol.Cmp(big.NewInt(0)) != 0 {
return fmt.Errorf("someone already settle the match, buy = %v, sell = %v", buy.ID, sell.ID)
}

// Submit match
tx, sendTxErr := binder.sendTx(func() (*types.Transaction, error) {
return binder.submitMatch(buy.ID, sell.ID)
Expand All @@ -243,7 +255,8 @@ func (binder *Binder) Settle(buy order.Order, sell order.Order) error {
}

// Wait for last transaction
if _, waitErr := binder.conn.PatchedWaitMined(context.Background(), tx); waitErr != nil {
_, waitErr := binder.conn.PatchedWaitMined(context.Background(), tx)
if waitErr != nil {
return fmt.Errorf("cannot wait to settle buy = %v, sell = %v: %v", buy.ID, sell.ID, waitErr)
}

Expand Down
7 changes: 6 additions & 1 deletion contract/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package contract

import (
"context"
"errors"
"fmt"
"math/big"
"time"
Expand Down Expand Up @@ -139,7 +140,11 @@ func (conn *Conn) PatchedWaitMined(ctx context.Context, tx *types.Transaction) (
time.Sleep(100 * time.Millisecond)
return nil, nil
default:
return bind.WaitMined(ctx, conn.Client, tx)
receipt, err := bind.WaitMined(ctx, conn.Client, tx)
if receipt.Status != types.ReceiptStatusSuccessful {
return receipt, errors.New("transaction reverted")
}
return receipt, err
}
}

Expand Down
Loading

0 comments on commit 2fa023b

Please sign in to comment.