Skip to content

Commit 4453f17

Browse files
committed
deploy testnet and mainnet contracts
1 parent a2df88a commit 4453f17

31 files changed

Lines changed: 399 additions & 191 deletions

client/asset/eth/deploy.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
"decred.org/dcrdex/client/asset"
1414
"decred.org/dcrdex/dex"
1515
erc20v0 "decred.org/dcrdex/dex/networks/erc20/contracts/v0"
16+
erc20v1 "decred.org/dcrdex/dex/networks/erc20/contracts/v1"
1617
dexeth "decred.org/dcrdex/dex/networks/eth"
1718
multibal "decred.org/dcrdex/dex/networks/eth/contracts/multibalance"
1819
ethv0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
20+
ethv1 "decred.org/dcrdex/dex/networks/eth/contracts/v1"
1921
"github.com/ethereum/go-ethereum"
2022
"github.com/ethereum/go-ethereum/accounts/abi"
2123
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -140,37 +142,35 @@ func (contractDeployer) EstimateMultiBalanceDeployFunding(
140142
}
141143

142144
func (contractDeployer) txData(contractVer uint32, tokenAddr common.Address) (txData []byte, err error) {
143-
var abi *abi.ABI
144-
var bytecode []byte
145-
isToken := tokenAddr != (common.Address{})
146-
if isToken {
147-
switch contractVer {
148-
case 0:
149-
bytecode = common.FromHex(erc20v0.ERC20SwapBin)
150-
abi, err = erc20v0.ERC20SwapMetaData.GetAbi()
151-
}
152-
} else {
145+
if tokenAddr == (common.Address{}) {
153146
switch contractVer {
154147
case 0:
155-
bytecode = common.FromHex(ethv0.ETHSwapBin)
156-
abi, err = ethv0.ETHSwapMetaData.GetAbi()
148+
return common.FromHex(ethv0.ETHSwapBin), nil
149+
case 1:
150+
return common.FromHex(ethv1.ETHSwapBin), nil
157151
}
158152
}
153+
var abi *abi.ABI
154+
var bytecode []byte
155+
switch contractVer {
156+
case 0:
157+
bytecode = common.FromHex(erc20v0.ERC20SwapBin)
158+
abi, err = erc20v0.ERC20SwapMetaData.GetAbi()
159+
case 1:
160+
bytecode = common.FromHex(erc20v1.ERC20SwapBin)
161+
abi, err = erc20v1.ERC20SwapMetaData.GetAbi()
162+
}
159163
if err != nil {
160164
return nil, fmt.Errorf("error parsing ABI: %w", err)
161165
}
162166
if abi == nil {
163167
return nil, fmt.Errorf("no abi data for version %d", contractVer)
164168
}
165-
txData = bytecode
166-
if isToken {
167-
argData, err := abi.Pack("", tokenAddr)
168-
if err != nil {
169-
return nil, fmt.Errorf("error packing token address: %w", err)
170-
}
171-
txData = append(txData, argData...)
169+
argData, err := abi.Pack("", tokenAddr)
170+
if err != nil {
171+
return nil, fmt.Errorf("error packing token address: %w", err)
172172
}
173-
return
173+
return append(bytecode, argData...), nil
174174
}
175175

176176
// DeployContract deployes a dcrdex swap contract.
@@ -199,6 +199,11 @@ func (contractDeployer) DeployContract(
199199
contractAddr, tx, _, err := erc20v0.DeployERC20Swap(txOpts, cb, tokenAddress)
200200
return contractAddr, tx, err
201201
}
202+
case 1:
203+
deployer = func(txOpts *bind.TransactOpts, cb bind.ContractBackend) (common.Address, *types.Transaction, error) {
204+
contractAddr, tx, _, err := erc20v1.DeployERC20Swap(txOpts, cb, tokenAddress)
205+
return contractAddr, tx, err
206+
}
202207

203208
}
204209
} else {
@@ -208,6 +213,11 @@ func (contractDeployer) DeployContract(
208213
contractAddr, tx, _, err := ethv0.DeployETHSwap(txOpts, cb)
209214
return contractAddr, tx, err
210215
}
216+
case 1:
217+
deployer = func(txOpts *bind.TransactOpts, cb bind.ContractBackend) (common.Address, *types.Transaction, error) {
218+
contractAddr, tx, _, err := ethv1.DeployETHSwap(txOpts, cb)
219+
return contractAddr, tx, err
220+
}
211221
}
212222
}
213223
if deployer == nil {
@@ -264,7 +274,7 @@ func (contractDeployer) deployContract(
264274
}
265275

266276
feeRate := dexeth.WeiToGweiCeil(maxFeeRate)
267-
log.Infof("Estimated fees: %s gwei / gas", ui.ConventionalString(feeRate*gas))
277+
log.Infof("Estimated fees: %s gwei", ui.ConventionalString(feeRate*gas))
268278

269279
gas *= 5 / 4 // Add 20% buffer
270280
feesWithBuffer := feeRate * gas
@@ -287,7 +297,13 @@ func (contractDeployer) deployContract(
287297
return err
288298
}
289299

290-
log.Infof("👍 Contract %s launched with tx %s", contractAddr, tx.Hash())
300+
log.Infof("Contract %s launched with tx %s", contractAddr, tx.Hash())
301+
302+
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
303+
return fmt.Errorf("error waiting for deployment transaction status: %w", err)
304+
}
305+
306+
log.Info("👍 Transaction confirmed")
291307

292308
return nil
293309
}

client/asset/eth/eth.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ import (
4646
"github.com/tyler-smith/go-bip39"
4747
)
4848

49-
func init() {
50-
dexeth.MaybeReadSimnetAddrs()
51-
}
52-
5349
func registerToken(tokenID uint32, desc string) {
5450
token, found := dexeth.Tokens[tokenID]
5551
if !found {
@@ -67,6 +63,7 @@ func registerToken(tokenID uint32, desc string) {
6763
}
6864

6965
func init() {
66+
dexeth.MaybeReadSimnetAddrs()
7067
asset.Register(BipID, &Driver{})
7168
registerToken(usdcTokenID, "The USDC Ethereum ERC20 token.")
7269
registerToken(usdtTokenID, "The USDT Ethereum ERC20 token.")
@@ -882,7 +879,8 @@ func (w *ETHWallet) Connect(ctx context.Context) (_ *sync.WaitGroup, err error)
882879
for ver, constructor := range contractorConstructors {
883880
contractAddr, exists := w.versionedContracts[ver]
884881
if !exists || contractAddr == (common.Address{}) {
885-
return nil, fmt.Errorf("no contract address for version %d, net %s", ver, w.net)
882+
w.log.Debugf("no eth swap contract address for version %d, net %s", ver, w.net)
883+
continue
886884
}
887885
c, err := constructor(w.net, contractAddr, w.addr, w.node.contractBackend())
888886
if err != nil {
@@ -5629,9 +5627,26 @@ func (getGas) ReadCredentials(chain, credentialsPath string, net dex.Network) (a
56295627
return
56305628
}
56315629

5632-
func getGetGasClientWithEstimatesAndBalances(ctx context.Context, net dex.Network, contractVer uint32, maxSwaps int,
5633-
walletDir string, providers []string, seed []byte, wParams *GetGasWalletParams, log dex.Logger) (cl *multiRPCClient, c contractor,
5634-
ethReq, swapReq, feeRate uint64, ethBal, tokenBal *big.Int, err error) {
5630+
func getGetGasClientWithEstimatesAndBalances(
5631+
ctx context.Context,
5632+
net dex.Network,
5633+
contractVer uint32,
5634+
maxSwaps int,
5635+
walletDir string,
5636+
providers []string,
5637+
seed []byte,
5638+
wParams *GetGasWalletParams,
5639+
log dex.Logger,
5640+
) (
5641+
cl *multiRPCClient,
5642+
c contractor,
5643+
ethReq,
5644+
swapReq,
5645+
feeRate uint64,
5646+
ethBal,
5647+
tokenBal *big.Int,
5648+
err error,
5649+
) {
56355650

56365651
cl, c, err = quickNode(ctx, walletDir, contractVer, seed, providers, wParams, net, log)
56375652
if err != nil {
@@ -5882,6 +5897,7 @@ func (getGas) returnFunds(
58825897
}
58835898

58845899
remainder := ethBal - fees
5900+
58855901
txOpts, err := cl.txOpts(ctx, remainder, defaultSendGasLimit, maxFeeRate, tipRate, nil)
58865902
if err != nil {
58875903
return fmt.Errorf("error generating tx opts: %w", err)
@@ -5906,6 +5922,10 @@ func (getGas) returnFunds(
59065922
func (getGas) Estimate(ctx context.Context, net dex.Network, assetID, contractVer uint32, maxSwaps int,
59075923
credentialsPath string, wParams *GetGasWalletParams, log dex.Logger) error {
59085924

5925+
if *wParams.Gas == (dexeth.Gases{}) {
5926+
return fmt.Errorf("empty gas table. put some estimates in VersionedGases or Tokens for this contract")
5927+
}
5928+
59095929
symbol := dex.BipIDSymbol(assetID)
59105930
log.Infof("Getting gas estimates for up to %d swaps of asset %s, contract version %d on %s", maxSwaps, symbol, contractVer, symbol)
59115931

@@ -6047,6 +6067,8 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
60476067
return fmt.Errorf("error getting network fees: %v", err)
60486068
}
60496069

6070+
maxFeeRate := new(big.Int).Add(tipRate, new(big.Int).Mul(baseRate, big.NewInt(2)))
6071+
60506072
defer func() {
60516073
if len(stats.swaps) == 0 {
60526074
return
@@ -6087,17 +6109,23 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
60876109
fmt.Printf(" %+v \n", stats.transfers)
60886110
}()
60896111

6112+
logTx := func(tag string, n int, tx *types.Transaction) {
6113+
log.Infof("%s %d tx, hash = %s, nonce = %d, maxFeeRate = %s, tip cap = %s",
6114+
tag, n, tx.Hash(), tx.Nonce(), tx.GasFeeCap(), tx.GasTipCap())
6115+
}
6116+
60906117
// Estimate approve for tokens.
60916118
if isToken {
60926119
sendApprove := func(cl ethFetcher, c tokenContractor) error {
6093-
txOpts, err := cl.txOpts(ctx, 0, g.Approve*2, baseRate, tipRate, nil)
6120+
txOpts, err := cl.txOpts(ctx, 0, g.Approve*2, maxFeeRate, tipRate, nil)
60946121
if err != nil {
60956122
return fmt.Errorf("error constructing signed tx opts for approve: %w", err)
60966123
}
60976124
tx, err := c.approve(txOpts, unlimitedAllowance)
60986125
if err != nil {
60996126
return fmt.Errorf("error estimating approve gas: %w", err)
61006127
}
6128+
logTx("Approve", 1, tx)
61016129
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
61026130
return fmt.Errorf("error waiting for approve transaction: %w", err)
61036131
}
@@ -6124,7 +6152,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
61246152
return fmt.Errorf("error sending approve transaction for the initiator: %w", err)
61256153
}
61266154

6127-
txOpts, err := cl.txOpts(ctx, 0, g.Transfer*2, baseRate, tipRate, nil)
6155+
txOpts, err := cl.txOpts(ctx, 0, g.Transfer*2, maxFeeRate, tipRate, nil)
61286156
if err != nil {
61296157
return fmt.Errorf("error constructing signed tx opts for transfer: %w", err)
61306158
}
@@ -6137,6 +6165,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
61376165
if err != nil {
61386166
return fmt.Errorf("transfer error: %w", err)
61396167
}
6168+
logTx("Transfer", 1, transferTx)
61406169
if err = waitForConfirmation(ctx, cl, transferTx.Hash()); err != nil {
61416170
return fmt.Errorf("error waiting for transfer tx: %w", err)
61426171
}
@@ -6176,7 +6205,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
61766205
}
61776206

61786207
// Send the inits
6179-
txOpts, err := cl.txOpts(ctx, optsVal, g.SwapN(n)*2, baseRate, tipRate, nil)
6208+
txOpts, err := cl.txOpts(ctx, optsVal, g.SwapN(n)*2, maxFeeRate, tipRate, nil)
61806209
if err != nil {
61816210
return fmt.Errorf("error constructing signed tx opts for %d swaps: %v", n, err)
61826211
}
@@ -6185,6 +6214,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
61856214
if err != nil {
61866215
return fmt.Errorf("initiate error for %d swaps: %v", n, err)
61876216
}
6217+
logTx("Initiate", n, tx)
61886218
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
61896219
return fmt.Errorf("error waiting for init tx to be mined: %w", err)
61906220
}
@@ -6219,7 +6249,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
62196249
})
62206250
}
62216251

6222-
txOpts, err = cl.txOpts(ctx, 0, g.RedeemN(n)*2, baseRate, tipRate, nil)
6252+
txOpts, err = cl.txOpts(ctx, 0, g.RedeemN(n)*2, maxFeeRate, tipRate, nil)
62236253
if err != nil {
62246254
return fmt.Errorf("error constructing signed tx opts for %d redeems: %v", n, err)
62256255
}
@@ -6228,6 +6258,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
62286258
if err != nil {
62296259
return fmt.Errorf("redeem error for %d swaps: %v", n, err)
62306260
}
6261+
logTx("Redeem", n, tx)
62316262
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
62326263
return fmt.Errorf("error waiting for redeem tx to be mined: %w", err)
62336264
}

client/asset/polygon/polygon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ var (
7474
WalletInfo = asset.WalletInfo{
7575
Name: "Polygon",
7676
Version: 0,
77-
SupportedVersions: []uint32{0},
77+
SupportedVersions: []uint32{0, 1},
7878
UnitInfo: dexpolygon.UnitInfo,
7979
AvailableWallets: []*asset.WalletDefinition{
8080
{

client/webserver/site/src/js/markets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ export default class MarketsPage extends BasePage {
21262126
if (!app().checkResponse(res)) {
21272127
throw Error('error unlocking wallet ' + res.msg)
21282128
}
2129-
this.balanceWgt.updateAsset(this.openAsset.id)
2129+
if (this.openAsset) this.balanceWgt.updateAsset(this.openAsset.id)
21302130
}
21312131

21322132
/*

dex/networks/erc20/contracts/updatecontract.sh

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ fi
3636
mkdir temp
3737

3838
solc --abi --bin --bin-runtime --overwrite --optimize ${SOLIDITY_FILE} -o ./temp/
39-
BYTECODE=$(<./temp/${CONTRACT_NAME}.bin-runtime)
40-
41-
cat > "./${PKG_NAME}/BinRuntimeV${VERSION}.go" <<EOF
42-
// Code generated - DO NOT EDIT.
43-
// This file is a generated binding and any manual changes will be lost.
44-
45-
package ${PKG_NAME}
46-
47-
const ${CONTRACT_NAME}RuntimeBin = "${BYTECODE}"
48-
EOF
4939

5040
CONTRACT_FILE=./${PKG_NAME}/contract.go
5141
abigen --abi ./temp/${CONTRACT_NAME}.abi --bin ./temp/${CONTRACT_NAME}.bin \
@@ -56,15 +46,8 @@ BYTECODE=$(<./temp/${CONTRACT_NAME}.bin)
5646
solc --bin --optimize ${TEST_TOKEN} -o ./temp
5747
TEST_TOKEN_BYTECODE=$(<./temp/TestToken.bin)
5848

59-
for HARNESS_PATH in "$(realpath ../../../testing/eth/harness.sh)" "$(realpath ../../../testing/polygon/harness.sh)"; do
60-
sed -i.tmp "s/ERC20_SWAP_V${VERSION}=.*/ERC20_SWAP_V${VERSION}=\"${BYTECODE}\"/" "${HARNESS_PATH}"
61-
# mac needs a temp file specified above.
62-
rm "${HARNESS_PATH}.tmp"
63-
64-
sed -i.tmp "s/TEST_TOKEN=.*/TEST_TOKEN=\"${TEST_TOKEN_BYTECODE}\"/" "${HARNESS_PATH}"
65-
# mac needs a temp file specified above.
66-
rm "${HARNESS_PATH}.tmp"
67-
done
49+
echo "${BYTECODE}" | xxd -r -p > "v${VERSION}/swap_contract.bin"
50+
echo "${TEST_TOKEN_BYTECODE}" | xxd -r -p > "v${VERSION}/token_contract.bin"
6851

6952
rm -fr temp
7053

dex/networks/erc20/contracts/v0/BinRuntimeV0.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)