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 8, 2022
1 parent 52652b0 commit 40b0d90
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 404 deletions.
24 changes: 23 additions & 1 deletion dex/networks/eth/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

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

Expand Down Expand Up @@ -130,7 +131,11 @@ func GweiToWei(v uint64) *big.Int {

// GweiToWei converts *big.Int Wei to uint64 Gwei.
func WeiToGwei(v *big.Int) uint64 {
return new(big.Int).Div(v, BigGweiFactor).Uint64()
vGwei := new(big.Int).Div(v, BigGweiFactor)
if vGwei.IsUint64() {
return vGwei.Uint64()
}
return 0
}

// SwapStep is the state of a swap and corresponds to values in the Solidity
Expand Down Expand Up @@ -197,3 +202,20 @@ type Redemption struct {
Secret [32]byte
SecretHash [32]byte
}

// 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),
}
}
Loading

0 comments on commit 40b0d90

Please sign in to comment.