Skip to content

Commit 98f845d

Browse files
authored
eth: v1 contract (#2038)
* 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".
1 parent 9848bb0 commit 98f845d

72 files changed

Lines changed: 4819 additions & 2367 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ client/tor/build
5252
server/cmd/geogame/geogame
5353
internal/cmd/xmrswap/xmrswap
5454
internal/cmd/xmrswap/config.json
55+
internal/libsecp256k1/secp256k1
56+
server/cmd/dcrdex/evm-protocol-overrides.json

client/asset/btc/btc_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,7 @@ func testAvailableFund(t *testing.T, segwit bool, walletType string) {
21972197
}
21982198

21992199
ord := &asset.Order{
2200-
Version: version,
2200+
AssetVersion: version,
22012201
Value: 0,
22022202
MaxSwapCount: 1,
22032203
MaxFeeRate: tBTC.MaxFeeRate,
@@ -2762,7 +2762,7 @@ func TestFundEdges(t *testing.T) {
27622762
unspents := []*ListUnspentResult{p2pkhUnspent}
27632763
node.listUnspent = unspents
27642764
ord := &asset.Order{
2765-
Version: version,
2765+
AssetVersion: version,
27662766
Value: swapVal,
27672767
MaxSwapCount: lots,
27682768
MaxFeeRate: tBTC.MaxFeeRate,
@@ -2986,7 +2986,7 @@ func TestFundEdgesSegwit(t *testing.T) {
29862986
unspents := []*ListUnspentResult{p2wpkhUnspent}
29872987
node.listUnspent = unspents
29882988
ord := &asset.Order{
2989-
Version: version,
2989+
AssetVersion: version,
29902990
Value: swapVal,
29912991
MaxSwapCount: lots,
29922992
MaxFeeRate: tBTC.MaxFeeRate,
@@ -4365,7 +4365,7 @@ func testPreSwap(t *testing.T, segwit bool, walletType string) {
43654365
}
43664366

43674367
form := &asset.PreSwapForm{
4368-
Version: version,
4368+
AssetVersion: version,
43694369
LotSize: tLotSize,
43704370
Lots: lots,
43714371
MaxFeeRate: tBTC.MaxFeeRate,
@@ -4411,8 +4411,8 @@ func testPreRedeem(t *testing.T, segwit bool, walletType string) {
44114411
defer shutdown()
44124412

44134413
preRedeem, err := wallet.PreRedeem(&asset.PreRedeemForm{
4414-
Version: version,
4415-
Lots: 5,
4414+
AssetVersion: version,
4415+
Lots: 5,
44164416
})
44174417
// Shouldn't actually be any path to error.
44184418
if err != nil {

client/asset/btc/livetest/livetest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func Run(t *testing.T, cfg *Config) {
266266
checkAmt("second", rig.secondWallet)
267267

268268
ord := &asset.Order{
269-
Version: 0,
269+
AssetVersion: 0,
270270
Value: contractValue * 3,
271271
MaxSwapCount: lots * 3,
272272
MaxFeeRate: cfg.Asset.MaxFeeRate,

client/asset/btc/simnet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func testWalletTxBalanceSync(t *testing.T, fromWallet, toWallet *ExchangeWalletF
367367
}
368368

369369
order := &asset.Order{
370-
Value: toSatoshi(1),
370+
AssetVersion: toSatoshi(1),
371371
FeeSuggestion: 10,
372372
MaxSwapCount: 1,
373373
MaxFeeRate: 20,

client/asset/dcr/dcr_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ func TestAvailableFund(t *testing.T) {
876876
}
877877

878878
ord := &asset.Order{
879-
Version: version,
879+
AssetVersion: version,
880880
Value: 0,
881881
MaxSwapCount: 1,
882882
MaxFeeRate: tDCR.MaxFeeRate,
@@ -2595,7 +2595,7 @@ func TestFundEdges(t *testing.T) {
25952595

25962596
node.unspent = []walletjson.ListUnspentResult{p2pkhUnspent}
25972597
ord := &asset.Order{
2598-
Version: version,
2598+
AssetVersion: version,
25992599
Value: swapVal,
26002600
MaxSwapCount: lots,
26012601
MaxFeeRate: tDCR.MaxFeeRate,
@@ -3871,7 +3871,7 @@ func TestPreSwap(t *testing.T) {
38713871
node.unspent = []walletjson.ListUnspentResult{p2pkhUnspent}
38723872

38733873
form := &asset.PreSwapForm{
3874-
Version: version,
3874+
AssetVersion: version,
38753875
LotSize: tLotSize,
38763876
Lots: lots,
38773877
MaxFeeRate: tDCR.MaxFeeRate,
@@ -3913,8 +3913,8 @@ func TestPreRedeem(t *testing.T) {
39133913
defer shutdown()
39143914

39153915
preRedeem, err := wallet.PreRedeem(&asset.PreRedeemForm{
3916-
Version: version,
3917-
Lots: 5,
3916+
AssetVersion: version,
3917+
Lots: 5,
39183918
})
39193919
// Shouldn't actually be any path to error.
39203920
if err != nil {

client/asset/dcr/simnet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func runTest(t *testing.T, splitTx bool) {
348348
}
349349

350350
ord := &asset.Order{
351-
Version: tDCR.Version,
351+
AssetVersion: tDCR.Version,
352352
Value: contractValue * 3,
353353
MaxSwapCount: lots * 3,
354354
MaxFeeRate: tDCR.MaxFeeRate,

client/asset/driver.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
// networks enables filtering out tokens via the package's SetNetwork.
1717
type nettedToken struct {
1818
*Token
19-
addrs map[dex.Network]string
19+
erc20NetAddrs map[dex.Network]string
20+
netSupportedAssetVersions map[dex.Network][]uint32
2021
}
2122

2223
var (
@@ -85,7 +86,13 @@ func Register(assetID uint32, driver Driver) {
8586
// RegisterToken should be called to register tokens. If no nets are specified
8687
// the token will be registered for all networks. The user must invoke
8788
// SetNetwork to enable net-based filtering of package function output.
88-
func RegisterToken(tokenID uint32, token *dex.Token, walletDef *WalletDefinition, addrs map[dex.Network]string) {
89+
func RegisterToken(
90+
tokenID uint32,
91+
token *dex.Token,
92+
walletDef *WalletDefinition,
93+
erc20NetAddrs map[dex.Network]string,
94+
netSupportedAssetVersions map[dex.Network][]uint32,
95+
) {
8996
driversMtx.Lock()
9097
defer driversMtx.Unlock()
9198
if _, exists := tokens[tokenID]; exists {
@@ -101,7 +108,8 @@ func RegisterToken(tokenID uint32, token *dex.Token, walletDef *WalletDefinition
101108
Definition: walletDef,
102109
// ContractAddress specified in SetNetwork.
103110
},
104-
addrs: addrs,
111+
erc20NetAddrs: erc20NetAddrs,
112+
netSupportedAssetVersions: netSupportedAssetVersions,
105113
}
106114
}
107115

@@ -262,12 +270,13 @@ func UnitInfo(assetID uint32) (dex.UnitInfo, error) {
262270
// network. SetNetwork need only be called once during initialization.
263271
func SetNetwork(net dex.Network) {
264272
for assetID, nt := range tokens {
265-
addr, exists := nt.addrs[net]
273+
addr, exists := nt.erc20NetAddrs[net]
266274
if !exists {
267275
delete(tokens, assetID)
268276
continue
269277
}
270278
nt.Token.ContractAddress = addr
279+
nt.Token.SupportedAssetVersions = nt.netSupportedAssetVersions[net]
271280
}
272281
}
273282

client/asset/estimation.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ type RedeemEstimate struct {
3838

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

8485
// PreRedeemForm can be used to get a redemption estimate.
8586
type PreRedeemForm struct {
86-
// Version is the asset version. Most backends only support one version.
87-
Version uint32
87+
// AssetVersion is the asset version. Most backends only support one
88+
// version.
89+
AssetVersion uint32
8890
// Lots is the number of lots in the order.
8991
Lots uint64
9092
// FeeSuggestion is a suggested fee from the server.

client/asset/eth/cmd/getgas/main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func mainErr() error {
4848
flag.BoolVar(&useMainnet, "mainnet", false, "use mainnet")
4949
flag.BoolVar(&useTestnet, "testnet", false, "use testnet")
5050
flag.BoolVar(&useSimnet, "simnet", false, "use simnet")
51-
flag.BoolVar(&trace, "trace", false, "use simnet")
52-
flag.BoolVar(&debug, "debug", false, "use simnet")
51+
flag.BoolVar(&trace, "trace", false, "use trace logging")
52+
flag.BoolVar(&debug, "debug", false, "use debug logging")
5353
flag.IntVar(&maxSwaps, "n", 5, "max number of swaps per transaction. minimum is 2. test will run from 2 swap up to n swaps.")
5454
flag.StringVar(&chain, "chain", "eth", "symbol of the base chain")
5555
flag.StringVar(&token, "token", "", "symbol of the token. if token is not specified, will check gas for base chain")
@@ -125,7 +125,8 @@ func mainErr() error {
125125

126126
wParams := new(eth.GetGasWalletParams)
127127
wParams.BaseUnitInfo = bui
128-
if token != chain {
128+
isToken := token != chain
129+
if isToken {
129130
var exists bool
130131
tkn, exists := tokens[assetID]
131132
if !exists {
@@ -139,16 +140,18 @@ func mainErr() error {
139140
}
140141
swapContract, exists := netToken.SwapContracts[contractVer]
141142
if !exists {
142-
return nil, fmt.Errorf("no verion %d contract for %s token on %s network %s", contractVer, tkn.Name, chain, net)
143+
return nil, fmt.Errorf("no version %d contract for %s token on %s network %s", contractVer, tkn.Name, chain, net)
143144
}
144145
wParams.Gas = &swapContract.Gas
145146
} else {
146147
wParams.UnitInfo = bui
147148
g, exists := gases[contractVer]
148149
if !exists {
149-
return nil, fmt.Errorf("no verion %d contract for %s network %s", contractVer, chain, net)
150+
return nil, fmt.Errorf("no version %d contract for %s network %s", contractVer, chain, net)
150151
}
151152
wParams.Gas = g
153+
}
154+
if !isToken || contractVer == 1 {
152155
cs, exists := contracts[contractVer]
153156
if !exists {
154157
return nil, fmt.Errorf("no version %d base chain swap contract on %s", contractVer, chain)

0 commit comments

Comments
 (0)