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

feat: CreatePolicyByMsg #251

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
68 changes: 68 additions & 0 deletions bsctypes/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

permissiontype "github.com/bnb-chain/greenfield/x/permission/types"
"github.com/bnb-chain/greenfield/x/storage/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"

Expand All @@ -27,6 +28,8 @@ type IMessages interface {
UpdateGroupCallBack(sender *common.Address, synPkg *UpdateGroupMemberSynPackage, callbackGasLimit *big.Int, extraData *ExtraData, opt *RelayFeeOption) *Messages
CreatePolicy(sender *common.Address, policy *permissiontype.Policy) *Messages
CreatePolicyCallBack(sender *common.Address, policy *permissiontype.Policy, extraData *ExtraData, opt *RelayFeeOption) *Messages
CreatePolicyByMsg(sender *common.Address, policy *types.MsgPutPolicy) *Messages
CreatePolicyByMsgCallBack(sender *common.Address, policy *types.MsgPutPolicy, extraData *ExtraData, opt *RelayFeeOption) *Messages
DeletePolicy(sender *common.Address, id *big.Int) *Messages
DeletePolicyCallBack(sender *common.Address, id *big.Int, extraData *ExtraData, opt *RelayFeeOption) *Messages
TransferOut(sender *common.Address, recipient *common.Address, amount *big.Int) *Messages
Expand Down Expand Up @@ -440,6 +443,71 @@ func (m *Messages) CreatePolicyCallBack(sender *common.Address, policy *permissi
return m
}

func (m *Messages) CreatePolicyByMsg(sender *common.Address, policy *types.MsgPutPolicy) *Messages {
fee := new(big.Int)
fee.Add(m.RelayFee, m.MinAckRelayFee)

address := common.HexToAddress(m.Deployment.PermissionHub)
parsedABI, err := abi.JSON(strings.NewReader(bsccommon.PermissionABI))
if err != nil {
log.Fatalf("failed to parse contract ABI: %v", err)
}

data, err := policy.Marshal()
if err != nil {
log.Fatalf("failed to marshal policy: %v", err)
}

packedData, err := parsedABI.Pack("prepareCreatePolicy", sender, data)
if err != nil {
log.Fatalf("failed to pack data for prepareCreatePolicy: %v", err)
}

message := &Message{
Target: &address,
Data: packedData,
Value: fee,
}
m.Message = append(m.Message, message)
return m
}

func (m *Messages) CreatePolicyByMsgCallBack(sender *common.Address, policy *types.MsgPutPolicy, extraData *ExtraData, opt *RelayFeeOption) *Messages {
fee := new(big.Int)
ackFee := m.MinAckRelayFee
if opt != nil && opt.AckRelayFee != nil {
if opt.AckRelayFee.Cmp(m.MinAckRelayFee) < 0 {
log.Fatalf("opt.AckRelayFee can't be smaller than MinAckRelayFee")
}
ackFee = opt.AckRelayFee
}
fee.Add(m.RelayFee, ackFee)

address := common.HexToAddress(m.Deployment.PermissionHub)
parsedABI, err := abi.JSON(strings.NewReader(bsccommon.PermissionABI))
if err != nil {
log.Fatalf("failed to parse contract ABI: %v", err)
}

data, err := policy.Marshal()
if err != nil {
log.Fatalf("failed to marshal policy: %v", err)
}

packedData, err := parsedABI.Pack("prepareCreatePolicy0", sender, data, extraData)
if err != nil {
log.Fatalf("failed to pack data for prepareCreatePolicy0: %v", err)
}

message := &Message{
Target: &address,
Data: packedData,
Value: fee,
}
m.Message = append(m.Message, message)
return m
}

func (m *Messages) DeletePolicy(sender *common.Address, id *big.Int) *Messages {
fee := new(big.Int)
fee.Add(m.RelayFee, m.MinAckRelayFee)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
cosmossdk.io/errors v1.0.0-beta.7
cosmossdk.io/math v1.0.1
github.com/bnb-chain/greenfield v1.7.2
github.com/bnb-chain/greenfield v1.8.1-0.20240708064658-d58b2100e6fc
github.com/bnb-chain/greenfield-common/go v0.0.0-20240410092538-5e3891943cbb
github.com/cometbft/cometbft v0.38.6
github.com/consensys/gnark-crypto v0.7.0
Expand Down Expand Up @@ -154,10 +154,10 @@ replace (
cosmossdk.io/api => github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20231129013257-1e407f209b02
cosmossdk.io/math => github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20231129013257-1e407f209b02
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.23.0
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-cometbft v1.2.1-0.20240408033601-a6b682aa870e
github.com/cometbft/cometbft => github.com/bnb-chain/greenfield-cometbft v1.3.0
github.com/cometbft/cometbft-db => github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v1.6.0
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/greenfield-cosmos-sdk v1.8.0
github.com/cosmos/iavl => github.com/bnb-chain/greenfield-iavl v0.20.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/wercker/journalhook => github.com/wercker/journalhook v0.0.0-20230927020745-64542ffa4117
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=
github.com/bnb-chain/greenfield v1.7.2 h1:JWbw1oS1tzcyN6fJXEecJwCvQD3nO7gMrm71UC/P8cs=
github.com/bnb-chain/greenfield v1.7.2/go.mod h1:WamCBK6HPOqlVVuZoEN+jk923Ok2PZglPHeUmPa/d1w=
github.com/bnb-chain/greenfield-cometbft v1.2.1-0.20240408033601-a6b682aa870e h1:4ttDy8yBhBUW0gdFyBK0wHMJS5ZtlhBdoYx/O6T6Eqg=
github.com/bnb-chain/greenfield-cometbft v1.2.1-0.20240408033601-a6b682aa870e/go.mod h1:q9/nqW19iXvxyma5XgcZfxL/OkWI9s5e7yX9ecePz8A=
github.com/bnb-chain/greenfield v1.8.1-0.20240708064658-d58b2100e6fc h1:+ftgpdgNVfwFz3Epjs+7mD/2vO1YD6sbWD9qRHGklj4=
github.com/bnb-chain/greenfield v1.8.1-0.20240708064658-d58b2100e6fc/go.mod h1:R4itO5Q7d5wj0L9sAXpbrVZMUrdDyRtSjccW8XOEFvI=
github.com/bnb-chain/greenfield-cometbft v1.3.0 h1:v3nZ16ledTZGF5Csys7fTQGZcEV78ZLUtptA9PLKMo4=
github.com/bnb-chain/greenfield-cometbft v1.3.0/go.mod h1:0D+VPivZTeBldjtGGi9LKbBnKEO/RtMRJikie92LkYI=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1 h1:XcWulGacHVRiSCx90Q8Y//ajOrLNBQWR/KDB89dy3cU=
github.com/bnb-chain/greenfield-cometbft-db v0.8.1-alpha.1/go.mod h1:ey1CiK4bYo1RBNJLRiVbYr5CMdSxci9S/AZRINLtppI=
github.com/bnb-chain/greenfield-common/go v0.0.0-20240410092538-5e3891943cbb h1:D3LCFJRdM+5xRhIVvRDJeT/vLs/p96z/fiFJqTQl/n4=
github.com/bnb-chain/greenfield-common/go v0.0.0-20240410092538-5e3891943cbb/go.mod h1:DORUA5zwjRZjgpRtJw/qphdTCZHQjHYYfKZhlEUiDRQ=
github.com/bnb-chain/greenfield-cosmos-sdk v1.6.0 h1:La26hQZ+as0vrgbR5ppjz8rDFH4TgYLJd8ctG7z9zNQ=
github.com/bnb-chain/greenfield-cosmos-sdk v1.6.0/go.mod h1:XF8U3VN1euzLkIR5xiSNyQSnBabvnD86oz6fgdrpteQ=
github.com/bnb-chain/greenfield-cosmos-sdk v1.8.0 h1:XaHBYnlAJNIEVTr9dXp3jzw12gCoIEL5jHiAMp+PX0s=
github.com/bnb-chain/greenfield-cosmos-sdk v1.8.0/go.mod h1:2bwmwdXYBISnQoMwgAcZTVGt21lMsHZSeeeMByTvDlQ=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20231129013257-1e407f209b02 h1:cwuShQ+MlvwkfbOz79BRF4aYjgKAuSyugoCtXE8tWgM=
github.com/bnb-chain/greenfield-cosmos-sdk/api v0.0.0-20231129013257-1e407f209b02/go.mod h1:vhsZxXE9tYJeYB5JR4hPhd6Pc/uPf7j1T8IJ7p9FdeM=
github.com/bnb-chain/greenfield-cosmos-sdk/math v0.0.0-20231129013257-1e407f209b02 h1:RMrZep7e2dcMk4E5YysMdCn6PekI3vA2WHfnMQ/kg18=
Expand Down
Loading