move to gsnforwarder that is bytecode identical on mainnet and dev (#427)

This commit is contained in:
Dmitry Holodov
2023-04-26 13:55:00 -05:00
committed by GitHub
parent 4c10263ebd
commit caa5251e59
8 changed files with 109 additions and 90 deletions

View File

@@ -39,7 +39,6 @@ type Config struct {
DataDir string
MoneroNodes []*MoneroNode
SwapCreatorAddr ethcommon.Address
ForwarderAddr ethcommon.Address
Bootnodes []string
}
@@ -67,9 +66,10 @@ func MainnetConfig() *Config {
Port: DefaultMoneroDaemonMainnetPort,
},
},
// Note: SwapCreator contract below is using GSN Forwarder address
// 0xB2b5841DBeF766d4b521221732F9B618fCf34A87
// https://docs.opengsn.org/networks/addresses.html
SwapCreatorAddr: ethcommon.HexToAddress("0xD3d19539D61bB0e7617E499C7262594E71CA1c66"),
// ForwarderAddr is from https://docs.opengsn.org/networks/addresses.html
ForwarderAddr: ethcommon.HexToAddress("0xB2b5841DBeF766d4b521221732F9B618fCf34A87"),
Bootnodes: []string{
"/ip4/67.205.131.11/tcp/9909/p2p/12D3KooWGpCLC4y42rf6aR3cguVFJAruzFXT6mUEyp7C32jTsyJd",
"/ip4/143.198.123.27/tcp/9909/p2p/12D3KooWDCE2ukB1Sw88hmLFk5BZRRViyYLeuAKPuu59nYyFWAec",
@@ -102,8 +102,7 @@ func StagenetConfig() *Config {
Port: 38081,
},
},
SwapCreatorAddr: ethcommon.HexToAddress("0x0296C47519f4dc290da2bd3fFDdDBe21F40990e3"),
ForwarderAddr: ethcommon.HexToAddress("0xa030E074b8398005a454CB7c51E9b7CDb966744a"),
SwapCreatorAddr: ethcommon.HexToAddress("0xEd014568991A9BE34F381Bf46d9c3f7623D4DEa5"),
Bootnodes: []string{
"/ip4/134.122.115.208/tcp/9900/p2p/12D3KooWDqCzbjexHEa8Rut7bzxHFpRMZyDRW1L6TGkL1KY24JH5",
"/ip4/143.198.123.27/tcp/9900/p2p/12D3KooWSc4yFkPWBFmPToTMbhChH3FAgGH96DNzSg5fio1pQYoN",

View File

@@ -33,7 +33,6 @@ var forwarderAddrIndices = []int{203, 1124}
var (
errInvalidSwapCreatorContract = errors.New("given contract address does not contain correct SwapCreator code")
errInvalidForwarderContract = errors.New("given contract address does not contain correct Forwarder code")
)
// CheckSwapCreatorContractCode checks that the bytecode at the given address matches the
@@ -91,7 +90,7 @@ func CheckSwapCreatorContractCode(
err = CheckForwarderContractCode(ctx, ec, forwarderAddr)
if err != nil {
return ethcommon.Address{}, err
return ethcommon.Address{}, fmt.Errorf("%w: %s", errInvalidSwapCreatorContract, err)
}
// return the trusted forwarder address that was parsed from the deployed contract byte code
@@ -105,30 +104,21 @@ func CheckForwarderContractCode(
ec *ethclient.Client,
contractAddr ethcommon.Address,
) error {
// mainnet override - since the forwarder contract deployed on mainnet is compiled
// with solidity 0.8.7, but we're using 0.8.19 for SwapCreator.sol, we can just
// check that the address is what's expected.
chainID, err := ec.ChainID(ctx)
if err != nil {
return err
}
if contractAddr == common.MainnetConfig().ForwarderAddr && chainID.Uint64() == common.MainnetChainID {
return nil
switch chainID.Uint64() {
case common.MainnetChainID:
if contractAddr == ethcommon.HexToAddress(gsnforwarder.MainnetForwarderAddrHex) {
return nil
}
case common.SepoliaChainID:
if contractAddr == ethcommon.HexToAddress(gsnforwarder.SepoliaForwarderAddrHex) {
return nil
}
}
code, err := ec.CodeAt(ctx, contractAddr, nil)
if err != nil {
return err
}
expectedCode := ethcommon.FromHex(gsnforwarder.ForwarderMetaData.Bin)
// expectedCode is the compiled code, while code is the deployed bytecode.
// the deployed bytecode is a subset of the compiled code.
if !bytes.Equal(expectedCode[705:9585], code) {
return errInvalidForwarderContract
}
return nil
return gsnforwarder.CheckForwarderContractCode(ctx, ec, contractAddr)
}

View File

@@ -8,17 +8,16 @@ import (
"context"
"crypto/ecdsa"
"errors"
"os"
"testing"
"github.com/athanorlabs/go-relayer/impls/gsnforwarder"
ethcommon "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
"github.com/athanorlabs/atomic-swap/common"
"github.com/athanorlabs/atomic-swap/tests"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
// deployContract is a test helper that deploys the SwapCreator contract and returns the
@@ -124,30 +123,31 @@ func TestCheckSwapCreatorContractCode_fail(t *testing.T) {
}
func TestSepoliaContract(t *testing.T) {
endpoint := os.Getenv("ETH_SEPOLIA_ENDPOINT")
if endpoint == "" {
endpoint = "https://rpc.sepolia.org/"
}
ctx := context.Background()
ec := tests.NewEthSepoliaClient(t)
// temporarily place a funded sepolia private key below to deploy the test contract
const sepoliaKey = ""
ctx := context.Background()
ec, err := ethclient.Dial(endpoint)
require.NoError(t, err)
defer ec.Close()
parsedTFAddr, err := CheckSwapCreatorContractCode(ctx, ec, common.StagenetConfig().SwapCreatorAddr)
if errors.Is(err, errInvalidSwapCreatorContract) && sepoliaKey != "" {
pk, err := ethcrypto.HexToECDSA(sepoliaKey) //nolint:govet // shadow declaration of err
require.NoError(t, err)
forwarderAddr := common.StagenetConfig().ForwarderAddr
sfAddr, _, err := DeploySwapCreatorWithKey(context.Background(), ec, pk, forwarderAddr)
forwarderAddr := ethcommon.HexToAddress(gsnforwarder.SepoliaForwarderAddrHex)
swapCreatorAddr, _, err := DeploySwapCreatorWithKey(ctx, ec, pk, forwarderAddr)
require.NoError(t, err)
t.Logf("New Sepolia SwapCreator deployed with TrustedForwarder %s", forwarderAddr)
t.Fatalf("Update common.StagenetConfig.ContractAddress with %s", sfAddr.Hex())
} else {
require.NoError(t, err)
t.Logf("Sepolia SwapCreator deployed with TrustedForwarder=%s", parsedTFAddr.Hex())
t.Fatalf("Update common.StagenetConfig()'s SwapCreatorAddr with %s", swapCreatorAddr.Hex())
}
require.NoError(t, err)
require.Equal(t, gsnforwarder.SepoliaForwarderAddrHex, parsedTFAddr.Hex())
}
func TestMainnetContract(t *testing.T) {
ctx := context.Background()
ec := tests.NewEthMainnetClient(t)
mainnetConf := common.MainnetConfig()
parsedTFAddr, err := CheckSwapCreatorContractCode(ctx, ec, mainnetConf.SwapCreatorAddr)
require.NoError(t, err)
require.Equal(t, gsnforwarder.MainnetForwarderAddrHex, parsedTFAddr.Hex())
}

14
go.mod
View File

@@ -9,11 +9,11 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/athanorlabs/go-dleq v0.1.0
github.com/athanorlabs/go-p2p-net v0.2.0
github.com/athanorlabs/go-relayer v0.1.0
github.com/athanorlabs/go-relayer v0.2.0
github.com/btcsuite/btcd/btcutil v1.1.3
github.com/cockroachdb/apd/v3 v3.1.2
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/ethereum/go-ethereum v1.11.5
github.com/ethereum/go-ethereum v1.11.6
github.com/fatih/color v1.15.0
github.com/go-playground/validator/v10 v10.12.0
github.com/golang/mock v1.6.0
@@ -34,9 +34,9 @@ require (
require (
github.com/ChainSafe/log15 v1.0.0 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/benbjohnson/clock v1.3.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
@@ -76,7 +76,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/google/uuid v1.3.0 // indirect
@@ -149,7 +149,7 @@ require (
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
github.com/quic-go/quic-go v0.33.0 // indirect
github.com/quic-go/quic-go v0.34.0 // indirect
github.com/quic-go/webtransport-go v0.5.2 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
@@ -172,7 +172,7 @@ require (
go.uber.org/fx v1.19.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.1.0 // indirect

27
go.sum
View File

@@ -19,8 +19,8 @@ github.com/ChainSafe/log15 v1.0.0/go.mod h1:5v1+ALHtdW0NfAeeoYyKmzCAMcAeqkdhIg4u
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
github.com/CloudyKit/jet/v3 v3.0.0/go.mod h1:HKQPgSJmdK8hdoAbKUUWajkHyHo4RaU5rMdUywE7VMo=
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
github.com/MarinX/monerorpc v1.0.5 h1:3brpRWTLngzjlAGprmLWuAY16QCSiwokoaGdpu+/ukc=
github.com/MarinX/monerorpc v1.0.5/go.mod h1:NohAIf5kJ4pS0sO9mbEQkI1dLHuxd4L0DX2Zou0Yofo=
@@ -41,12 +41,13 @@ github.com/athanorlabs/go-dleq v0.1.0 h1:0/llWZG8fz2uintMBKOiBC502zCsDA8nt8vxI73
github.com/athanorlabs/go-dleq v0.1.0/go.mod h1:DWry6jSD7A13MKmeZA0AX3/xBeQCXDoygX99VPwL3yU=
github.com/athanorlabs/go-p2p-net v0.2.0 h1:+VpAN10Ys0B28QDXQRaDySvNfHS99Jt83Qq1sUhEnG4=
github.com/athanorlabs/go-p2p-net v0.2.0/go.mod h1:egbDohZq6I4FzKaVytR+xZKUwA2OqTE6mr9dsNQPPbE=
github.com/athanorlabs/go-relayer v0.1.0 h1:kmUmAQsAgO+tTli3t+NZkmbRnRsdVISQGqOgY0tHhGQ=
github.com/athanorlabs/go-relayer v0.1.0/go.mod h1:Ww/wfTsi+WGS2Yf6Zk3ao7EjoJmcIdWfq6WNHcCEmxk=
github.com/athanorlabs/go-relayer v0.2.0 h1:cYwEadgLWotWBlCx+uhLZphQna3EKEekHhNViHgaSSo=
github.com/athanorlabs/go-relayer v0.2.0/go.mod h1:xh6P9KTXNS9zENAT3QyOBP3sdyHtefrtBLugKeI8oJ4=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.3 h1:g+rSsSaAzhHJYcIQE78hJ3AhyjjtQvleKDjlhdBnIhc=
github.com/benbjohnson/clock v1.3.3/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
@@ -161,8 +162,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/ethereum/go-ethereum v1.11.5 h1:3M1uan+LAUvdn+7wCEFrcMM4LJTeuxDrPTg/f31a5QQ=
github.com/ethereum/go-ethereum v1.11.5/go.mod h1:it7x0DWnTDMfVFdXcU6Ti4KEFQynLHVRarcSlPr0HBo=
github.com/ethereum/go-ethereum v1.11.6 h1:2VF8Mf7XiSUfmoNOy3D+ocfl9Qu8baQBrCNbo2CXQ8E=
github.com/ethereum/go-ethereum v1.11.6/go.mod h1:+a8pUj1tOyJ2RinsNQD4326YS+leSoKGiG/uVVb0x6Y=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
@@ -278,8 +279,9 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
@@ -610,8 +612,8 @@ github.com/quic-go/qtls-go1-19 v0.3.2 h1:tFxjCFcTQzK+oMxG6Zcvp4Dq8dx4yD3dDiIiyc8
github.com/quic-go/qtls-go1-19 v0.3.2/go.mod h1:ySOI96ew8lnoKPtSqx2BlI5wCpUVPT05RMAlajtnyOI=
github.com/quic-go/qtls-go1-20 v0.2.2 h1:WLOPx6OY/hxtTxKV1Zrq20FtXtDEkeY00CGQm8GEa3E=
github.com/quic-go/qtls-go1-20 v0.2.2/go.mod h1:JKtK6mjbAVcUTN/9jZpvLbGxvdWIKS8uT7EiStoU1SM=
github.com/quic-go/quic-go v0.33.0 h1:ItNoTDN/Fm/zBlq769lLJc8ECe9gYaW40veHCCco7y0=
github.com/quic-go/quic-go v0.33.0/go.mod h1:YMuhaAV9/jIu0XclDXwZPAsP/2Kgr5yMYhe9oxhhOFA=
github.com/quic-go/quic-go v0.34.0 h1:OvOJ9LFjTySgwOTYUZmNoq0FzVicP8YujpV0kB7m2lU=
github.com/quic-go/quic-go v0.34.0/go.mod h1:+4CVgVppm0FNjpG3UcX8Joi/frKOH7/ciD5yGcwOO1g=
github.com/quic-go/webtransport-go v0.5.2 h1:GA6Bl6oZY+g/flt00Pnu0XtivSD8vukOu3lYhJjnGEk=
github.com/quic-go/webtransport-go v0.5.2/go.mod h1:OhmmgJIzTTqXK5xvtuX0oBpLV2GkLWNDA+UeTGJXErU=
github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk=
@@ -785,8 +787,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb h1:rhjz/8Mbfa8xROFiH+MQphmAmgqRM0bOMnytznhWEXk=
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
@@ -987,6 +989,7 @@ gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/R
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/ini.v1 v1.51.1/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=

View File

@@ -22,9 +22,9 @@ import (
const (
// mainnetEndpoint is a mainnet ethereum endpoint, from
// https://chainlist.org/chain/1, which stagenet users get pointed at for price
// feeds, as Goeri doesn't have an XMR feed. Mainnet users will use the same
// ethereum endpoint that they use for other swap transactions.
// https://chainlist.org/chain/1, which stagenet users get pointed at for
// price feeds, as Sepolia doesn't have an XMR feed. Mainnet users will use
// the same ethereum endpoint that they use for other swap transactions.
mainnetEndpoint = "https://eth-rpc.gateway.pokt.network"
// https://data.chain.link/ethereum/mainnet/crypto-usd/eth-usd

View File

@@ -5,11 +5,8 @@ package pricefeed
import (
"context"
"net/url"
"os"
"testing"
"github.com/ethereum/go-ethereum/ethclient"
logging "github.com/ipfs/go-log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -21,23 +18,8 @@ func init() {
logging.SetLogLevel("pricefeed", "debug")
}
func getMainnetEndpoint(t *testing.T) string {
endpoint := os.Getenv("ETH_MAINNET_ENDPOINT")
if endpoint == "" {
endpoint = mainnetEndpoint
}
eURL, err := url.Parse(endpoint)
require.NoError(t, err)
// path and fragments may have API keys, so don't log them
t.Logf("mainnet endpoint is %s://%s", eURL.Scheme, eURL.Host)
return endpoint
}
func TestGetETHUSDPrice_mainnet(t *testing.T) {
ec, err := ethclient.Dial(getMainnetEndpoint(t))
require.NoError(t, err)
defer ec.Close()
ec := tests.NewEthMainnetClient(t)
feed, err := GetETHUSDPrice(context.Background(), ec)
require.NoError(t, err)
@@ -56,9 +38,7 @@ func TestGetETHUSDPrice_dev(t *testing.T) {
}
func TestGetXMRUSDPrice_mainnet(t *testing.T) {
ec, err := ethclient.Dial(getMainnetEndpoint(t))
require.NoError(t, err)
defer ec.Close()
ec := tests.NewEthMainnetClient(t)
feed, err := GetXMRUSDPrice(context.Background(), ec)
require.NoError(t, err)

View File

@@ -7,6 +7,7 @@ import (
"context"
"crypto/ecdsa"
"math/big"
"os"
"runtime"
"strings"
"testing"
@@ -54,6 +55,14 @@ const (
repoName = "github.com/athanorlabs/atomic-swap/"
)
// Fallback endpoints for tests that require mainnet or sepolia clients.
// Endpoint values set by ETH_MAINNET_ENDPOINT and ETH_SEPOLIA_ENDPOINT
// environment variables have precedence.
const (
fallbackMainnetEndpoint = "https://eth-rpc.gateway.pokt.network"
fallbackSepoliaEndpoint = "https://rpc.sepolia.org/"
)
// `ganache --deterministic --accounts=50` provides the following keys with
// 1000 ETH on startup. The first 5 are reserved for integration tests and
// should not be referenced directly in any *_test.go file.
@@ -196,6 +205,44 @@ func NewEthClient(t *testing.T) (*ethclient.Client, *big.Int) {
return ec, chainID
}
// NewEthMainnetClient returns a connection to a mainnet endpoint, set by an
// ETH_MAINNET_ENDPOINT environment variable, for testing. If the environment
// variable is empty, the test falls back to the `fallbackMainnetEndpoint`
// constant.
func NewEthMainnetClient(t *testing.T) *ethclient.Client {
endpoint := os.Getenv("ETH_MAINNET_ENDPOINT")
if endpoint == "" {
endpoint = fallbackMainnetEndpoint
}
ec, err := ethclient.Dial(endpoint)
require.NoError(t, err)
t.Cleanup(func() {
ec.Close()
})
return ec
}
// NewEthSepoliaClient returns a connection to a Sepolia endpoint, set by an
// ETH_SEPOLIA_ENDPOINT environment variable, for testing. If the environment
// variable is empty, the test falls back to the `fallbackSepoliaEndpoint`
// constant.
func NewEthSepoliaClient(t *testing.T) *ethclient.Client {
endpoint := os.Getenv("ETH_SEPOLIA_ENDPOINT")
if endpoint == "" {
endpoint = fallbackSepoliaEndpoint
}
ec, err := ethclient.Dial(endpoint)
require.NoError(t, err)
t.Cleanup(func() {
ec.Close()
})
return ec
}
// MineTransaction is a test helper that blocks until the transaction is included in a block
// and returns the receipt. Errors are checked including the status.
func MineTransaction(t *testing.T, ec bind.DeployBackend, tx *ethtypes.Transaction) *ethtypes.Receipt {