Skip to content

Commit

Permalink
server/asset/eth: refactor coins
Browse files Browse the repository at this point in the history
Separate swapCoin from redeemCoin. Switch (*rpcclient).swap method to
return a *dexeth.SwapState.
  • Loading branch information
buck54321 committed Jan 12, 2022
1 parent f2c6e9f commit 8be5846
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 441 deletions.
2 changes: 1 addition & 1 deletion client/asset/eth/contractor.go
Expand Up @@ -112,7 +112,7 @@ func (c *contractorV0) initiate(txOpts *bind.TransactOpts, contracts []*asset.Co
RefundTimestamp: big.NewInt(int64(contract.LockTime)),
SecretHash: secretHash,
Participant: common.HexToAddress(contract.Address),
Value: new(big.Int).Mul(bigVal, dexeth.BigGweiFactor),
Value: new(big.Int).Mul(bigVal, big.NewInt(dexeth.GweiFactor)),
})
}

Expand Down
4 changes: 2 additions & 2 deletions client/asset/eth/eth_test.go
Expand Up @@ -1639,7 +1639,7 @@ func packInitiateDataV0(initiations []*dexeth.Initiation) ([]byte, error) {
RefundTimestamp: big.NewInt(init.LockTime.Unix()),
SecretHash: init.SecretHash,
Participant: init.Participant,
Value: new(big.Int).Mul(bigVal, dexeth.BigGweiFactor),
Value: new(big.Int).Mul(bigVal, big.NewInt(dexeth.GweiFactor)),
})
}
return (*dexeth.ABIs[0]).Pack("initiate", abiInitiations)
Expand Down Expand Up @@ -2411,7 +2411,7 @@ func ethToGwei(v uint64) uint64 {

func ethToWei(v uint64) *big.Int {
bigV := new(big.Int).SetUint64(ethToGwei(v))
return new(big.Int).Mul(bigV, dexeth.BigGweiFactor)
return new(big.Int).Mul(bigV, big.NewInt(dexeth.GweiFactor))
}

func TestPayFee(t *testing.T) {
Expand Down
22 changes: 0 additions & 22 deletions dex/networks/eth/common.go
Expand Up @@ -8,7 +8,6 @@ package eth

import (
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -29,24 +28,3 @@ func DecodeCoinID(coinID []byte) (common.Hash, error) {
// SecretHashSize is the byte-length of the hash of the secret key used in
// swaps.
const SecretHashSize = 32

// ToGwei converts a *big.Int in wei (1e18 unit) to gwei (1e9 unit) as a uint64.
// Errors if the amount of gwei is too big to fit fully into a uint64.
func ToGwei(wei *big.Int) (uint64, error) {
if wei.Cmp(new(big.Int)) == -1 {
return 0, fmt.Errorf("wei must be non-negative")
}
gweiFactorBig := big.NewInt(GweiFactor)
gwei := new(big.Int).Div(wei, gweiFactorBig)
if !gwei.IsUint64() {
return 0, fmt.Errorf("suggest gas price %v gwei is too big for a uint64", wei)
}
return gwei.Uint64(), nil
}

// ToWei converts a uint64 in gwei (1e9 unit) to wei (1e18 unit) as a *big.Int.
func ToWei(gwei uint64) *big.Int {
bigGwei := new(big.Int).SetUint64(gwei)
gweiFactorBig := big.NewInt(GweiFactor)
return new(big.Int).Mul(bigGwei, gweiFactorBig)
}
46 changes: 40 additions & 6 deletions dex/networks/eth/params.go
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"decred.org/dcrdex/dex"
v0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -36,9 +37,6 @@ var (
},
}

// BigGweiFactor is the *big.Int form of the GweiFactor.
BigGweiFactor = big.NewInt(GweiFactor)

VersionedGases = map[uint32]*dex.Gases{
0: v0Gases,
}
Expand Down Expand Up @@ -115,12 +113,31 @@ func RefundGas(contractVer uint32) uint64 {

// GweiToWei converts uint64 Gwei to *big.Int Wei.
func GweiToWei(v uint64) *big.Int {
return new(big.Int).Mul(big.NewInt(int64(v)), BigGweiFactor)
return new(big.Int).Mul(big.NewInt(int64(v)), big.NewInt(GweiFactor))
}

// GweiToWei converts *big.Int Wei to uint64 Gwei.
// WeiToGwei converts *big.Int Wei to uint64 Gwei. If v is determined to be
// unsuitable for a uint64, zero is returned.
func WeiToGwei(v *big.Int) uint64 {
return new(big.Int).Div(v, BigGweiFactor).Uint64()
vGwei := new(big.Int).Div(v, big.NewInt(GweiFactor))
if vGwei.IsUint64() {
return vGwei.Uint64()
}
return 0
}

// WeiToGweiUint64 converts a *big.Int in wei (1e18 unit) to gwei (1e9 unit) as
// a uint64. Errors if the amount of gwei is too big to fit fully into a uint64.
func WeiToGweiUint64(wei *big.Int) (uint64, error) {
if wei.Cmp(new(big.Int)) == -1 {
return 0, fmt.Errorf("wei must be non-negative")
}
gweiFactorBig := big.NewInt(GweiFactor)
gwei := new(big.Int).Div(wei, gweiFactorBig)
if !gwei.IsUint64() {
return 0, fmt.Errorf("suggest gas price %v gwei is too big for a uint64", wei)
}
return gwei.Uint64(), nil
}

// SwapStep is the state of a swap and corresponds to values in the Solidity
Expand Down Expand Up @@ -249,3 +266,20 @@ func VersionedNetworkToken(assetID uint32, contractVer uint32, net dex.Network)
}
return token, addrs.Address, contractAddr, nil
}

// SwapStateFromV0 converts a v0.ETHSwapSwap to a *SwapState.
func SwapStateFromV0(state *v0.ETHSwapSwap) *SwapState {
var blockTime int64
if state.RefundBlockTimestamp.IsInt64() {
blockTime = state.RefundBlockTimestamp.Int64()
}
return &SwapState{
BlockHeight: state.InitBlockNumber.Uint64(),
LockTime: time.Unix(blockTime, 0),
Secret: state.Secret,
Initiator: state.Initiator,
Participant: state.Participant,
Value: WeiToGwei(state.Value),
State: SwapStep(state.State),
}
}
2 changes: 1 addition & 1 deletion dex/networks/eth/txdata.go
Expand Up @@ -128,7 +128,7 @@ func (t *txDataHandlerV0) parseInitiateData(calldata []byte) (map[[SecretHashSiz

toReturn := make(map[[SecretHashSize]byte]*Initiation, len(initiations))
for _, init := range initiations {
gweiValue, err := ToGwei(init.Value)
gweiValue, err := WeiToGweiUint64(init.Value)
if err != nil {
return nil, fmt.Errorf("cannot convert wei to gwei: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion dex/networks/eth/txdata_test.go
Expand Up @@ -22,7 +22,7 @@ func packInitiateDataV0(initiations []*Initiation) ([]byte, error) {
RefundTimestamp: big.NewInt(init.LockTime.Unix()),
SecretHash: init.SecretHash,
Participant: init.Participant,
Value: new(big.Int).Mul(bigVal, BigGweiFactor),
Value: new(big.Int).Mul(bigVal, big.NewInt(GweiFactor)),
})
}
return (*ABIs[0]).Pack("initiate", abiInitiations)
Expand Down

0 comments on commit 8be5846

Please sign in to comment.