/
proposals.go
122 lines (91 loc) · 3.37 KB
/
proposals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package keeper
import (
"math/big"
"github.com/Canto-Network/Canto/v6/contracts"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/Canto-Network/Canto/v6/x/govshuttle/types"
erc20types "github.com/Canto-Network/Canto/v6/x/erc20/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
//method for appending govshuttle proposal types to the govshuttle Map contract
func (k *Keeper) AppendLendingMarketProposal(ctx sdk.Context, lm *types.LendingMarketProposal) (*types.LendingMarketProposal, error) {
m := lm.GetMetadata()
var err error
if m.GetPropId() == 0 {
m.PropId, err = k.govKeeper.GetProposalID(ctx)
}
if err != nil {
return nil, sdkerrors.Wrap(err, "Error obtaining Proposal ID")
}
//if this is the first govshuttle proposal, deploy the map contract as well
addr, found := k.GetPort(ctx)
// port has not been deployed yet, deploy and store
if !found {
addr, err = k.DeployMapContract(ctx, lm)
if err != nil {
return &types.LendingMarketProposal{}, err
}
// set the port address in state
k.SetPort(ctx, addr)
}
_, err = k.erc20Keeper.CallEVM(ctx, contracts.ProposalStoreContract.ABI, types.ModuleAddress, addr, true,
"AddProposal", sdk.NewIntFromUint64(m.GetPropId()).BigInt(), lm.GetTitle(), lm.GetDescription(), ToAddress(m.GetAccount()),
ToBigInt(m.GetValues()), m.GetSignatures(), ToBytes(m.GetCalldatas()))
if err != nil {
return nil, sdkerrors.Wrap(err, "Error in EVM Call")
}
return lm, nil
}
func (k Keeper) DeployMapContract(ctx sdk.Context, lm *types.LendingMarketProposal) (common.Address, error) {
m := lm.GetMetadata()
ctorArgs, err := contracts.ProposalStoreContract.ABI.Pack("", sdk.NewIntFromUint64(m.GetPropId()).BigInt(), lm.GetTitle(), lm.GetDescription(), ToAddress(m.GetAccount()),
ToBigInt(m.GetValues()), m.GetSignatures(), ToBytes(m.GetCalldatas())) //Call empty constructor of Proposal-Store
if err != nil {
return common.Address{}, sdkerrors.Wrapf(erc20types.ErrABIPack, "Contract deployment failure: %s", err.Error())
}
data := make([]byte, len(contracts.ProposalStoreContract.Bin)+len(ctorArgs))
copy(data[:len(contracts.ProposalStoreContract.Bin)], contracts.ProposalStoreContract.Bin)
copy(data[len(contracts.ProposalStoreContract.Bin):], ctorArgs)
nonce, err := k.accKeeper.GetSequence(ctx, types.ModuleAddress.Bytes())
if err != nil {
return common.Address{}, sdkerrors.Wrap(err, "Error obtaining account nonce")
}
contractAddr := crypto.CreateAddress(types.ModuleAddress, nonce)
_, err = k.erc20Keeper.CallEVMWithData(ctx, types.ModuleAddress, nil, data, true)
if err != nil {
return common.Address{}, sdkerrors.Wrap(err, "Failed to deploy contract")
}
return contractAddr, nil
}
func ToAddress(addrs []string) []common.Address {
if addrs == nil {
return make([]common.Address, 0)
}
arr := make([]common.Address, len(addrs))
for i, v := range addrs {
arr[i] = common.HexToAddress(v)
}
return arr
}
func ToBytes(strs []string) [][]byte {
if strs == nil {
return make([][]byte, 0)
}
arr := make([][]byte, len(strs))
for i, v := range strs {
arr[i] = common.Hex2Bytes(v)
}
return arr
}
func ToBigInt(ints []uint64) []*big.Int {
if ints == nil {
return make([]*big.Int, 0)
}
arr := make([]*big.Int, len(ints))
for i, a := range ints {
arr[i] = sdk.NewIntFromUint64(a).BigInt()
}
return arr
}