-
Notifications
You must be signed in to change notification settings - Fork 21
/
contract.rs
202 lines (180 loc) · 6.34 KB
/
contract.rs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::{
error::{ContractError, ContractResult},
execute::{
execute_post_swap_action, execute_swap_and_action, execute_swap_and_action_with_recover,
execute_user_swap, receive_cw20,
},
query::{query_ibc_transfer_adapter_contract, query_swap_venue_adapter_contract},
reply::{reply_swap_and_action_with_recover, RECOVER_REPLY_ID},
state::{BLOCKED_CONTRACT_ADDRESSES, IBC_TRANSFER_CONTRACT_ADDRESS, SWAP_VENUE_MAP},
};
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response,
StdResult,
};
use cw2::set_contract_version;
use skip::entry_point::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
///////////////
/// MIGRATE ///
///////////////
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> ContractResult<Response> {
unimplemented!()
}
///////////////////
/// INSTANTIATE ///
///////////////////
// Contract name and version used for migration.
const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> ContractResult<Response> {
// Set contract version
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
// Create response object to return
let mut response: Response = Response::new().add_attribute("action", "instantiate");
// Insert the entry point contract address into the blocked contract addresses map
BLOCKED_CONTRACT_ADDRESSES.save(deps.storage, &env.contract.address, &())?;
// Iterate through the swap venues provided and create a map of venue names to swap adapter contract addresses
for swap_venue in msg.swap_venues.iter() {
// Validate the swap contract address
let checked_swap_contract_address = deps
.api
.addr_validate(&swap_venue.adapter_contract_address)?;
// Prevent duplicate swap venues by erroring if the venue name is already stored
if SWAP_VENUE_MAP.has(deps.storage, &swap_venue.name) {
return Err(ContractError::DuplicateSwapVenueName);
}
// Store the swap venue name and contract address inside the swap venue map
SWAP_VENUE_MAP.save(
deps.storage,
&swap_venue.name,
&checked_swap_contract_address,
)?;
// Insert the swap contract address into the blocked contract addresses map
BLOCKED_CONTRACT_ADDRESSES.save(deps.storage, &checked_swap_contract_address, &())?;
// Add the swap venue and contract address to the response
response = response
.add_attribute("action", "add_swap_venue")
.add_attribute("name", &swap_venue.name)
.add_attribute("contract_address", &checked_swap_contract_address);
}
// Validate ibc transfer adapter contract addresses
let checked_ibc_transfer_contract_address =
deps.api.addr_validate(&msg.ibc_transfer_contract_address)?;
// Store the ibc transfer adapter contract address
IBC_TRANSFER_CONTRACT_ADDRESS.save(deps.storage, &checked_ibc_transfer_contract_address)?;
// Insert the ibc transfer adapter contract address into the blocked contract addresses map
BLOCKED_CONTRACT_ADDRESSES.save(deps.storage, &checked_ibc_transfer_contract_address, &())?;
// Add the ibc transfer adapter contract address to the response
response = response
.add_attribute("action", "add_ibc_transfer_adapter")
.add_attribute("contract_address", &checked_ibc_transfer_contract_address);
Ok(response)
}
///////////////
/// EXECUTE ///
///////////////
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> ContractResult<Response> {
match msg {
ExecuteMsg::Receive(msg) => receive_cw20(deps, env, info, msg),
ExecuteMsg::SwapAndActionWithRecover {
sent_asset,
user_swap,
min_asset,
timeout_timestamp,
post_swap_action,
affiliates,
recovery_addr,
} => execute_swap_and_action_with_recover(
deps,
env,
info,
sent_asset,
user_swap,
min_asset,
timeout_timestamp,
post_swap_action,
affiliates,
recovery_addr,
),
ExecuteMsg::SwapAndAction {
sent_asset,
user_swap,
min_asset,
timeout_timestamp,
post_swap_action,
affiliates,
} => execute_swap_and_action(
deps,
env,
info,
sent_asset,
user_swap,
min_asset,
timeout_timestamp,
post_swap_action,
affiliates,
),
ExecuteMsg::UserSwap {
swap,
min_asset,
remaining_asset,
affiliates,
} => execute_user_swap(
deps,
env,
info,
swap,
min_asset,
remaining_asset,
affiliates,
),
ExecuteMsg::PostSwapAction {
min_asset,
timeout_timestamp,
post_swap_action,
exact_out,
} => execute_post_swap_action(
deps,
env,
info,
min_asset,
timeout_timestamp,
post_swap_action,
exact_out,
),
}
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
RECOVER_REPLY_ID => reply_swap_and_action_with_recover(deps, msg),
_ => Err(ContractError::ReplyIdError(msg.id)),
}
}
/////////////
/// QUERY ///
/////////////
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::SwapVenueAdapterContract { name } => {
to_json_binary(&query_swap_venue_adapter_contract(deps, name)?)
}
QueryMsg::IbcTransferAdapterContract {} => {
to_json_binary(&query_ibc_transfer_adapter_contract(deps)?)
}
}
}