Skip to content

Commit

Permalink
chore: update contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Nov 7, 2023
1 parent fbd16d4 commit f3e4b0b
Show file tree
Hide file tree
Showing 117 changed files with 2,676 additions and 2,416 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ jobs:
- name: Run all the linter tools against code
run: make lint

smart-contracts:
name: Build Smart Contracts
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: Install dependencies
working-directory: contracts
run: forge install
- name: Run forge build
working-directory: contracts
run: |
forge --version
FOUNDRY_PROFILE=lite forge build --contracts ./src/loadtester/LoadTester.sol
forge build --skip LoadTester.sol
doc:
name: Doc
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[submodule "contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std

[submodule "contracts/lib/openzeppelin-contracts"]
path = contracts/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
60 changes: 20 additions & 40 deletions cmd/loadtest/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import (
"time"

uniswapv3loadtest "github.com/maticnetwork/polygon-cli/cmd/loadtest/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts"
"github.com/maticnetwork/polygon-cli/contracts/tokens"
"github.com/maticnetwork/polygon-cli/contracts/src/loadtester"
"github.com/maticnetwork/polygon-cli/contracts/src/tokens"

"github.com/maticnetwork/polygon-cli/metrics"
"github.com/maticnetwork/polygon-cli/rpctypes"
"github.com/maticnetwork/polygon-cli/util"

Expand Down Expand Up @@ -472,7 +471,7 @@ func mainLoop(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client) erro

// deploy and instantiate the load tester contract
var ltAddr ethcommon.Address
var ltContract *contracts.LoadTester
var ltContract *loadtester.LoadTester
if anyModeRequiresLoadTestContract(ltp.ParsedModes) || *inputLoadTestParams.ForceContractDeploy {
ltAddr, ltContract, err = getLoadTestContract(ctx, c, tops, cops)
if err != nil {
Expand Down Expand Up @@ -659,19 +658,19 @@ func mainLoop(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client) erro
return nil
}

func getLoadTestContract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (ltAddr ethcommon.Address, ltContract *contracts.LoadTester, err error) {
func getLoadTestContract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (ltAddr ethcommon.Address, ltContract *loadtester.LoadTester, err error) {
ltAddr = ethcommon.HexToAddress(*inputLoadTestParams.LtAddress)

if *inputLoadTestParams.LtAddress == "" {
ltAddr, _, _, err = contracts.DeployLoadTester(tops, c)
ltAddr, _, _, err = loadtester.DeployLoadTester(tops, c)
if err != nil {
log.Error().Err(err).Msg("Failed to create the load testing contract. Do you have the right chain id? Do you have enough funds?")
return
}
}
log.Trace().Interface("contractaddress", ltAddr).Msg("Load test contract address")

ltContract, err = contracts.NewLoadTester(ltAddr, c)
ltContract, err = loadtester.NewLoadTester(ltAddr, c)
if err != nil {
log.Error().Err(err).Msg("Unable to instantiate new contract")
return
Expand All @@ -685,15 +684,13 @@ func getLoadTestContract(ctx context.Context, c *ethclient.Client, tops *bind.Tr
}
func getERC20Contract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (erc20Addr ethcommon.Address, erc20Contract *tokens.ERC20, err error) {
erc20Addr = ethcommon.HexToAddress(*inputLoadTestParams.ERC20Address)
shouldMint := false
if *inputLoadTestParams.ERC20Address == "" {
erc20Addr, _, _, err = tokens.DeployERC20(tops, c, "ERC20TestToken", "T20")
erc20Addr, _, _, err = tokens.DeployERC20(tops, c)
if err != nil {
log.Error().Err(err).Msg("Unable to deploy ERC20 contract")
return
}
// if we're deploying a new ERC 20 we should mint tokens
shouldMint = true
// Tokens already minted and sent to the address of the deployer.
}
log.Trace().Interface("contractaddress", erc20Addr).Msg("ERC20 contract address")

Expand All @@ -703,23 +700,6 @@ func getERC20Contract(ctx context.Context, c *ethclient.Client, tops *bind.Trans
return
}

err = blockUntilSuccessful(ctx, c, func() error {
_, err = erc20Contract.BalanceOf(cops, *inputLoadTestParams.FromETHAddress)
return err
})
if err != nil {
return
}

if !shouldMint {
return
}
_, err = erc20Contract.Mint(tops, metrics.UnitMegaether)
if err != nil {
log.Error().Err(err).Msg("There was an error minting ERC20")
return
}

err = blockUntilSuccessful(ctx, c, func() error {
var balance *big.Int
balance, err = erc20Contract.BalanceOf(cops, *inputLoadTestParams.FromETHAddress)
Expand Down Expand Up @@ -923,10 +903,10 @@ func loadTestDeploy(ctx context.Context, c *ethclient.Client, nonce uint64) (t1
defer func() { t2 = time.Now() }()
if *ltp.CallOnly {
msg := transactOptsToCallMsg(tops)
msg.Data = ethcommon.FromHex(contracts.LoadTesterMetaData.Bin)
msg.Data = ethcommon.FromHex(loadtester.LoadTesterMetaData.Bin)
_, err = c.CallContract(ctx, msg, nil)
} else {
_, _, _, err = contracts.DeployLoadTester(tops, c)
_, _, _, err = loadtester.DeployLoadTester(tops, c)
}
return
}
Expand All @@ -939,9 +919,9 @@ func getCurrentLoadTestFunction() uint64 {
if loadTestModeFunction == inputLoadTestParams.Mode {
return *inputLoadTestParams.Function
}
return contracts.GetRandomOPCode()
return loadtester.GetRandomOPCode()
}
func loadTestFunction(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *contracts.LoadTester) (t1 time.Time, t2 time.Time, err error) {
func loadTestFunction(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *loadtester.LoadTester) (t1 time.Time, t2 time.Time, err error) {
ltp := inputLoadTestParams

chainID := new(big.Int).SetUint64(*ltp.ChainID)
Expand All @@ -962,19 +942,19 @@ func loadTestFunction(ctx context.Context, c *ethclient.Client, nonce uint64, lt
if *ltp.CallOnly {
tops.NoSend = true
var tx *ethtypes.Transaction
tx, err = contracts.CallLoadTestFunctionByOpCode(f, ltContract, tops, *iterations)
tx, err = loadtester.CallLoadTestFunctionByOpCode(f, ltContract, tops, *iterations)
if err != nil {
return
}
msg := txToCallMsg(tx)
_, err = c.CallContract(ctx, msg, nil)
} else {
_, err = contracts.CallLoadTestFunctionByOpCode(f, ltContract, tops, *iterations)
_, err = loadtester.CallLoadTestFunctionByOpCode(f, ltContract, tops, *iterations)
}
return
}

func loadTestCallPrecompiledContracts(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *contracts.LoadTester, useSelectedAddress bool) (t1 time.Time, t2 time.Time, err error) {
func loadTestCallPrecompiledContracts(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *loadtester.LoadTester, useSelectedAddress bool) (t1 time.Time, t2 time.Time, err error) {
var f int
ltp := inputLoadTestParams

Expand All @@ -984,7 +964,7 @@ func loadTestCallPrecompiledContracts(ctx context.Context, c *ethclient.Client,
if useSelectedAddress {
f = int(*ltp.Function)
} else {
f = contracts.GetRandomPrecompiledContractAddress()
f = loadtester.GetRandomPrecompiledContractAddress()
}

tops, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
Expand All @@ -1000,19 +980,19 @@ func loadTestCallPrecompiledContracts(ctx context.Context, c *ethclient.Client,
if *ltp.CallOnly {
tops.NoSend = true
var tx *ethtypes.Transaction
tx, err = contracts.CallPrecompiledContracts(f, ltContract, tops, *iterations, privateKey)
tx, err = loadtester.CallPrecompiledContracts(f, ltContract, tops, *iterations, privateKey)
if err != nil {
return
}
msg := txToCallMsg(tx)
_, err = c.CallContract(ctx, msg, nil)
} else {
_, err = contracts.CallPrecompiledContracts(f, ltContract, tops, *iterations, privateKey)
_, err = loadtester.CallPrecompiledContracts(f, ltContract, tops, *iterations, privateKey)
}
return
}

func loadTestInc(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *contracts.LoadTester) (t1 time.Time, t2 time.Time, err error) {
func loadTestInc(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *loadtester.LoadTester) (t1 time.Time, t2 time.Time, err error) {
ltp := inputLoadTestParams

chainID := new(big.Int).SetUint64(*ltp.ChainID)
Expand Down Expand Up @@ -1043,7 +1023,7 @@ func loadTestInc(ctx context.Context, c *ethclient.Client, nonce uint64, ltContr
return
}

func loadTestStore(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *contracts.LoadTester) (t1 time.Time, t2 time.Time, err error) {
func loadTestStore(ctx context.Context, c *ethclient.Client, nonce uint64, ltContract *loadtester.LoadTester) (t1 time.Time, t2 time.Time, err error) {
ltp := inputLoadTestParams

chainID := new(big.Int).SetUint64(*ltp.ChainID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/loadtest/uniswapv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
uniswapv3loadtest "github.com/maticnetwork/polygon-cli/cmd/loadtest/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts/src/uniswapv3"
"github.com/rs/zerolog/log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/loadtest/uniswapv3/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/maticnetwork/polygon-cli/contracts/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts/src/uniswapv3"
"github.com/rs/zerolog/log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/loadtest/uniswapv3/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/maticnetwork/polygon-cli/contracts/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts/src/uniswapv3"
"github.com/rs/zerolog/log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/loadtest/uniswapv3/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/maticnetwork/polygon-cli/contracts/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts/src/uniswapv3"
"github.com/rs/zerolog/log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/loadtest/uniswapv3/swapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/maticnetwork/polygon-cli/contracts/uniswapv3"
"github.com/maticnetwork/polygon-cli/contracts/src/uniswapv3"
"github.com/rs/zerolog/log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcfuzz/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $ cast send \
--rpc-url localhost:8545 \
--json \
--create \
"$(cat ./contracts/tokens/ERC20/ERC20.bin)" | jq
"$(cat ./contracts/src/tokens/ERC20.bin)" | jq
```

Once this has been completed this will be the address of the contract: `0x6fda56c57b0acadb96ed5624ac500c0429d59429`.
Expand Down
14 changes: 14 additions & 0 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
58 changes: 58 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Contracts

Smart contracts used to perform different types of tests:

- `LoadTester` to call various opcodes, precompiles, and store random data.
- `Tokens` to perform ERC20 transfers or ERC721 mints for example.
- `UniswapV3` to deploy the full UniswapV3 contract suite and perform some swaps.
- Other: `asm` and `yul`, contracts written in other languages than Solidity.

## LoadTester

Generate go bindings for the `LoadTester` contract.

```sh
$ FOUNDRY_PROFILE=lite forge build --contracts ./src/loadtester/LoadTester.sol \
&& cat ./out/LoadTester.sol/LoadTester.json| jq -r '.abi' > ./src/loadtester/LoadTester.abi \
&& cat ./out/LoadTester.sol/LoadTester.json| jq -r '.bytecode.object' > ./src/loadtester/LoadTester.bin \
&& abigen \
--abi ./src/loadtester/LoadTester.abi \
--bin ./src/loadtester/LoadTester.bin \
--pkg loadtester \
--type loadTester \
--out ./src/loadtester/loadTester.go
```

## Tokens

Generate go bindings for the `ERC20` contract.

```sh
$ forge build --contracts ./src/tokens/ERC20.sol \
&& cat ./out/ERC20.sol/ERC20.json| jq -r '.abi' > ./src/tokens/ERC20.abi \
&& cat ./out/ERC20.sol/ERC20.json| jq -r '.bytecode.object' > ./src/tokens/ERC20.bin \
&& abigen \
--abi ./src/tokens/ERC20.abi \
--bin ./src/tokens/ERC20.bin \
--pkg tokens \
--type ERC20 \
--out ./src/tokens/ERC20.go
```

Generate go bindings for the `ERC721` contract.

```sh
$ forge build --contracts ./src/tokens/ERC721.sol \
&& cat ./out/ERC721.sol/ERC721.json| jq -r '.abi' > ./src/tokens/ERC721.abi \
&& cat ./out/ERC721.sol/ERC721.json| jq -r '.bytecode.object' > ./src/tokens/ERC721.bin \
&& abigen \
--abi ./src/tokens/ERC721.abi \
--bin ./src/tokens/ERC721.bin \
--pkg tokens \
--type ERC721 \
--out ./src/tokens/ERC721.go
```

## UniswapV3

The UniswapV3 go bindings have been generated in a certain way. Check `uniswapv3/README.org` for more details.
12 changes: 12 additions & 0 deletions contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
remappings = [
'@openzeppelin/=lib/openzeppelin-contracts/contracts',
]

# Lite profile with Yul optimizer disabled.
[profile.lite.optimizer_details]
yul = false
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions contracts/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at 932fdd
1,819 changes: 0 additions & 1,819 deletions contracts/loadtester.go

This file was deleted.

Loading

0 comments on commit f3e4b0b

Please sign in to comment.