Skip to content

Commit

Permalink
eth: v1 contract (#2038)
Browse files Browse the repository at this point in the history
* eth v1 contract

For v1, the locator encodes all of the immutable data that defines the
swap. This immutable data is now collected in something called
a "vector" (dexeth.SwapVector). For version 0, some vector data
is stored on-chain indexed by the secret hash. For version 1, all
vector data is encoded in the locator.

I've also made an effort to standardize the use of status/step,
and eliminated the use of ambiguous "ver" variables throughout.
A "status" is now the collection of mutable contract data: the step,
the init block height, and the secret. The status and vector
collectively fully characterize the swap.

client/asset/eth:
New contractV1 and tokenContractorV1 interfaces. To avoid duplication,
the ERC20 parts of the tokenContractors are separated into a new type
erc20Contractor that is embedded by both versions. Getters for
status and vector are added in place of the old method "swap".
  • Loading branch information
buck54321 authored Feb 18, 2025
1 parent 9848bb0 commit 98f845d
Show file tree
Hide file tree
Showing 72 changed files with 4,819 additions and 2,367 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ client/tor/build
server/cmd/geogame/geogame
internal/cmd/xmrswap/xmrswap
internal/cmd/xmrswap/config.json
internal/libsecp256k1/secp256k1
server/cmd/dcrdex/evm-protocol-overrides.json
12 changes: 6 additions & 6 deletions client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2197,7 +2197,7 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
}

ord := &asset.Order{
Version: version,
AssetVersion: version,
Value: 0,
MaxSwapCount: 1,
MaxFeeRate: tBTC.MaxFeeRate,
Expand Down Expand Up @@ -2762,7 +2762,7 @@ func TestFundEdges(t *testing.T) {
unspents := []*ListUnspentResult{p2pkhUnspent}
node.listUnspent = unspents
ord := &asset.Order{
Version: version,
AssetVersion: version,
Value: swapVal,
MaxSwapCount: lots,
MaxFeeRate: tBTC.MaxFeeRate,
Expand Down Expand Up @@ -2986,7 +2986,7 @@ func TestFundEdgesSegwit(t *testing.T) {
unspents := []*ListUnspentResult{p2wpkhUnspent}
node.listUnspent = unspents
ord := &asset.Order{
Version: version,
AssetVersion: version,
Value: swapVal,
MaxSwapCount: lots,
MaxFeeRate: tBTC.MaxFeeRate,
Expand Down Expand Up @@ -4365,7 +4365,7 @@ func testPreSwap(t *testing.T, segwit bool, walletType string) {
}

form := &asset.PreSwapForm{
Version: version,
AssetVersion: version,
LotSize: tLotSize,
Lots: lots,
MaxFeeRate: tBTC.MaxFeeRate,
Expand Down Expand Up @@ -4411,8 +4411,8 @@ func testPreRedeem(t *testing.T, segwit bool, walletType string) {
defer shutdown()

preRedeem, err := wallet.PreRedeem(&asset.PreRedeemForm{
Version: version,
Lots: 5,
AssetVersion: version,
Lots: 5,
})
// Shouldn't actually be any path to error.
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/asset/btc/livetest/livetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func Run(t *testing.T, cfg *Config) {
checkAmt("second", rig.secondWallet)

ord := &asset.Order{
Version: 0,
AssetVersion: 0,
Value: contractValue * 3,
MaxSwapCount: lots * 3,
MaxFeeRate: cfg.Asset.MaxFeeRate,
Expand Down
2 changes: 1 addition & 1 deletion client/asset/btc/simnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func testWalletTxBalanceSync(t *testing.T, fromWallet, toWallet *ExchangeWalletF
}

order := &asset.Order{
Value: toSatoshi(1),
AssetVersion: toSatoshi(1),
FeeSuggestion: 10,
MaxSwapCount: 1,
MaxFeeRate: 20,
Expand Down
10 changes: 5 additions & 5 deletions client/asset/dcr/dcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ func TestAvailableFund(t *testing.T) {
}

ord := &asset.Order{
Version: version,
AssetVersion: version,
Value: 0,
MaxSwapCount: 1,
MaxFeeRate: tDCR.MaxFeeRate,
Expand Down Expand Up @@ -2595,7 +2595,7 @@ func TestFundEdges(t *testing.T) {

node.unspent = []walletjson.ListUnspentResult{p2pkhUnspent}
ord := &asset.Order{
Version: version,
AssetVersion: version,
Value: swapVal,
MaxSwapCount: lots,
MaxFeeRate: tDCR.MaxFeeRate,
Expand Down Expand Up @@ -3871,7 +3871,7 @@ func TestPreSwap(t *testing.T) {
node.unspent = []walletjson.ListUnspentResult{p2pkhUnspent}

form := &asset.PreSwapForm{
Version: version,
AssetVersion: version,
LotSize: tLotSize,
Lots: lots,
MaxFeeRate: tDCR.MaxFeeRate,
Expand Down Expand Up @@ -3913,8 +3913,8 @@ func TestPreRedeem(t *testing.T) {
defer shutdown()

preRedeem, err := wallet.PreRedeem(&asset.PreRedeemForm{
Version: version,
Lots: 5,
AssetVersion: version,
Lots: 5,
})
// Shouldn't actually be any path to error.
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/asset/dcr/simnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func runTest(t *testing.T, splitTx bool) {
}

ord := &asset.Order{
Version: tDCR.Version,
AssetVersion: tDCR.Version,
Value: contractValue * 3,
MaxSwapCount: lots * 3,
MaxFeeRate: tDCR.MaxFeeRate,
Expand Down
17 changes: 13 additions & 4 deletions client/asset/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
// networks enables filtering out tokens via the package's SetNetwork.
type nettedToken struct {
*Token
addrs map[dex.Network]string
erc20NetAddrs map[dex.Network]string
netSupportedAssetVersions map[dex.Network][]uint32
}

var (
Expand Down Expand Up @@ -85,7 +86,13 @@ func Register(assetID uint32, driver Driver) {
// RegisterToken should be called to register tokens. If no nets are specified
// the token will be registered for all networks. The user must invoke
// SetNetwork to enable net-based filtering of package function output.
func RegisterToken(tokenID uint32, token *dex.Token, walletDef *WalletDefinition, addrs map[dex.Network]string) {
func RegisterToken(
tokenID uint32,
token *dex.Token,
walletDef *WalletDefinition,
erc20NetAddrs map[dex.Network]string,
netSupportedAssetVersions map[dex.Network][]uint32,
) {
driversMtx.Lock()
defer driversMtx.Unlock()
if _, exists := tokens[tokenID]; exists {
Expand All @@ -101,7 +108,8 @@ func RegisterToken(tokenID uint32, token *dex.Token, walletDef *WalletDefinition
Definition: walletDef,
// ContractAddress specified in SetNetwork.
},
addrs: addrs,
erc20NetAddrs: erc20NetAddrs,
netSupportedAssetVersions: netSupportedAssetVersions,
}
}

Expand Down Expand Up @@ -262,12 +270,13 @@ func UnitInfo(assetID uint32) (dex.UnitInfo, error) {
// network. SetNetwork need only be called once during initialization.
func SetNetwork(net dex.Network) {
for assetID, nt := range tokens {
addr, exists := nt.addrs[net]
addr, exists := nt.erc20NetAddrs[net]
if !exists {
delete(tokens, assetID)
continue
}
nt.Token.ContractAddress = addr
nt.Token.SupportedAssetVersions = nt.netSupportedAssetVersions[net]
}
}

Expand Down
10 changes: 6 additions & 4 deletions client/asset/estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type RedeemEstimate struct {

// PreSwapForm can be used to get a swap fees estimate.
type PreSwapForm struct {
// Version is the asset version. Most backends only support one version.
Version uint32
// AssetVersion is the server's asset version. Most backends only support
// one version.
AssetVersion uint32
// LotSize is the lot size for the calculation. For quote assets, LotSize
// should be based on either the user's limit order rate, or some measure
// of the current market rate.
Expand Down Expand Up @@ -83,8 +84,9 @@ type PreSwap struct {

// PreRedeemForm can be used to get a redemption estimate.
type PreRedeemForm struct {
// Version is the asset version. Most backends only support one version.
Version uint32
// AssetVersion is the asset version. Most backends only support one
// version.
AssetVersion uint32
// Lots is the number of lots in the order.
Lots uint64
// FeeSuggestion is a suggested fee from the server.
Expand Down
13 changes: 8 additions & 5 deletions client/asset/eth/cmd/getgas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func mainErr() error {
flag.BoolVar(&useMainnet, "mainnet", false, "use mainnet")
flag.BoolVar(&useTestnet, "testnet", false, "use testnet")
flag.BoolVar(&useSimnet, "simnet", false, "use simnet")
flag.BoolVar(&trace, "trace", false, "use simnet")
flag.BoolVar(&debug, "debug", false, "use simnet")
flag.BoolVar(&trace, "trace", false, "use trace logging")
flag.BoolVar(&debug, "debug", false, "use debug logging")
flag.IntVar(&maxSwaps, "n", 5, "max number of swaps per transaction. minimum is 2. test will run from 2 swap up to n swaps.")
flag.StringVar(&chain, "chain", "eth", "symbol of the base chain")
flag.StringVar(&token, "token", "", "symbol of the token. if token is not specified, will check gas for base chain")
Expand Down Expand Up @@ -125,7 +125,8 @@ func mainErr() error {

wParams := new(eth.GetGasWalletParams)
wParams.BaseUnitInfo = bui
if token != chain {
isToken := token != chain
if isToken {
var exists bool
tkn, exists := tokens[assetID]
if !exists {
Expand All @@ -139,16 +140,18 @@ func mainErr() error {
}
swapContract, exists := netToken.SwapContracts[contractVer]
if !exists {
return nil, fmt.Errorf("no verion %d contract for %s token on %s network %s", contractVer, tkn.Name, chain, net)
return nil, fmt.Errorf("no version %d contract for %s token on %s network %s", contractVer, tkn.Name, chain, net)
}
wParams.Gas = &swapContract.Gas
} else {
wParams.UnitInfo = bui
g, exists := gases[contractVer]
if !exists {
return nil, fmt.Errorf("no verion %d contract for %s network %s", contractVer, chain, net)
return nil, fmt.Errorf("no version %d contract for %s network %s", contractVer, chain, net)
}
wParams.Gas = g
}
if !isToken || contractVer == 1 {
cs, exists := contracts[contractVer]
if !exists {
return nil, fmt.Errorf("no version %d base chain swap contract on %s", contractVer, chain)
Expand Down
Loading

0 comments on commit 98f845d

Please sign in to comment.