Skip to content

Commit

Permalink
Merge pull request #61 from Lorenzo-Protocol/oris/token
Browse files Browse the repository at this point in the history
feat(token): token module for native coin and erc20 token conversion
  • Loading branch information
sheldonleedev authored Jul 19, 2024
2 parents 70c4ce3 + 9b32836 commit 56cf3a2
Show file tree
Hide file tree
Showing 64 changed files with 12,970 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ repos:
stages: [ commit ]
always_run: true
- repo: https://github.com/golangci/golangci-lint
rev: v1.59.1
rev: v1.53.3
hooks:
- id: golangci-lint
stages: [ push ]
always_run: true
51 changes: 51 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,54 @@ format:
$(golangci_lint_cmd) run --fix

.PHONY: lint lint-fix format

###############################################################################
### Compile Solidity Contracts ###
###############################################################################

# Currently only support for compiling ERC20 contracts.
CONTRACTS_PRJ := erc20
CONTRACTS_DIR := contracts/$(CONTRACTS_PRJ)
EVM_VERSION := paris
COMPILED_DIR := $(CONTRACTS_DIR)/compiled_contracts
TMP_DIR := $(CONTRACTS_DIR)/tmp
TMP_COMPILED := $(TMP_DIR)/compiled.json
TMP_JSON := $(TMP_DIR)/tmp.json

contracts-compile: contracts-check-project-name contracts-install-dependencies contracts-create-json contracts-clean-up

contracts-check-project-name:
@if [ -z "$(CONTRACTS_PRJ)" ]; then \
echo "CONTRACTS_PRJ is not set. Please set it to the project name of the contracts you want to compile."; \
exit 1; \
fi

# Install open-zeppelin solidity contracts
contracts-install-dependencies:
@echo "Importing open-zeppelin contracts..."
@cd $(CONTRACTS_DIR) && npm install
@mv $(CONTRACTS_DIR)/node_modules/@openzeppelin $(CONTRACTS_DIR)
@rm -rf $(CONTRACTS_DIR)/node_modules $(CONTRACTS_DIR)/package-lock.json

# Compile, filter out and format contracts into the following format.
contracts-create-json:
@for c in $(shell ls $(CONTRACTS_DIR) | grep '\.sol' | sed 's/.sol//g'); do \
mkdir -p $(TMP_DIR) ;\
mkdir -p $(COMPILED_DIR) ;\
echo "\nCompiling solidity contract $${c}..." ;\
solc --evm-version $(EVM_VERSION) --combined-json abi,bin $(CONTRACTS_DIR)/$${c}.sol > $(TMP_COMPILED);\
echo "Formatting JSON..." ;\
get_contract=$$(jq '.contracts["$(CONTRACTS_DIR)/'$$c'.sol:'$$c'"]' $(TMP_COMPILED)) ;\
add_contract_name=$$(echo $$get_contract | jq '. + { "contractName": "'$$c'" }') ;\
echo $$add_contract_name | jq > $(TMP_JSON) ;\
abi_string=$$(echo $$add_contract_name | jq -cr '.abi') ;\
echo $$add_contract_name | jq --arg newval "$$abi_string" '.abi = $$newval' > $(TMP_JSON) ;\
mv $(TMP_JSON) $(COMPILED_DIR)/$${c}.json ;\
rm -rf $(TMP_DIR) ;\
done

# Clean tmp files
contracts-clean-up:
@echo "Cleaning up..."
@rm -rf $(CONTRACTS_DIR)/@openzeppelin
@echo "Finished cleaning up."
75 changes: 46 additions & 29 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

/* ------------------------------ ibc imports ----------------------------- */
ibctransfer "github.com/cosmos/ibc-go/v7/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcclient "github.com/cosmos/ibc-go/v7/modules/core/02-client"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand Down Expand Up @@ -112,6 +109,11 @@ import (
feetypes "github.com/Lorenzo-Protocol/lorenzo/x/fee/types"
plankeeper "github.com/Lorenzo-Protocol/lorenzo/x/plan/keeper"
plantypes "github.com/Lorenzo-Protocol/lorenzo/x/plan/types"

ics20wrapper "github.com/Lorenzo-Protocol/lorenzo/x/ibctransfer"
ics20wrapperkeeper "github.com/Lorenzo-Protocol/lorenzo/x/ibctransfer/keeper"
tokenkeeper "github.com/Lorenzo-Protocol/lorenzo/x/token/keeper"
tokentypes "github.com/Lorenzo-Protocol/lorenzo/x/token/types"
)

var (
Expand Down Expand Up @@ -157,22 +159,26 @@ type LorenzoApp struct {
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
ParamsKeeper paramskeeper.Keeper
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
EvidenceKeeper *evidencekeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
FeeGrantKeeper feegrantkeeper.Keeper
ConsensusParamsKeeper consensuskeeper.Keeper

// IBC keepers
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
// TransferKeeper ibctransferkeeper.Keeper
ICS20WrapperKeeper *ics20wrapperkeeper.Keeper

// Ethermint keepers
EvmKeeper *evmkeeper.Keeper
FeeMarketKeeper feemarketkeeper.Keeper

// self keeper
// Lorenzo keeper
BTCLightClientKeeper btclightclientkeeper.Keeper
FeeKeeper *feekeeper.Keeper
AgentKeeper agentkeeper.Keeper
BTCStakingKeeper btcstakingkeeper.Keeper
PlanKeeper *plankeeper.Keeper
TokenKeeper *tokenkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand All @@ -181,8 +187,6 @@ type LorenzoApp struct {
// the module manager
mm *module.Manager

transferModule ibctransfer.AppModule

// module configurator
configurator module.Configurator
}
Expand Down Expand Up @@ -243,15 +247,16 @@ func NewLorenzoApp(
crisistypes.StoreKey,
govtypes.StoreKey,
paramstypes.StoreKey,
ibcexported.StoreKey,
upgradetypes.StoreKey,
feegrant.StoreKey,
consensustypes.StoreKey,

evidencetypes.StoreKey,
ibctransfertypes.StoreKey,
capabilitytypes.StoreKey,

// ibc keys
ibcexported.StoreKey,
ibctransfertypes.StoreKey,

// ethermint keys
evmtypes.StoreKey,
feemarkettypes.StoreKey,
Expand All @@ -260,8 +265,9 @@ func NewLorenzoApp(
btcstakingtypes.StoreKey,
agenttypes.StoreKey,

// self modules
// lorenzo module keys
plantypes.StoreKey,
tokentypes.StoreKey,
)

tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey, evmtypes.TransientKey, feemarkettypes.TransientKey)
Expand Down Expand Up @@ -416,19 +422,6 @@ func NewLorenzoApp(
scopedIBCKeeper,
)

// Create IBC Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
scopedTransferKeeper,
)

// Create evidence Keeper for to register the IBC light client misbehaviour evidence route
app.EvidenceKeeper = evidencekeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -461,8 +454,6 @@ func NewLorenzoApp(

app.GovKeeper.SetLegacyRouter(govRouter)

app.transferModule = ibctransfer.NewAppModule(app.TransferKeeper)

btclightclientKeeper := btclightclientkeeper.NewKeeper(
appCodec,
keys[btclightclienttypes.StoreKey],
Expand Down Expand Up @@ -498,12 +489,38 @@ func NewLorenzoApp(
app.AgentKeeper,
)

transferStack := ibctransfer.NewIBCModule(app.TransferKeeper)
app.TokenKeeper = tokenkeeper.NewKeeper(
appCodec,
keys[tokentypes.StoreKey],
authtypes.NewModuleAddress(govtypes.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.EvmKeeper,
)

app.EvmKeeper.SetHooks(evmkeeper.NewMultiEvmHooks(
app.TokenKeeper.Hooks(),
))

// ibc transfer keeper wrapper
app.ICS20WrapperKeeper = ics20wrapperkeeper.NewKeeper(
appCodec,
keys[ibctransfertypes.StoreKey],
app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
scopedTransferKeeper,
app.TokenKeeper,
)

transferStack := ics20wrapper.NewIBCModule(app.ICS20WrapperKeeper)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
// this line is used by starport scaffolding # ibc/app/router
app.IBCKeeper.SetRouter(ibcRouter)

/**** Module Options ****/
Expand Down
32 changes: 22 additions & 10 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ import (
feetypes "github.com/Lorenzo-Protocol/lorenzo/x/fee/types"
"github.com/Lorenzo-Protocol/lorenzo/x/plan"
plantypes "github.com/Lorenzo-Protocol/lorenzo/x/plan/types"

ics20wrapper "github.com/Lorenzo-Protocol/lorenzo/x/ibctransfer"
"github.com/Lorenzo-Protocol/lorenzo/x/token"
tokentypes "github.com/Lorenzo-Protocol/lorenzo/x/token/types"
)

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down Expand Up @@ -110,21 +114,26 @@ var (
ibctm.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
ibctransfer.AppModuleBasic{},
vesting.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic

// ibc modules
ibc.AppModuleBasic{},
ics20wrapper.AppModuleBasic{AppModuleBasic: &ibctransfer.AppModuleBasic{}}, // ibc transfer wrapper

// Ethermint modules
evm.AppModuleBasic{},
feemarket.AppModuleBasic{},

// slef modules
// lorenzo modules
btclightclient.AppModuleBasic{},
btcstaking.AppModuleBasic{},
fee.AppModuleBasic{},
agent.AppModuleBasic{},
plan.AppModuleBasic{},
token.AppModuleBasic{},
)

// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
Expand All @@ -139,8 +148,9 @@ var (
evmtypes.ModuleName: {authtypes.Minter, authtypes.Burner}, // used for secure addition and subtraction of balance using module account
btcstakingtypes.ModuleName: {authtypes.Minter, authtypes.Burner},

// self module
plantypes.ModuleName: nil,
// lorenzo module
plantypes.ModuleName: nil,
tokentypes.ModuleName: {authtypes.Minter, authtypes.Burner},
}
)

Expand Down Expand Up @@ -215,23 +225,23 @@ func appModules(
),
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(*app.EvidenceKeeper),
ibc.NewAppModule(app.IBCKeeper),
params.NewAppModule(app.ParamsKeeper),

app.transferModule,

// this line is used by starport scaffolding # stargate/app/appModule
// ibc modules
ibc.NewAppModule(app.IBCKeeper),
ics20wrapper.NewAppModule(app.ICS20WrapperKeeper), // wrapper module on ibc transfer

// Ethermint app modules
evm.NewAppModule(app.EvmKeeper, app.AccountKeeper, app.GetSubspace(evmtypes.ModuleName).WithKeyTable(evmtypes.ParamKeyTable())),
feemarket.NewAppModule(app.FeeMarketKeeper, app.GetSubspace(feemarkettypes.ModuleName).WithKeyTable(feemarkettypes.ParamKeyTable())),

// self modules
// lorenzo modules
btclightclient.NewAppModule(appCodec, app.BTCLightClientKeeper),
fee.NewAppModule(appCodec, app.FeeKeeper),
btcstaking.NewAppModule(appCodec, app.BTCStakingKeeper),
agent.NewAppModule(appCodec, app.AgentKeeper),
plan.NewAppModule(appCodec, app.PlanKeeper),
token.NewAppModule(appCodec, app.TokenKeeper),
}
}

Expand Down Expand Up @@ -275,6 +285,7 @@ func orderBeginBlockers() []string {
feetypes.ModuleName,
agenttypes.ModuleName,
plantypes.ModuleName,
tokentypes.ModuleName,
}
}

Expand Down Expand Up @@ -315,6 +326,7 @@ func orderEndBlockers() []string {
feetypes.ModuleName,
agenttypes.ModuleName,
plantypes.ModuleName,
tokentypes.ModuleName,
}
}

Expand Down Expand Up @@ -353,12 +365,12 @@ func orderInitBlockers() []string {
evidencetypes.ModuleName,

// self module

btclightclienttypes.ModuleName,
btcstakingtypes.ModuleName,
feetypes.ModuleName,
agenttypes.ModuleName,
plantypes.ModuleName,
tokentypes.ModuleName,

// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,
Expand Down
Loading

0 comments on commit 56cf3a2

Please sign in to comment.