Skip to content

Commit

Permalink
smarter balance
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Dec 1, 2021
1 parent 0bde1c1 commit 86dff7d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
57 changes: 54 additions & 3 deletions server/asset/eth/rpcclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)

// Check that rpcclient satisfies the ethFetcher interface.
var _ ethFetcher = (*rpcclient)(nil)
var (
_ ethFetcher = (*rpcclient)(nil)

bigZero = new(big.Int)
)

type rpcclient struct {
// ec wraps a *rpc.Client with some useful calls.
Expand Down Expand Up @@ -124,9 +129,55 @@ func (c *rpcclient) transaction(ctx context.Context, hash common.Hash) (tx *type
// accountBalance gets the account balance, including the effects of known
// unmined transactions.
func (c *rpcclient) accountBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
bigBal, err := c.ec.PendingBalanceAt(ctx, addr)
tip, err := c.blockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("blockNumber error: %v", err)
}
currentBal, err := c.ec.BalanceAt(ctx, addr, big.NewInt(int64(tip)))
if err != nil {
return nil, err
}
return bigBal, nil

var txs map[string]map[string]*RPCTransaction
err = c.c.CallContext(ctx, &txs, "txpool_contentFrom", addr)
if err != nil {
return nil, fmt.Errorf("contentFrom error: %w", err)
}

outgoing := new(big.Int)
for _, group := range txs { // 2 groups, pending and queued
for _, tx := range group {
outgoing.Add(outgoing, tx.Value.ToInt())
gas := new(big.Int).SetUint64(uint64(tx.Gas))
if tx.GasPrice != nil && tx.GasPrice.ToInt().Cmp(bigZero) > 0 {
outgoing.Add(outgoing, new(big.Int).Mul(gas, tx.GasPrice.ToInt()))
} else if tx.GasFeeCap != nil {
outgoing.Add(outgoing, new(big.Int).Mul(gas, tx.GasFeeCap.ToInt()))
}
}
}

return currentBal.Sub(currentBal, outgoing), nil
}

type RPCTransaction struct {
Value *hexutil.Big `json:"value"`
Gas hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"`
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
// BlockHash *common.Hash `json:"blockHash"`
// BlockNumber *hexutil.Big `json:"blockNumber"`
// From common.Address `json:"from"`
// GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
// Hash common.Hash `json:"hash"`
// Input hexutil.Bytes `json:"input"`
// Nonce hexutil.Uint64 `json:"nonce"`
// To *common.Address `json:"to"`
// TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
// Type hexutil.Uint64 `json:"type"`
// Accesses *types.AccessList `json:"accessList,omitempty"`
// ChainID *hexutil.Big `json:"chainId,omitempty"`
// V *hexutil.Big `json:"v"`
// R *hexutil.Big `json:"r"`
// S *hexutil.Big `json:"s"`
}
42 changes: 42 additions & 0 deletions server/asset/eth/rpcclient_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ package eth
import (
"errors"
"fmt"
"math/big"
"os"
"os/exec"
"path/filepath"

"context"
"testing"

"decred.org/dcrdex/dex/encode"
dexeth "decred.org/dcrdex/dex/networks/eth"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -24,6 +27,8 @@ var (
homeDir = os.Getenv("HOME")
ipc = filepath.Join(homeDir, "dextest/eth/alpha/node/geth.ipc")
contractAddrFile = filepath.Join(homeDir, "dextest", "eth", "contract_addr.txt")
alphaAddress = "18d65fb8d60c1199bb1ad381be47aa692b482605"
gammaAddress = "41293c2032bac60aa747374e966f79f575d42379"
ethClient = new(rpcclient)
ctx context.Context
)
Expand Down Expand Up @@ -136,3 +141,40 @@ func TestTransaction(t *testing.T) {
t.Fatal(err)
}
}

func TestAccountBalance(t *testing.T) {
addr := common.HexToAddress(alphaAddress)
const vGwei = 1e7

balBefore, err := ethClient.accountBalance(ctx, addr)
if err != nil {
t.Fatalf("accountBalance error: %v", err)
}

if err := tmuxSend(alphaAddress, gammaAddress, vGwei); err != nil {
t.Fatalf("send error: %v", err)
}

balAfter, err := ethClient.accountBalance(ctx, addr)
if err != nil {
t.Fatalf("accountBalance error: %v", err)
}

diff := new(big.Int).Sub(balBefore, balAfter)
if diff.Cmp(dexeth.GweiToWei(vGwei)) <= 0 {
t.Fatalf("account balance changed by %d. expected > %d", dexeth.WeiToGwei(diff), uint64(vGwei))
}
}

func tmuxRun(cmd string) error {
cmd += "; tmux wait-for -S harnessdone"
err := exec.Command("tmux", "send-keys", "-t", "eth-harness:0", cmd, "C-m").Run() // ; wait-for harnessdone
if err != nil {
return nil
}
return exec.Command("tmux", "wait-for", "harnessdone").Run()
}

func tmuxSend(from, to string, v uint64) error {
return tmuxRun(fmt.Sprintf("./alpha attach --preload send.js --exec \"send(\\\"%s\\\",\\\"%s\\\",%s)\"", from, to, dexeth.GweiToWei(v)))
}

0 comments on commit 86dff7d

Please sign in to comment.