Skip to content

Commit

Permalink
script, example contract, checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Oct 1, 2023
1 parent 687c947 commit 42d8296
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 23 deletions.
3 changes: 3 additions & 0 deletions x/cw-hooks/contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contract

<https://github.com/Reecepbcups/cw-juno-staking-hooks-example>
Binary file not shown.
17 changes: 11 additions & 6 deletions x/cw-hooks/keeper/contracts.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) SetContract(ctx sdk.Context, keyPrefix []byte, contractAddr sdk.AccAddress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
store.Set(contractAddr, []byte{})
store.Set(contractAddr.Bytes(), []byte{})
}

func (k Keeper) IsContractRegistered(ctx sdk.Context, keyPrefix []byte, contractAddr sdk.AccAddress) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
return store.Has(contractAddr)
return store.Has(contractAddr.Bytes())
}

func (k Keeper) IterateContracts(
Expand All @@ -25,7 +27,7 @@ func (k Keeper) IterateContracts(
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
keyAddr := iterator.Key()
keyAddr := iterator.Key()[len(keyPrefix):]
addr := sdk.AccAddress(keyAddr)

if handlerFn(addr) {
Expand All @@ -46,7 +48,9 @@ func (k Keeper) GetAllContractsBech32(ctx sdk.Context, keyPrefix []byte) []strin

list := make([]string, 0, len(contracts))
for _, c := range contracts {
list = append(list, c.String())

c := sdk.MustBech32ifyAddressBytes("juno", c.Bytes())
list = append(list, c)
}
return list
}
Expand All @@ -59,9 +63,10 @@ func (k Keeper) DeleteContract(ctx sdk.Context, keyPrefix []byte, contractAddr s
func (k Keeper) ExecuteMessageOnContracts(ctx sdk.Context, keyPrefix []byte, msgBz []byte) error {
p := k.GetParams(ctx)

for _, cAddr := range k.GetAllContracts(ctx, keyPrefix) {
for _, c := range k.GetAllContracts(ctx, keyPrefix) {
gasLimitCtx := ctx.WithGasMeter(sdk.NewGasMeter(p.ContractGasLimit))
if _, err := k.contractKeeper.Sudo(gasLimitCtx, cAddr.Bytes(), msgBz); err != nil {
if _, err := k.GetContractKeeper().Sudo(gasLimitCtx, sdk.AccAddress(c.Bytes()), msgBz); err != nil {
fmt.Println("ExecuteMessageOnContracts error: ", err)
return err
}
}
Expand Down
11 changes: 7 additions & 4 deletions x/cw-hooks/keeper/staking_hook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Validator struct {
Commission string `json:"commission"`
ValidatorTokens string `json:"validator_tokens"`
BondedTokens string `json:"bonded_tokens"`
BondStatus uint `json:"bond_status"`
BondStatus string `json:"bond_status"`
}

func NewValidator(val stakingtypes.ValidatorI) *Validator {
Expand All @@ -23,7 +23,7 @@ func NewValidator(val stakingtypes.ValidatorI) *Validator {
Commission: val.GetCommission().String(),
ValidatorTokens: val.GetTokens().String(),
BondedTokens: val.GetBondedTokens().String(),
BondStatus: uint(val.GetStatus()),
BondStatus: val.GetStatus().String(),
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ type SudoMsgAfterValidatorModified struct {
}

type SudoMsgAfterValidatorBeginUnbonding struct {
AfterValidatorBeginUnbonding *Validator `json:"after_validator_modified"`
AfterValidatorBeginUnbonding *Validator `json:"after_validator_begin_unbonding"`
}
type SudoMsgAfterValidatorBonded struct {
AfterValidatorBonded *Validator `json:"after_validator_bonded"`
Expand All @@ -78,7 +78,7 @@ type SudoMsgAfterValidatorRemoved struct {
AfterValidatorRemoved *Validator `json:"after_validator_removed"`
}
type SudoMsgBeforeValidatorSlashed struct {
BeforeValidatorSlashed *ValidatorSlashed `json:"before_validator_slash"`
BeforeValidatorSlashed *ValidatorSlashed `json:"before_validator_slashed"`
}
type SudoMsgBeforeDelegationSharesModified struct {
BeforeDelegationSharesModified *Delegation `json:"before_delegation_modified"`
Expand All @@ -92,3 +92,6 @@ type SudoMsgAfterDelegationModified struct {
type SudoMsgBeforeDelegationRemoved struct {
BeforeDelegationRemoved *Delegation `json:"before_delegation_removed"`
}
type SudoMsgAfterUnbondingInitiated struct {
AfterUnbondingInitiated uint64 `json:"after_unbonding_initiated"`
}
70 changes: 57 additions & 13 deletions x/cw-hooks/keeper/staking_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// TODO: return nil on unmarshal or err?

// skipUntilHeight allows us to skip gentxs.
const skipUntilHeight = 2

Expand All @@ -29,8 +31,11 @@ func (h StakingHooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddr
return nil
}

val := h.k.stakingKeeper.Validator(ctx, valAddr)
val := h.k.GetStakingKeeper().Validator(ctx, valAddr)
fmt.Println("VALIDATOR_CREATED: ", val)
if val == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgAfterValidatorCreated{
AfterValidatorCreated: NewValidator(val),
Expand All @@ -48,8 +53,11 @@ func (h StakingHooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress,
return nil
}

val := h.k.stakingKeeper.Validator(ctx, valAddr)
val := h.k.GetStakingKeeper().Validator(ctx, valAddr)
fmt.Println("AfterValidatorRemoved: ", val)
if val == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgAfterValidatorRemoved{
AfterValidatorRemoved: NewValidator(val),
Expand All @@ -67,8 +75,11 @@ func (h StakingHooks) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAd
return nil
}

del := h.k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
del := h.k.GetStakingKeeper().Delegation(ctx, delAddr, valAddr)
fmt.Println("BeforeDelegationCreated: ", del)
if del == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgBeforeDelegationCreated{
BeforeDelegationCreated: NewDelegation(del),
Expand All @@ -86,8 +97,11 @@ func (h StakingHooks) BeforeDelegationSharesModified(ctx sdk.Context, delAddr sd
return nil
}

del := h.k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
del := h.k.GetStakingKeeper().Delegation(ctx, delAddr, valAddr)
fmt.Println("BeforeDelegationSharesModified: ", del)
if del == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgBeforeDelegationSharesModified{
BeforeDelegationSharesModified: NewDelegation(del),
Expand All @@ -106,8 +120,11 @@ func (h StakingHooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAd
return nil
}

del := h.k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
del := h.k.GetStakingKeeper().Delegation(ctx, delAddr, valAddr)
fmt.Println("BeforeDelegationSharesModified: ", del)
if del == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgAfterDelegationModified{
AfterDelegationModified: NewDelegation(del),
Expand All @@ -124,8 +141,11 @@ func (h StakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAdd
if ctx.BlockHeight() <= skipUntilHeight {
return nil
}
val := h.k.stakingKeeper.Validator(ctx, valAddr)
val := h.k.GetStakingKeeper().Validator(ctx, valAddr)
fmt.Println("BeforeValidatorSlashed: ", val, fraction)
if val == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgBeforeValidatorSlashed{
BeforeValidatorSlashed: NewValidatorSlashed(val, fraction),
Expand All @@ -141,8 +161,11 @@ func (h StakingHooks) BeforeValidatorModified(ctx sdk.Context, valAddr sdk.ValAd
if ctx.BlockHeight() <= skipUntilHeight {
return nil
}
val := h.k.stakingKeeper.Validator(ctx, valAddr)
val := h.k.GetStakingKeeper().Validator(ctx, valAddr)
fmt.Println("BeforeValidatorModified: ", val)
if val == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgBeforeValidatorModified{
BeforeValidatorModified: NewValidator(val),
Expand All @@ -158,8 +181,11 @@ func (h StakingHooks) AfterValidatorBonded(ctx sdk.Context, _ sdk.ConsAddress, v
if ctx.BlockHeight() <= skipUntilHeight {
return nil
}
val := h.k.stakingKeeper.Validator(ctx, valAddr)
val := h.k.GetStakingKeeper().Validator(ctx, valAddr)
fmt.Println("AfterValidatorBonded: ", val)
if val == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgAfterValidatorBonded{
AfterValidatorBonded: NewValidator(val),
Expand All @@ -175,8 +201,11 @@ func (h StakingHooks) AfterValidatorBeginUnbonding(ctx sdk.Context, _ sdk.ConsAd
if ctx.BlockHeight() <= skipUntilHeight {
return nil
}
val := h.k.stakingKeeper.Validator(ctx, valAddr)
val := h.k.GetStakingKeeper().Validator(ctx, valAddr)
fmt.Println("AfterValidatorBeginUnbonding: ", val)
if val == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgAfterValidatorBeginUnbonding{
AfterValidatorBeginUnbonding: NewValidator(val),
Expand All @@ -192,8 +221,11 @@ func (h StakingHooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAd
if ctx.BlockHeight() <= skipUntilHeight {
return nil
}
del := h.k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
del := h.k.GetStakingKeeper().Delegation(ctx, delAddr, valAddr)
fmt.Println("BeforeDelegationRemoved: ", del)
if del == nil {
return nil
}

msgBz, err := json.Marshal(SudoMsgBeforeDelegationRemoved{
BeforeDelegationRemoved: NewDelegation(del),
Expand All @@ -205,7 +237,19 @@ func (h StakingHooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAd
return h.k.ExecuteMessageOnContracts(ctx, types.KeyPrefixStaking, msgBz)
}

func (h StakingHooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) error {
// idk what this is / does. Need to look into
return nil
func (h StakingHooks) AfterUnbondingInitiated(ctx sdk.Context, amt uint64) error {
if ctx.BlockHeight() <= skipUntilHeight {
return nil
}

fmt.Println("AfterUnbondingInitiated: ", amt)

msgBz, err := json.Marshal(SudoMsgAfterUnbondingInitiated{
AfterUnbondingInitiated: amt,
})
if err != nil {
return nil
}

return h.k.ExecuteMessageOnContracts(ctx, types.KeyPrefixStaking, msgBz)
}
26 changes: 26 additions & 0 deletions x/cw-hooks/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export JUNOD_NODE="tcp://localhost:26657"

# upload the smart contract, then create a validator. Confirm it works

FLAGS="--from=juno1 --gas=2500000 --fees=50000ujuno --node=http://localhost:26657 --yes --keyring-backend=test --home $HOME/.juno1 --chain-id=local-1 --output=json"

junod tx wasm store ./contract/juno_staking_hooks_example.wasm $FLAGS

sleep 5

txhash=$(junod tx wasm instantiate 1 '{}' --label=juno_staking --no-admin $FLAGS | jq -r .txhash)
sleep 5
addr=$(junod q tx $txhash --output=json --node=http://localhost:26657 | jq -r .logs[0].events[2].attributes[0].value) && echo $addr

# register addr to staking
junod tx cw-hooks register-staking $addr $FLAGS
junod q cw-hooks staking-contracts

# get config
junod q wasm contract-state smart $addr '{"get_config":{}}' --node=http://localhost:26657

# get last validator
junod q wasm contract-state smart $addr '{"get_last_val_info":{}}' --node=http://localhost:26657

# create validator
junod tx staking create-validator --amount 1ujuno --commission-rate="0.05" --commission-max-rate="1.0" --commission-max-change-rate="1.0" --moniker="test123" --from=juno2 --pubkey=$(junod tendermint show-validator --home $HOME/.juno) --min-self-delegation="1" --gas=1000000 --fees=50000ujuno --node=http://localhost:26657 --yes --keyring-backend=test --home $HOME/.juno1 --chain-id=local-1 --output=json

0 comments on commit 42d8296

Please sign in to comment.