Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reorganize contracts folder #123

Merged
merged 20 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,36 @@ test: ## Run tests.
go test ./... -coverprofile=coverage.out
go tool cover -func coverage.out

##@ Documentation
##@ Generation

.PHONY: gen-doc
gen-doc: ## Generate documentation for `polycli`.
go run docutil/*.go

.PHONY: gen-loadtest-modes
gen-loadtest-modes: ## Generate loadtest modes strings.
cd cmd/loadtest && stringer -type=loadTestMode

.PHONY: gen-go-bindings
gen-go-bindings: ## Generate go bindings for smart contracts.
$(call gen_go_binding,contracts/tokens/ERC20,ERC20,tokens,contracts/tokens)
$(call gen_go_binding,contracts/tokens/ERC721,ERC721,tokens,contracts/tokens)
$(call gen_go_binding,contracts/loadtester,LoadTester,contracts,contracts)

# Generate go binding.
# - $1: input_dir
# - $2: name
# - $3: pkg
# - $4: output_dir
define gen_go_binding
solc $1/$2.sol --abi --bin --output-dir $1 --overwrite --evm-version paris
abigen --abi $1/$2.abi --bin $1/$2.bin --pkg $3 --type $2 --out $4/$2.go
endef

# Example for the ERC20 contract.
# solc contracts/tokens/ERC20/ERC20.sol --abi --bin --output-dir contracts/tokens/ERC20 --overwrite
# abigen --abi contracts/tokens/ERC20/ERC20.abi --bin contracts/tokens/ERC20/ERC20.bin --pkg tokens --type ERC20 --out contracts/tokens/ERC20.go

# Example for the LoadTester contract.
# solc contracts/loadtester/LoadTester.sol --abi --bin --output-dir contracts/loadtester --overwrite
# abigen --abi contracts/loadtester/LoadTester.abi --bin contracts/loadtester/LoadTester.bin --pkg contracts --type LoadTester --out contracts/LoadTester.go
59 changes: 49 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Polycli

We run a lot of different blockchain technologies. Different tools often have inconsistent tooling and this makes automation and operations painful. The goal of this codebase is to standardize some of our commonly needed tools and provide interfaces and formats.

# Summary
## Summary

- [Install](#install)
- [Features](#features)
- [Testing](#testing)
- [Reference](#reference)
- [Contributing](#contributing)
- [References](#references)

# Install
## Install

Requirements:

Expand All @@ -29,7 +32,7 @@ By default, the commands will be in `$HOME/go/bin/`, so for ease, we recommend a
export PATH="$HOME/go/bin:$PATH"
```

# Features
## Features

![polycli monitor](doc/assets/monitor.gif)

Expand Down Expand Up @@ -75,13 +78,13 @@ Note: Do not modify this section! It is auto-generated by `cobra` using `make ge

</generated>

# Testing
## Testing

To test the features of `polycli`, we'll run geth in `dev` mode but you can run any node you want.

```bash
$ make geth
INFO [07-10|10:43:12.499] Starting Geth in ephemeral dev mode...
INFO [07-10|10:43:12.499] Starting Geth in ephemeral dev mode...
...
```

Expand All @@ -108,7 +111,7 @@ First, wait for the IPC endpoint to open.

```bash
INFO [07-10|10:44:29.250] Starting peer-to-peer node instance=Geth/v1.12.0-stable/darwin-amd64/go1.20.4
WARN [07-10|10:44:29.250] P2P server will be useless, neither dialing nor listening
WARN [07-10|10:44:29.250] P2P server will be useless, neither dialing nor listening
INFO [07-10|10:44:29.255] Stored checkpoint snapshot to disk number=0 hash=039839..dcb5c1
DEBUG[07-10|10:44:29.255] IPCs registered namespaces=admin,debug,web3,eth,txpool,clique,miner,net,engine
INFO [07-10|10:44:29.256] IPC endpoint opened url=/var/folders/7m/3_x4ns7557x52hb6vncqkx8h0000gn/T/geth.ipc
Expand All @@ -124,11 +127,11 @@ Welcome to the Geth JavaScript console!
instance: Geth/v1.12.0-stable/darwin-amd64/go1.20.4
coinbase: 0x760f66cae63cb561ed0ef29d5e005f44215e2ba2
at block: 8 (Mon Jul 10 2023 10:49:07 GMT+0200 (CEST))
datadir:
datadir:
modules: admin:1.0 clique:1.0 debug:1.0 engine:1.0 eth:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0

To exit, press ctrl-d or type exit
>
>
```

From there, you can do anything such as funding the default load testing account with some currency.
Expand All @@ -147,7 +150,43 @@ You can then generate some load to make sure that blocks with transactions are b
$ polycli loadtest --verbosity 700 --chain-id 1337 --concurrency 1 --requests 1000 --rate-limit 5 --mode c http://127.0.0.1:8545
```

# Reference
## Contributing

The `Makefile` is here to assist you to build the project, run tests, generate documentation or go bindings, etc.

```sh
$ make
Usage:
make <target>
help Display this help.

Build
generate Generate protobuf stubs.
build Build go binary.
install Install the go binary.
cross Cross-compile go binaries using CGO.
simplecross Cross-compile go binaries without using CGO.
clean Clean the binary folder.

Test
test Run tests.

Generation
gen-doc Generate documentation for `polycli`.
gen-loadtest-modes Generate loadtest modes strings.
gen-go-bindings Generate go bindings for smart contracts.

Lint
lint Run linters.

Clients
geth Start a local geth node.
avail Start a local avail node.
geth-loadtest Fund test account with 5k ETH and run loadtest against an EVM/Geth chain.
avail-loadtest Run loadtest against an Avail chain.
```

## References

Sending some value to the default load testing account.

Expand Down
33 changes: 17 additions & 16 deletions cmd/loadtest/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"time"

"github.com/maticnetwork/polygon-cli/contracts/tokens"
"github.com/maticnetwork/polygon-cli/rpctypes"

_ "embed"
Expand Down Expand Up @@ -92,7 +93,7 @@ func characterToLoadTestMode(mode string) (loadTestMode, error) {
case "rpc":
return loadTestModeRPC, nil
default:
return 0, fmt.Errorf("Unrecognized load test mode: %s", mode)
return 0, fmt.Errorf("unrecognized load test mode: %s", mode)
}
}

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

var erc20Addr ethcommon.Address
var erc20Contract *contracts.ERC20
var erc20Contract *tokens.ERC20
if mode == loadTestModeERC20 || mode == loadTestModeRandom {
erc20Addr, erc20Contract, err = getERC20Contract(ctx, c, tops, cops)
if err != nil {
Expand All @@ -468,7 +469,7 @@ func mainLoop(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client) erro
}

var erc721Addr ethcommon.Address
var erc721Contract *contracts.ERC721
var erc721Contract *tokens.ERC721
if mode == loadTestModeERC721 || mode == loadTestModeRandom {
erc721Addr, erc721Contract, err = getERC721Contract(ctx, c, tops, cops)
if err != nil {
Expand All @@ -484,7 +485,7 @@ func mainLoop(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client) erro
return err
}
if len(recallTransactions) == 0 {
return fmt.Errorf("We weren't able to fetch any recall transactions")
return fmt.Errorf("we weren't able to fetch any recall transactions")
}
log.Debug().Int("txs", len(recallTransactions)).Msg("retrieved transactions for total recall")
}
Expand Down Expand Up @@ -655,11 +656,11 @@ func getLoadTestContract(ctx context.Context, c *ethclient.Client, tops *bind.Tr

return
}
func getERC20Contract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (erc20Addr ethcommon.Address, erc20Contract *contracts.ERC20, err error) {
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 = contracts.DeployERC20(tops, c)
erc20Addr, _, _, err = tokens.DeployERC20(tops, c, "ERC20TestToken", "T20")
if err != nil {
log.Error().Err(err).Msg("Unable to deploy ERC20 contract")
return
Expand All @@ -669,7 +670,7 @@ func getERC20Contract(ctx context.Context, c *ethclient.Client, tops *bind.Trans
}
log.Trace().Interface("contractaddress", erc20Addr).Msg("ERC20 contract address")

erc20Contract, err = contracts.NewERC20(erc20Addr, c)
erc20Contract, err = tokens.NewERC20(erc20Addr, c)
if err != nil {
log.Error().Err(err).Msg("Unable to instantiate new erc20 contract")
return
Expand Down Expand Up @@ -707,11 +708,11 @@ func getERC20Contract(ctx context.Context, c *ethclient.Client, tops *bind.Trans

return
}
func getERC721Contract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (erc721Addr ethcommon.Address, erc721Contract *contracts.ERC721, err error) {
func getERC721Contract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (erc721Addr ethcommon.Address, erc721Contract *tokens.ERC721, err error) {
erc721Addr = ethcommon.HexToAddress(*inputLoadTestParams.ERC721Address)
shouldMint := true
if *inputLoadTestParams.ERC721Address == "" {
erc721Addr, _, _, err = contracts.DeployERC721(tops, c)
erc721Addr, _, _, err = tokens.DeployERC721(tops, c)
if err != nil {
log.Error().Err(err).Msg("Unable to deploy ERC721 contract")
return
Expand All @@ -720,7 +721,7 @@ func getERC721Contract(ctx context.Context, c *ethclient.Client, tops *bind.Tran
}
log.Trace().Interface("contractaddress", erc721Addr).Msg("ERC721 contract address")

erc721Contract, err = contracts.NewERC721(erc721Addr, c)
erc721Contract, err = tokens.NewERC721(erc721Addr, c)
if err != nil {
log.Error().Err(err).Msg("Unable to instantiate new erc721 contract")
return
Expand Down Expand Up @@ -1089,7 +1090,7 @@ func loadTestStore(ctx context.Context, c *ethclient.Client, nonce uint64, ltCon
return
}

func loadTestERC20(ctx context.Context, c *ethclient.Client, nonce uint64, erc20Contract *contracts.ERC20, ltAddress ethcommon.Address) (t1 time.Time, t2 time.Time, err error) {
func loadTestERC20(ctx context.Context, c *ethclient.Client, nonce uint64, erc20Contract *tokens.ERC20, ltAddress ethcommon.Address) (t1 time.Time, t2 time.Time, err error) {
ltp := inputLoadTestParams

to := ltp.ToETHAddress
Expand Down Expand Up @@ -1127,7 +1128,7 @@ func loadTestERC20(ctx context.Context, c *ethclient.Client, nonce uint64, erc20
return
}

func loadTestERC721(ctx context.Context, c *ethclient.Client, nonce uint64, erc721Contract *contracts.ERC721, ltAddress ethcommon.Address) (t1 time.Time, t2 time.Time, err error) {
func loadTestERC721(ctx context.Context, c *ethclient.Client, nonce uint64, erc721Contract *tokens.ERC721, ltAddress ethcommon.Address) (t1 time.Time, t2 time.Time, err error) {
ltp := inputLoadTestParams
iterations := ltp.Iterations

Expand Down Expand Up @@ -1258,15 +1259,15 @@ func loadTestRPC(ctx context.Context, c *ethclient.Client, nonce uint64, ia *Ind
Msg("retrieve contract addresses")
cops := new(bind.CallOpts)
cops.Context = ctx
var erc721Contract *contracts.ERC721
var erc20Contract *contracts.ERC20
var erc721Contract *tokens.ERC721
var erc20Contract *tokens.ERC20

erc721Contract, err = contracts.NewERC721(erc721Addr, c)
erc721Contract, err = tokens.NewERC721(erc721Addr, c)
if err != nil {
log.Error().Err(err).Msg("Unable to instantiate new erc721 contract")
return
}
erc20Contract, err = contracts.NewERC20(erc20Addr, c)
erc20Contract, err = tokens.NewERC20(erc20Addr, c)
if err != nil {
log.Error().Err(err).Msg("Unable to instantiate new erc20 contract")
return
Expand Down
1 change: 0 additions & 1 deletion contracts/ERC20.abi

This file was deleted.

Loading