Skip to content

Commit

Permalink
wrapping native tokens in erc20 (crypto-org-chain#34)
Browse files Browse the repository at this point in the history
* wrapping native tokens in erc20

Closes crypto-org-chain#24

- add dapp tools to nixpkgs
- create CronosERC20 contract
- embed CronosERC20 in native module
- add keeper methods to transfer tokens between native and contract
- unit test

cleanup

cleanup

check contract artifacts up-to-date in CI

add gomod2nix check

make auto-deploy optional

update submodule

rename CronosERC20 to ModuleERC20

add external contract mapping

fix unit tests

fix contract test in CI

fix upload-artifacts

test ConvertERC20ToNative

fix unit test, burn shouldn't need approval

rename

try to fix integration test

fix misleading comments

change magic number to constant

validate native token denom

make GetExternalContractByDenom/GetAutoContractByDenom private

* ERC20 -> CRC20

* SendCoin -> ConvertCoin

* fix unit test

* reuse ibc denom validation logic

* fix ibc denom in unit test

* remove ConvertCoinsFromCRC20ToNative

* rename mint_by_native -> mint_by_cronos_module

* emit burn event

* fix burn and unit test

* store contract to denom reversed index
  • Loading branch information
yihuang authored Sep 8, 2021
1 parent 0b79cc9 commit 8101ac6
Show file tree
Hide file tree
Showing 36 changed files with 1,354 additions and 199 deletions.
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eval "$(lorri direnv --shell-file integration_tests/shell.nix)"
TMPDIR=/tmp
20 changes: 18 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ jobs:
.mod
.sum
- name: test & coverage report creation
run: |
make test
run: make test
if: "env.GIT_DIFF != ''"
- name: filter out proto files
run: |
Expand All @@ -75,3 +74,20 @@ jobs:
run: nix run -f ./nix gomod2nix -c gomod2nix
- name: check working directory is clean
uses: numtide/clean-git-action@v1

contracts:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: cachix/install-nix-action@v12
- uses: cachix/cachix-action@v8
with:
name: cronos
extraPullNames: dapp
signingKey: "${{ secrets.CACHIX_SIGNING_KEY }}"
- name: test contracts
run: make test-cronos-contracts
- name: build contracts
run: make gen-cronos-contracts
- name: check working directory is clean
uses: numtide/clean-git-action@v1
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
if: failure()
run: tar cfz debug_files.tar.gz -C /tmp/pytest-of-runner .
- uses: actions/upload-artifact@v2
if: failure()
with:
name: debug-files
path: debug_files.tar.gz
Expand Down
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "contracts/lib/ds-test"]
path = contracts/lib/ds-test
url = https://github.com/dapphub/ds-test
[submodule "contracts/lib/ds-token"]
path = contracts/lib/ds-token
url = https://github.com/dapphub/ds-token
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,17 @@ run-integration-tests:
@nix-shell ./integration_tests/shell.nix --run ./scripts/run-integration-tests

.PHONY: run-integration-tests

###############################################################################
### Utility ###
###############################################################################

test-cronos-contracts:
@git submodule update --init --recursive
@nix-shell ./contracts/shell.nix --pure --run "make -C contracts test"

gen-cronos-contracts:
@git submodule update --init --recursive
@nix-shell ./contracts/shell.nix --pure --run ./scripts/gen-cronos-contracts

.PHONY: gen-cronos-contracts test-cronos-contracts
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ func New(
keys[cronosmoduletypes.StoreKey],
keys[cronosmoduletypes.MemStoreKey],
app.GetSubspace(cronosmoduletypes.ModuleName),
app.GetSubspace(evmtypes.ModuleName),
app.BankKeeper,
app.TransferKeeper,
app.EvmKeeper,
)
cronosModule := cronosmodule.NewAppModule(appCodec, app.CronosKeeper)

Expand Down
6 changes: 5 additions & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import (
)

const (
SimAppChainID = "simulation_777-1"
SimAppChainID = "simulation_777-1"
TestAppChainID = "cronos_777-1"
)

// DefaultConsensusParams defines the default Tendermint consensus params used in
Expand Down Expand Up @@ -78,6 +79,7 @@ func Setup(isCheckTx bool) *App {
// Initialize the chain
app.InitChain(
abci.RequestInitChain{
ChainId: TestAppChainID,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
Expand Down Expand Up @@ -151,6 +153,7 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
// init chain will set the validator set and initialize the genesis accounts
app.InitChain(
abci.RequestInitChain{
ChainId: TestAppChainID,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
Expand Down Expand Up @@ -191,6 +194,7 @@ func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...ba

app.InitChain(
abci.RequestInitChain{
ChainId: TestAppChainID,
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
Expand Down
1 change: 1 addition & 0 deletions contracts/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol linguist-language=Solidity
1 change: 1 addition & 0 deletions contracts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/out
5 changes: 5 additions & 0 deletions contracts/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SOLC_VERSION := solc-0.6.11
all :; dapp --use ${SOLC_VERSION} build
clean :; dapp clean
test :; dapp --use ${SOLC_VERSION} test
deploy :; dapp create CronosERC20
1 change: 1 addition & 0 deletions contracts/lib/ds-test
Submodule ds-test added at 0a5da5
1 change: 1 addition & 0 deletions contracts/lib/ds-token
Submodule ds-token added at a5e709
9 changes: 9 additions & 0 deletions contracts/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ system ? builtins.currentSystem, pkgs ? import ../nix { inherit system; } }:
pkgs.mkShell {
buildInputs = [
pkgs.jq
pkgs.dapp
pkgs.solc-static-versions.solc_0_6_11
];
}

26 changes: 26 additions & 0 deletions contracts/src/ModuleCRC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.6.11;

import "ds-token/token.sol";

contract ModuleCRC20 is DSToken {
// sha256('cronos')[:20]
address constant module_address = 0x6526B48f897f6e08067dF00A1821d783cbC2af8b;

constructor(string memory denom, uint8 decimals_) DSToken(denom) public {
decimals = decimals_;
}

function mint_by_cronos_module(address addr, uint amount) public {
require(msg.sender == module_address);
mint(addr, amount);
}

function burn_by_cronos_module(address addr, uint amount) public {
require(msg.sender == module_address);
// Deduct user's balance without approval
require(balanceOf[addr] >= amount, "ds-token-insufficient-balance");
balanceOf[addr] = sub(balanceOf[addr], amount);
totalSupply = sub(totalSupply, amount);
emit Burn(addr, amount);
}
}
25 changes: 25 additions & 0 deletions contracts/src/ModuleCRC20.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity ^0.6.11;

import "ds-test/test.sol";

import "./ModuleCRC20.sol";

contract ModuleCRC20Test is DSTest {
ModuleCRC20 token;

function setUp() public {
token = new ModuleCRC20("gravity0x0", 0);
}

function test_basic_sanity() public {
assertEq(uint(token.decimals()), uint(0));
}

function testFail_mint_by_cronos_module() public {
token.mint_by_cronos_module(0x208AE63c976d145AB328afdcE251c7051D8E452D, 100);
}

function testFail_burn_by_cronos_module() public {
token.burn_by_cronos_module(0x208AE63c976d145AB328afdcE251c7051D8E452D, 100);
}
}
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ go 1.16
require (
github.com/armon/go-metrics v0.3.9
github.com/cosmos/cosmos-sdk v0.43.0
github.com/cosmos/ibc-go v1.0.0
github.com/cosmos/ibc-go v1.0.1
github.com/ethereum/go-ethereum v1.10.3
github.com/gogo/protobuf v1.3.3
github.com/google/go-cmp v0.5.6 // indirect
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/peggyjv/gravity-bridge/module v0.1.21
Expand All @@ -17,10 +16,10 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tendermint/spm v0.0.0-20210524110815-6d7452d2dc4a
github.com/tendermint/tendermint v0.34.11
github.com/tendermint/tendermint v0.34.12
github.com/tendermint/tm-db v0.6.4
github.com/tharsis/ethermint v0.4.2-0.20210817172937-b8fc58a75c33
google.golang.org/genproto v0.0.0-20210816143620-e15ff196659d
github.com/tharsis/ethermint v0.4.2-0.20210831155846-516ffe2bf176
google.golang.org/genproto v0.0.0-20210825212027-de86158e7fda
google.golang.org/grpc v1.40.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
Loading

0 comments on commit 8101ac6

Please sign in to comment.