Skip to content

Commit

Permalink
deploy testnet and mainnet contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Jun 3, 2024
1 parent 56b14df commit 4278b8b
Show file tree
Hide file tree
Showing 29 changed files with 397 additions and 177 deletions.
60 changes: 38 additions & 22 deletions client/asset/eth/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"decred.org/dcrdex/client/asset"
"decred.org/dcrdex/dex"
erc20v0 "decred.org/dcrdex/dex/networks/erc20/contracts/v0"
erc20v1 "decred.org/dcrdex/dex/networks/erc20/contracts/v1"
dexeth "decred.org/dcrdex/dex/networks/eth"
multibal "decred.org/dcrdex/dex/networks/eth/contracts/multibalance"
ethv0 "decred.org/dcrdex/dex/networks/eth/contracts/v0"
ethv1 "decred.org/dcrdex/dex/networks/eth/contracts/v1"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -140,37 +142,35 @@ func (contractDeployer) EstimateMultiBalanceDeployFunding(
}

func (contractDeployer) txData(contractVer uint32, tokenAddr common.Address) (txData []byte, err error) {
var abi *abi.ABI
var bytecode []byte
isToken := tokenAddr != (common.Address{})
if isToken {
switch contractVer {
case 0:
bytecode = common.FromHex(erc20v0.ERC20SwapBin)
abi, err = erc20v0.ERC20SwapMetaData.GetAbi()
}
} else {
if tokenAddr == (common.Address{}) {
switch contractVer {
case 0:
bytecode = common.FromHex(ethv0.ETHSwapBin)
abi, err = ethv0.ETHSwapMetaData.GetAbi()
return common.FromHex(ethv0.ETHSwapBin), nil
case 1:
return common.FromHex(ethv1.ETHSwapBin), nil
}
}
var abi *abi.ABI
var bytecode []byte
switch contractVer {
case 0:
bytecode = common.FromHex(erc20v0.ERC20SwapBin)
abi, err = erc20v0.ERC20SwapMetaData.GetAbi()
case 1:
bytecode = common.FromHex(erc20v1.ERC20SwapBin)
abi, err = erc20v1.ERC20SwapMetaData.GetAbi()
}
if err != nil {
return nil, fmt.Errorf("error parsing ABI: %w", err)
}
if abi == nil {
return nil, fmt.Errorf("no abi data for version %d", contractVer)
}
txData = bytecode
if isToken {
argData, err := abi.Pack("", tokenAddr)
if err != nil {
return nil, fmt.Errorf("error packing token address: %w", err)
}
txData = append(txData, argData...)
argData, err := abi.Pack("", tokenAddr)
if err != nil {
return nil, fmt.Errorf("error packing token address: %w", err)
}
return
return append(bytecode, argData...), nil
}

// DeployContract deployes a dcrdex swap contract.
Expand Down Expand Up @@ -199,6 +199,11 @@ func (contractDeployer) DeployContract(
contractAddr, tx, _, err := erc20v0.DeployERC20Swap(txOpts, cb, tokenAddress)
return contractAddr, tx, err
}
case 1:
deployer = func(txOpts *bind.TransactOpts, cb bind.ContractBackend) (common.Address, *types.Transaction, error) {
contractAddr, tx, _, err := erc20v1.DeployERC20Swap(txOpts, cb, tokenAddress)
return contractAddr, tx, err
}

}
} else {
Expand All @@ -208,6 +213,11 @@ func (contractDeployer) DeployContract(
contractAddr, tx, _, err := ethv0.DeployETHSwap(txOpts, cb)
return contractAddr, tx, err
}
case 1:
deployer = func(txOpts *bind.TransactOpts, cb bind.ContractBackend) (common.Address, *types.Transaction, error) {
contractAddr, tx, _, err := ethv1.DeployETHSwap(txOpts, cb)
return contractAddr, tx, err
}
}
}
if deployer == nil {
Expand Down Expand Up @@ -264,7 +274,7 @@ func (contractDeployer) deployContract(
}

feeRate := dexeth.WeiToGweiCeil(maxFeeRate)
log.Infof("Estimated fees: %s gwei / gas", ui.ConventionalString(feeRate*gas))
log.Infof("Estimated fees: %s gwei", ui.ConventionalString(feeRate*gas))

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

log.Infof("👍 Contract %s launched with tx %s", contractAddr, tx.Hash())
log.Infof("Contract %s launched with tx %s", contractAddr, tx.Hash())

if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
return fmt.Errorf("error waiting for deployment transaction status: %w", err)
}

log.Info("👍 Transaction confirmed")

return nil
}
Expand Down
54 changes: 44 additions & 10 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ func (w *ETHWallet) Connect(ctx context.Context) (_ *sync.WaitGroup, err error)
for ver, constructor := range contractorConstructors {
contractAddr, exists := w.versionedContracts[ver]
if !exists || contractAddr == (common.Address{}) {
return nil, fmt.Errorf("no contract address for version %d, net %s", ver, w.net)
w.log.Debugf("no eth swap contract address for version %d, net %s", ver, w.net)
continue
}
c, err := constructor(w.net, contractAddr, w.addr, w.node.contractBackend())
if err != nil {
Expand Down Expand Up @@ -5580,9 +5581,26 @@ func (getGas) ReadCredentials(chain, credentialsPath string, net dex.Network) (a
return
}

func getGetGasClientWithEstimatesAndBalances(ctx context.Context, net dex.Network, assetID, contractVer uint32, maxSwaps int,
walletDir string, providers []string, seed []byte, wParams *GetGasWalletParams, log dex.Logger) (cl *multiRPCClient, c contractor,
ethReq, swapReq, feeRate uint64, ethBal, tokenBal *big.Int, err error) {
func getGetGasClientWithEstimatesAndBalances(
ctx context.Context,
net dex.Network,
contractVer uint32,
maxSwaps int,
walletDir string,
providers []string,
seed []byte,
wParams *GetGasWalletParams,
log dex.Logger,
) (
cl *multiRPCClient,
c contractor,
ethReq,
swapReq,
feeRate uint64,
ethBal,
tokenBal *big.Int,
err error,
) {

cl, c, err = quickNode(ctx, walletDir, contractVer, seed, providers, wParams, net, log)
if err != nil {
Expand Down Expand Up @@ -5666,7 +5684,7 @@ func (getGas) EstimateFunding(ctx context.Context, net dex.Network, assetID, con
}
defer os.RemoveAll(walletDir)

cl, _, ethReq, swapReq, _, ethBalBig, tokenBalBig, err := getGetGasClientWithEstimatesAndBalances(ctx, net, assetID, contractVer, maxSwaps, walletDir, providers, seed, wParams, log)
cl, _, ethReq, swapReq, _, ethBalBig, tokenBalBig, err := getGetGasClientWithEstimatesAndBalances(ctx, net, contractVer, maxSwaps, walletDir, providers, seed, wParams, log)
if err != nil {
return err
}
Expand Down Expand Up @@ -5833,6 +5851,7 @@ func (getGas) returnFunds(
}

remainder := ethBal - fees

txOpts, err := cl.txOpts(ctx, remainder, defaultSendGasLimit, maxFeeRate, tipRate, nil)
if err != nil {
return fmt.Errorf("error generating tx opts: %w", err)
Expand All @@ -5857,6 +5876,10 @@ func (getGas) returnFunds(
func (getGas) Estimate(ctx context.Context, net dex.Network, assetID, contractVer uint32, maxSwaps int,
credentialsPath string, wParams *GetGasWalletParams, log dex.Logger) error {

if *wParams.Gas == (dexeth.Gases{}) {
return fmt.Errorf("empty gas table. put some estimates in VersionedGases or Tokens for this contract")
}

symbol := dex.BipIDSymbol(assetID)
log.Infof("Getting gas estimates for up to %d swaps of asset %s, contract version %d on %s", maxSwaps, symbol, contractVer, symbol)

Expand All @@ -5873,7 +5896,7 @@ func (getGas) Estimate(ctx context.Context, net dex.Network, assetID, contractVe
}
defer os.RemoveAll(walletDir)

cl, c, ethReq, swapReq, feeRate, ethBal, tokenBal, err := getGetGasClientWithEstimatesAndBalances(ctx, net, assetID, contractVer, maxSwaps, walletDir, providers, seed, wParams, log)
cl, c, ethReq, swapReq, feeRate, ethBal, tokenBal, err := getGetGasClientWithEstimatesAndBalances(ctx, net, contractVer, maxSwaps, walletDir, providers, seed, wParams, log)
if err != nil {
return err
}
Expand Down Expand Up @@ -5998,6 +6021,8 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
return fmt.Errorf("error getting network fees: %v", err)
}

maxFeeRate := new(big.Int).Add(tipRate, new(big.Int).Mul(baseRate, big.NewInt(2)))

defer func() {
if len(stats.swaps) == 0 {
return
Expand Down Expand Up @@ -6038,17 +6063,23 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
fmt.Printf(" %+v \n", stats.transfers)
}()

logTx := func(tag string, n int, tx *types.Transaction) {
log.Infof("%s %d tx, hash = %s, nonce = %d, maxFeeRate = %s, tip cap = %s",
tag, n, tx.Hash(), tx.Nonce(), tx.GasFeeCap(), tx.GasTipCap())
}

// Estimate approve for tokens.
if isToken {
sendApprove := func(cl ethFetcher, c tokenContractor) error {
txOpts, err := cl.txOpts(ctx, 0, g.Approve*2, baseRate, tipRate, nil)
txOpts, err := cl.txOpts(ctx, 0, g.Approve*2, maxFeeRate, tipRate, nil)
if err != nil {
return fmt.Errorf("error constructing signed tx opts for approve: %w", err)
}
tx, err := c.approve(txOpts, unlimitedAllowance)
if err != nil {
return fmt.Errorf("error estimating approve gas: %w", err)
}
logTx("Approve", 1, tx)
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
return fmt.Errorf("error waiting for approve transaction: %w", err)
}
Expand All @@ -6075,7 +6106,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
return fmt.Errorf("error sending approve transaction for the initiator: %w", err)
}

txOpts, err := cl.txOpts(ctx, 0, g.Transfer*2, baseRate, tipRate, nil)
txOpts, err := cl.txOpts(ctx, 0, g.Transfer*2, maxFeeRate, tipRate, nil)
if err != nil {
return fmt.Errorf("error constructing signed tx opts for transfer: %w", err)
}
Expand All @@ -6088,6 +6119,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
if err != nil {
return fmt.Errorf("transfer error: %w", err)
}
logTx("Transfer", 1, transferTx)
if err = waitForConfirmation(ctx, cl, transferTx.Hash()); err != nil {
return fmt.Errorf("error waiting for transfer tx: %w", err)
}
Expand Down Expand Up @@ -6127,7 +6159,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
}

// Send the inits
txOpts, err := cl.txOpts(ctx, optsVal, g.SwapN(n)*2, baseRate, tipRate, nil)
txOpts, err := cl.txOpts(ctx, optsVal, g.SwapN(n)*2, maxFeeRate, tipRate, nil)
if err != nil {
return fmt.Errorf("error constructing signed tx opts for %d swaps: %v", n, err)
}
Expand All @@ -6136,6 +6168,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
if err != nil {
return fmt.Errorf("initiate error for %d swaps: %v", n, err)
}
logTx("Initiate", n, tx)
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
return fmt.Errorf("error waiting for init tx to be mined: %w", err)
}
Expand Down Expand Up @@ -6170,7 +6203,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
})
}

txOpts, err = cl.txOpts(ctx, 0, g.RedeemN(n)*2, baseRate, tipRate, nil)
txOpts, err = cl.txOpts(ctx, 0, g.RedeemN(n)*2, maxFeeRate, tipRate, nil)
if err != nil {
return fmt.Errorf("error constructing signed tx opts for %d redeems: %v", n, err)
}
Expand All @@ -6179,6 +6212,7 @@ func getGasEstimates(ctx context.Context, cl, acl ethFetcher, c contractor, ac t
if err != nil {
return fmt.Errorf("redeem error for %d swaps: %v", n, err)
}
logTx("Redeem", n, tx)
if err = waitForConfirmation(ctx, cl, tx.Hash()); err != nil {
return fmt.Errorf("error waiting for redeem tx to be mined: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/asset/polygon/polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var (
WalletInfo = asset.WalletInfo{
Name: "Polygon",
Version: 0,
SupportedVersions: []uint32{0},
SupportedVersions: []uint32{0, 1},
UnitInfo: dexpolygon.UnitInfo,
AvailableWallets: []*asset.WalletDefinition{
{
Expand Down
21 changes: 2 additions & 19 deletions dex/networks/erc20/contracts/updatecontract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ fi
mkdir temp

solc --abi --bin --bin-runtime --overwrite --optimize ${SOLIDITY_FILE} -o ./temp/
BYTECODE=$(<./temp/${CONTRACT_NAME}.bin-runtime)

cat > "./${PKG_NAME}/BinRuntimeV${VERSION}.go" <<EOF
// Code generated - DO NOT EDIT.
// This file is a generated binding and any manual changes will be lost.
package ${PKG_NAME}
const ${CONTRACT_NAME}RuntimeBin = "${BYTECODE}"
EOF

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

for HARNESS_PATH in "$(realpath ../../../testing/eth/harness.sh)" "$(realpath ../../../testing/polygon/harness.sh)"; do
sed -i.tmp "s/ERC20_SWAP_V${VERSION}=.*/ERC20_SWAP_V${VERSION}=\"${BYTECODE}\"/" "${HARNESS_PATH}"
# mac needs a temp file specified above.
rm "${HARNESS_PATH}.tmp"

sed -i.tmp "s/TEST_TOKEN=.*/TEST_TOKEN=\"${TEST_TOKEN_BYTECODE}\"/" "${HARNESS_PATH}"
# mac needs a temp file specified above.
rm "${HARNESS_PATH}.tmp"
done
echo "${BYTECODE}" | xxd -r -p > "v${VERSION}/swap_contract.bin"
echo "${TEST_TOKEN_BYTECODE}" | xxd -r -p > "v${VERSION}/token_contract.bin"

rm -fr temp

Expand Down
6 changes: 0 additions & 6 deletions dex/networks/erc20/contracts/v0/BinRuntimeV0.go

This file was deleted.

Loading

0 comments on commit 4278b8b

Please sign in to comment.