-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosmwasmpool.go
126 lines (99 loc) · 4.96 KB
/
cosmwasmpool.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
123
124
125
126
package apptesting
import (
"encoding/json"
"os"
"strings"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmoutils/cosmwasm"
"github.com/percosis-labs/percosis/x/cosmwasmpool/cosmwasm/msg"
"github.com/percosis-labs/percosis/x/cosmwasmpool/cosmwasm/msg/transmuter"
"github.com/percosis-labs/percosis/x/cosmwasmpool/model"
cosmwasmpooltypes "github.com/percosis-labs/percosis/x/cosmwasmpool/types"
)
const (
DefaultTransmuterDenomA = "axlusdc"
DefaultTransmuterDenomB = "gravusdc"
TransmuterContractName = "transmuter"
TransmuterMigrateContractName = "transmuter_migrate"
DefaultCodeId = 1
)
// PrepareCosmWasmPool sets up a cosmwasm pool with the default parameters.
func (s *KeeperTestHelper) PrepareCosmWasmPool() cosmwasmpooltypes.CosmWasmExtension {
return s.PrepareCustomTransmuterPool(s.TestAccs[0], []string{DefaultTransmuterDenomA, DefaultTransmuterDenomB})
}
// PrepareCustomConcentratedPool sets up a concentrated liquidity pool with the custom parameters.
func (s *KeeperTestHelper) PrepareCustomTransmuterPool(owner sdk.AccAddress, denoms []string) cosmwasmpooltypes.CosmWasmExtension {
// Mint some assets to the account.
s.FundAcc(s.TestAccs[0], DefaultAcctFunds)
// Upload contract code and get the code id.
codeId := s.StoreCosmWasmPoolContractCode(TransmuterContractName)
// Add code id to the whitelist.
s.App.CosmwasmPoolKeeper.WhitelistCodeId(s.Ctx, codeId)
// Generate instantiate message bytes.
instantiateMsgBz := s.GetTransmuterInstantiateMsgBytes(denoms)
// Generate msg create pool.
validCWPoolMsg := model.NewMsgCreateCosmWasmPool(codeId, owner, instantiateMsgBz)
// Create pool.
poolId, err := s.App.PoolManagerKeeper.CreatePool(s.Ctx, validCWPoolMsg)
s.Require().NoError(err)
// Get and return the pool.
pool, err := s.App.CosmwasmPoolKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)
return pool
}
// GetDefaultTransmuterInstantiateMsgBytes returns the default instantiate message for the transmuter contract
// with DefaultTransmuterDenomA and DefaultTransmuterDenomB as the pool asset denoms.
func (s *KeeperTestHelper) GetDefaultTransmuterInstantiateMsgBytes() []byte {
return s.GetTransmuterInstantiateMsgBytes([]string{DefaultTransmuterDenomA, DefaultTransmuterDenomB})
}
// GetTransmuterInstantiateMsgBytes returns the instantiate message for the transmuter contract with the
// given pool asset denoms.
func (s *KeeperTestHelper) GetTransmuterInstantiateMsgBytes(poolAssetDenoms []string) []byte {
instantiateMsg := msg.InstantiateMsg{
PoolAssetDenoms: poolAssetDenoms,
}
instantiateMsgBz, err := json.Marshal(instantiateMsg)
s.Require().NoError(err)
return instantiateMsgBz
}
// StoreCosmWasmPoolContractCode stores the cosmwasm pool contract code in the wasm keeper and returns the code id.
// contractName is the name of the contract file in the x/cosmwasmpool/bytecode directory without the .wasm extension.
func (s *KeeperTestHelper) StoreCosmWasmPoolContractCode(contractName string) uint64 {
cosmwasmpoolModuleAddr := s.App.AccountKeeper.GetModuleAddress(cosmwasmpooltypes.ModuleName)
s.Require().NotNil(cosmwasmpoolModuleAddr)
// Allow the cosmwasm pool module to upload code.
params := s.App.WasmKeeper.GetParams(s.Ctx)
s.App.WasmKeeper.SetParams(s.Ctx, wasmtypes.Params{
CodeUploadAccess: wasmtypes.AccessConfig{
Permission: wasmtypes.AccessTypeAnyOfAddresses,
Addresses: []string{cosmwasmpoolModuleAddr.String()},
},
InstantiateDefaultPermission: params.InstantiateDefaultPermission,
})
code := s.GetContractCode(contractName)
instantiateConfig := wasmtypes.AccessConfig{Permission: wasmtypes.AccessTypeOnlyAddress, Address: cosmwasmpoolModuleAddr.String()}
codeID, _, err := s.App.ContractKeeper.Create(s.Ctx, cosmwasmpoolModuleAddr, code, &instantiateConfig)
s.Require().NoError(err)
return codeID
}
// GetContractCode returns the contract code for the given contract name.
// Assumes that the contract code is stored under x/cosmwasmpool/bytecode.
func (s *KeeperTestHelper) GetContractCode(contractName string) []byte {
workingDir, err := os.Getwd()
s.Require().NoError(err)
projectRootPath := "/percosis/"
projectRootIndex := strings.LastIndex(workingDir, projectRootPath) + len(projectRootPath)
workingDir = workingDir[:projectRootIndex]
code, err := os.ReadFile(workingDir + "x/cosmwasmpool/bytecode/" + contractName + ".wasm")
s.Require().NoError(err)
return code
}
// JoinTransmuterPool joins the given pool with the given coins from the given address.
func (s *KeeperTestHelper) JoinTransmuterPool(lpAddress sdk.AccAddress, poolId uint64, coins sdk.Coins) {
pool, err := s.App.CosmwasmPoolKeeper.GetPoolById(s.Ctx, poolId)
s.Require().NoError(err)
// add liquidity by joining the pool
request := transmuter.JoinPoolExecuteMsgRequest{}
cosmwasm.MustExecute[transmuter.JoinPoolExecuteMsgRequest, msg.EmptyStruct](s.Ctx, s.App.ContractKeeper, pool.GetContractAddress(), lpAddress, coins, request)
}