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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use crate::store::contract_state::{get_contract_state_v1, CONTRACT_TYPE};
use crate::types::error::ContractError;
use crate::util::conversion_utils::convert_denom;
use crate::util::provenance_utils::{
    check_account_has_all_attributes, check_account_has_enough_denom, get_marker_address_for_denom,
};
use crate::util::validation_utils::check_funds_are_empty;
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use provwasm_std::types::cosmos::base::v1beta1::Coin;
use provwasm_std::types::provenance::marker::v1::{MsgBurnRequest, MsgTransferRequest};
use result_extensions::ResultExtensions;

/// Invoked via the contract's execute functionality.  The function will attempt to pull [trade_amount](withdraw_trading#trade_amount)
/// of the trading marker's denom from the sender's account with a marker transfer, discern how much
/// of the deposit denom to which the submitted amount is equivalent, transfer that amount to the
/// sender, and then burn the exchanged trading marker denom.
///
/// # Parameters
/// * `deps` A dependencies object provided by the cosmwasm framework.  Allows access to useful
/// resources like contract internal storage and a querier to retrieve blockchain objects.
/// * `env` An environment object provided by the cosmwasm framework.  Describes the contract's
/// details, as well as blockchain information at the time of the transaction.
/// * `info` A message information object provided by the cosmwasm framework.  Describes the sender
/// of the instantiation message, as well as the funds provided as an amount during the transaction.
/// * `trade_amount` The amount of the trading marker to pull from the sender's account in exchange
/// for deposit denom.
pub fn withdraw_trading(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    trade_amount: u128,
) -> Result<Response, ContractError> {
    check_funds_are_empty(&info)?;
    let contract_state = get_contract_state_v1(deps.storage)?;
    check_account_has_all_attributes(
        &deps,
        &info.sender,
        &contract_state.required_withdraw_attributes,
    )?;
    let conversion = convert_denom(
        trade_amount,
        &contract_state.trading_marker,
        &contract_state.deposit_marker,
    )?;
    if conversion.target_amount == 0 {
        return ContractError::InvalidFundsError {
            message: format!(
                "sent [{}{}], but that is not enough to convert to at least one [{}]",
                trade_amount,
                &contract_state.trading_marker.name,
                &contract_state.deposit_marker.name,
            ),
        }
        .to_err();
    }
    let collected_amount = trade_amount - conversion.remainder;
    check_account_has_enough_denom(
        &deps.as_ref(),
        info.sender.as_str(),
        &contract_state.trading_marker.name,
        collected_amount,
    )?;
    // Collect the amount to be traded to the contract from the sender and give it directly to the
    // marker in order to stage it for burning
    let collect_funds_msg = MsgTransferRequest {
        administrator: env.contract.address.to_string(),
        amount: Some(Coin {
            denom: contract_state.trading_marker.name.to_owned(),
            amount: collected_amount.to_string(),
        }),
        from_address: info.sender.to_string(),
        to_address: get_marker_address_for_denom(
            &deps.as_ref(),
            &contract_state.trading_marker.name,
        )?,
    };
    // Release the total converted amount of funds back to the user
    let release_funds_msg = MsgTransferRequest {
        administrator: env.contract.address.to_string(),
        amount: Some(Coin {
            denom: contract_state.deposit_marker.name.to_owned(),
            amount: conversion.target_amount.to_string(),
        }),
        from_address: env.contract.address.to_string(),
        to_address: info.sender.to_string(),
    };
    // Burn all coins that were received except those that could not be converted, these will be
    // refunded
    let burn_msg = MsgBurnRequest {
        administrator: env.contract.address.to_string(),
        amount: Some(Coin {
            amount: collected_amount.to_string(),
            denom: contract_state.trading_marker.name.to_owned(),
        }),
    };
    Response::new()
        .add_message(collect_funds_msg)
        .add_message(release_funds_msg)
        .add_message(burn_msg)
        .add_attribute("action", "withdraw_trading")
        .add_attribute("contract_address", env.contract.address.to_string())
        .add_attribute("contract_type", CONTRACT_TYPE)
        .add_attribute("contract_name", &contract_state.contract_name)
        .add_attribute("withdraw_input_denom", &contract_state.trading_marker.name)
        .add_attribute("withdraw_input_amount", trade_amount.to_string())
        .add_attribute("withdraw_actual_amount", collected_amount.to_string())
        .add_attribute("received_denom", &contract_state.deposit_marker.name)
        .add_attribute("received_amount", conversion.target_amount.to_string())
        .to_ok()
}

#[cfg(test)]
mod tests {
    use crate::execute::withdraw_trading::withdraw_trading;
    use crate::store::contract_state::CONTRACT_TYPE;
    use crate::test::attribute_extractor::AttributeExtractor;
    use crate::test::test_constants::{
        DEFAULT_CONTRACT_NAME, DEFAULT_DEPOSIT_DENOM_NAME, DEFAULT_REQUIRED_WITHDRAW_ATTRIBUTE,
        DEFAULT_TRADING_DENOM_NAME,
    };
    use crate::test::test_instantiate::{test_instantiate, test_instantiate_with_msg};
    use crate::types::denom::Denom;
    use crate::types::error::ContractError;
    use crate::types::msg::InstantiateMsg;
    use cosmwasm_std::testing::{message_info, mock_env, MOCK_CONTRACT_ADDR};
    use cosmwasm_std::{coins, Addr, AnyMsg, CosmosMsg};
    use provwasm_mocks::{
        mock_provenance_dependencies, mock_provenance_dependencies_with_custom_querier,
        MockProvenanceQuerier,
    };
    use provwasm_std::shim::Any;
    use provwasm_std::types::cosmos::auth::v1beta1::BaseAccount;
    use provwasm_std::types::cosmos::bank::v1beta1::{QueryBalanceRequest, QueryBalanceResponse};
    use provwasm_std::types::cosmos::base::v1beta1::Coin;
    use provwasm_std::types::provenance::attribute::v1::{
        Attribute, AttributeType, QueryAttributesRequest, QueryAttributesResponse,
    };
    use provwasm_std::types::provenance::marker::v1::{
        MarkerAccount, MarkerStatus, MarkerType, MsgBurnRequest, MsgTransferRequest,
        QueryMarkerRequest, QueryMarkerResponse,
    };

    #[test]
    fn provided_funds_should_cause_an_error() {
        let mut deps = mock_provenance_dependencies();
        let error = withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &coins(10, "somecoin")),
            10,
        )
        .expect_err("an error should be emitted when coin is provided");
        assert!(
            matches!(error, ContractError::InvalidFundsError { .. }),
            "unexpected error type encountered when providing funds",
        );
    }

    #[test]
    fn missing_contract_state_should_cause_an_error() {
        let mut deps = mock_provenance_dependencies();
        let error = withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &[]),
            10,
        )
        .expect_err("an error should be emitted when no contract state exists");
        assert!(
            matches!(error, ContractError::StorageError { .. }),
            "unexpected error type encountered when no contract storage exists",
        );
    }

    #[test]
    fn sender_missing_required_amount_should_cause_an_error() {
        let mut querier = MockProvenanceQuerier::new(&[]);
        QueryBalanceRequest::mock_response(
            &mut querier,
            QueryBalanceResponse {
                balance: Some(Coin {
                    amount: "10".to_string(),
                    denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                }),
            },
        );
        QueryAttributesRequest::mock_response(
            &mut querier,
            QueryAttributesResponse {
                account: "sender".to_string(),
                attributes: vec![Attribute {
                    name: DEFAULT_REQUIRED_WITHDRAW_ATTRIBUTE.to_string(),
                    value: vec![],
                    attribute_type: AttributeType::Json as i32,
                    address: "addr".to_string(),
                    expiration_date: None,
                }],
                pagination: None,
            },
        );
        let mut deps = mock_provenance_dependencies_with_custom_querier(querier);
        test_instantiate(deps.as_mut());
        let error = withdraw_trading(deps.as_mut(), mock_env(), message_info(&Addr::unchecked("sender"), &[]), 10000)
            .expect_err("an error should occur when the sender tries to trade more funds than are available to them");
        assert!(
            matches!(error, ContractError::InvalidAccountError { .. }),
            "unexpected error type encountered when the sender tries to trade too much: {error:?}",
        );
    }

    #[test]
    fn sender_missing_required_attribute_should_cause_an_error() {
        let mut querier = MockProvenanceQuerier::new(&[]);
        QueryBalanceRequest::mock_response(
            &mut querier,
            QueryBalanceResponse {
                balance: Some(Coin {
                    amount: "10".to_string(),
                    denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                }),
            },
        );
        QueryAttributesRequest::mock_response(
            &mut querier,
            QueryAttributesResponse {
                account: "sender".to_string(),
                attributes: vec![],
                pagination: None,
            },
        );
        let mut deps = mock_provenance_dependencies_with_custom_querier(querier);
        test_instantiate(deps.as_mut());
        let error = withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &[]),
            10,
        )
        .expect_err("an error should occur when the sender does not have a required attribute");
        assert!(
            matches!(error, ContractError::InvalidAccountError { .. }),
            "unexpected error when account is missing required attribute",
        );
    }

    #[test]
    fn conversion_producing_no_output_denom_should_cause_an_error() {
        let mut querier = MockProvenanceQuerier::new(&[]);
        QueryBalanceRequest::mock_response(
            &mut querier,
            QueryBalanceResponse {
                balance: Some(Coin {
                    amount: "10".to_string(),
                    denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                }),
            },
        );
        QueryAttributesRequest::mock_response(
            &mut querier,
            QueryAttributesResponse {
                account: "sender".to_string(),
                attributes: vec![Attribute {
                    name: DEFAULT_REQUIRED_WITHDRAW_ATTRIBUTE.to_string(),
                    value: vec![],
                    attribute_type: AttributeType::Json as i32,
                    address: "addr".to_string(),
                    expiration_date: None,
                }],
                pagination: None,
            },
        );
        let mut deps = mock_provenance_dependencies_with_custom_querier(querier);
        // Setup trading marker to have a higher precision than deposit, which will cause a single
        // digit conversion to fail with the input value 7:
        // Input 7 == 0.07, but trading marker can only hold values with one decimal place.
        test_instantiate_with_msg(
            deps.as_mut(),
            InstantiateMsg {
                deposit_marker: Denom::new("denom1", 1),
                trading_marker: Denom::new("denom2", 2),
                ..InstantiateMsg::default()
            },
        );
        let error = withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &[]),
            7,
        )
        .expect_err("a conversion that does not produce any deposit denom should fail");
        let _expected_err =
            "sent [7denom2], but that is not enough to convert to at least one [denom1]"
                .to_string();
        assert!(
            matches!(
                error,
                ContractError::InvalidFundsError {
                    message: _expected_err,
                },
            ),
            "unexpected error when invalid conversion occurs",
        );
    }

    #[test]
    fn no_trading_marker_found_should_produce_an_error() {
        let mut querier = MockProvenanceQuerier::new(&[]);
        QueryBalanceRequest::mock_response(
            &mut querier,
            QueryBalanceResponse {
                balance: Some(Coin {
                    amount: "10".to_string(),
                    denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                }),
            },
        );
        QueryAttributesRequest::mock_response(
            &mut querier,
            QueryAttributesResponse {
                account: "sender".to_string(),
                attributes: vec![Attribute {
                    name: DEFAULT_REQUIRED_WITHDRAW_ATTRIBUTE.to_string(),
                    value: vec![],
                    attribute_type: AttributeType::Json as i32,
                    address: "addr".to_string(),
                    expiration_date: None,
                }],
                pagination: None,
            },
        );
        QueryMarkerRequest::mock_response(&mut querier, QueryMarkerResponse { marker: None });
        let mut deps = mock_provenance_dependencies_with_custom_querier(querier);
        test_instantiate_with_msg(
            deps.as_mut(),
            InstantiateMsg {
                deposit_marker: Denom::new("denom1", 2),
                trading_marker: Denom::new("denom2", 1),
                ..InstantiateMsg::default()
            },
        );
        let error = withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &[]),
            1,
        )
        .expect_err("a missing trading marker should cause a failure");
        let _expected_err = "unable to query marker by name [denom2]".to_string();
        assert!(
            matches!(
                error,
                ContractError::NotFoundError {
                    message: _expected_err,
                },
            ),
            "unexpected error when trading marker missing",
        );
    }

    #[test]
    fn successful_parameters_should_produce_a_result() {
        let mut querier = MockProvenanceQuerier::new(&[]);
        QueryBalanceRequest::mock_response(
            &mut querier,
            QueryBalanceResponse {
                balance: Some(Coin {
                    amount: "4321".to_string(),
                    denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                }),
            },
        );
        QueryAttributesRequest::mock_response(
            &mut querier,
            QueryAttributesResponse {
                account: "sender".to_string(),
                attributes: vec![Attribute {
                    name: DEFAULT_REQUIRED_WITHDRAW_ATTRIBUTE.to_string(),
                    value: vec![],
                    attribute_type: AttributeType::Json as i32,
                    address: "addr".to_string(),
                    expiration_date: None,
                }],
                pagination: None,
            },
        );
        QueryMarkerRequest::mock_response(
            &mut querier,
            QueryMarkerResponse {
                marker: Some(Any {
                    type_url: "/provenance.marker.v1.MarkerAccount".to_string(),
                    value: MarkerAccount {
                        base_account: Some(BaseAccount {
                            address: "trading-marker-addr".to_string(),
                            pub_key: None,
                            account_number: 32,
                            sequence: 37,
                        }),
                        manager: "some-manager".to_string(),
                        access_control: vec![],
                        status: MarkerStatus::Active as i32,
                        denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                        supply: "10".to_string(),
                        marker_type: MarkerType::Restricted as i32,
                        supply_fixed: false,
                        allow_governance_control: false,
                        allow_forced_transfer: false,
                        required_attributes: vec![],
                    }
                    .to_proto_bytes(),
                }),
            },
        );
        let mut deps = mock_provenance_dependencies_with_custom_querier(querier);
        // Setup the trading marker to have a higher precision than the deposit, requiring some
        // remainder to be returned. Ex:
        // Sender wants to send 4321 trading to get their deposit denom back, which equates to 4.321.
        // However, the deposit marker has a precision of 2, which will convert to 4.32.  The 1 will
        // be dropped and be a remaining value for the sender.
        test_instantiate_with_msg(
            deps.as_mut(),
            InstantiateMsg {
                deposit_marker: Denom::new(DEFAULT_DEPOSIT_DENOM_NAME, 2),
                trading_marker: Denom::new(DEFAULT_TRADING_DENOM_NAME, 3),
                ..InstantiateMsg::default()
            },
        );
        let response = withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &[]),
            4321,
        )
        .expect("proper circumstances should derive a successful result");
        assert_eq!(
            3,
            response.messages.len(),
            "expected the response to include three messages",
        );
        response.messages.iter().for_each(|msg| match &msg.msg {
            CosmosMsg::Any(AnyMsg { type_url, value }) => match type_url.as_str() {
                "/provenance.marker.v1.MsgTransferRequest" => {
                    let req = MsgTransferRequest::try_from(value.to_owned())
                        .expect("the transfer request msg should properly deserialize");
                    assert_eq!(
                        MOCK_CONTRACT_ADDR, req.administrator,
                        "the administrator should be the contract",
                    );
                    let amount = req
                        .amount
                        .expect("the transfer request should contain a coin amount");
                    match req.from_address.as_str() {
                        // Funds collection
                        "sender" => {
                            assert_eq!(
                                "4320", amount.amount,
                                "the fund collection should take all input funds except remainder",
                            );
                            assert_eq!(
                                DEFAULT_TRADING_DENOM_NAME, amount.denom,
                                "the fund collection should take the trading denom as input",
                            );
                            assert_eq!(
                                "trading-marker-addr", req.to_address,
                                "the fund collection should send funds back to the trading marker",
                            );
                        }
                        // Funds release
                        MOCK_CONTRACT_ADDR => {
                            assert_eq!(
                                "432", amount.amount,
                                "the fund release should return the properly converted deposit denom",
                            );
                            assert_eq!(
                                DEFAULT_DEPOSIT_DENOM_NAME, amount.denom,
                                "the fund release should return the deposit denom",
                            );
                            assert_eq!(
                                "sender", req.to_address,
                                "the fund release should send the funds back to the sender",
                            );
                        }
                        addr => panic!("transfer request included unexpected from_address: {addr}"),
                    }
                }
                "/provenance.marker.v1.MsgBurnRequest" => {
                    let req = MsgBurnRequest::try_from(value.to_owned())
                        .expect("the burn request msg should properly deserialize");
                    assert_eq!(
                        MOCK_CONTRACT_ADDR, req.administrator,
                        "the burn request should use the contract as the administrator",
                    );
                    let amount = req.amount.expect("the burn request should contain a coin amount");
                    assert_eq!(
                        "4320", amount.amount,
                        "the amount burned should be the amount of trading denom returned to the contract",
                    );
                    assert_eq!(
                        DEFAULT_TRADING_DENOM_NAME, amount.denom,
                        "the denom burned should be the trading denom",
                    );
                }
                url => panic!("unexpected type url in emitted msg: {url}"),
            },
            msg => panic!("unexpected message emitted: {msg:?}"),
        });
        assert_eq!(
            9,
            response.attributes.len(),
            "the response should emit nine attributes",
        );
        response.assert_attribute("action", "withdraw_trading");
        response.assert_attribute("contract_address", MOCK_CONTRACT_ADDR);
        response.assert_attribute("contract_type", CONTRACT_TYPE);
        response.assert_attribute("contract_name", DEFAULT_CONTRACT_NAME);
        response.assert_attribute("withdraw_input_denom", DEFAULT_TRADING_DENOM_NAME);
        response.assert_attribute("withdraw_input_amount", "4321");
        response.assert_attribute("withdraw_actual_amount", "4320");
        response.assert_attribute("received_denom", DEFAULT_DEPOSIT_DENOM_NAME);
        response.assert_attribute("received_amount", "432");
    }

    #[test]
    fn request_that_does_not_need_full_amount_expected_succeeds() {
        let mut querier = MockProvenanceQuerier::new(&[]);
        QueryBalanceRequest::mock_response(
            &mut querier,
            QueryBalanceResponse {
                balance: Some(Coin {
                    amount: "200".to_string(),
                    denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                }),
            },
        );
        QueryAttributesRequest::mock_response(
            &mut querier,
            QueryAttributesResponse {
                account: "sender".to_string(),
                attributes: vec![Attribute {
                    name: DEFAULT_REQUIRED_WITHDRAW_ATTRIBUTE.to_string(),
                    value: vec![],
                    attribute_type: AttributeType::Json as i32,
                    address: "addr".to_string(),
                    expiration_date: None,
                }],
                pagination: None,
            },
        );
        QueryMarkerRequest::mock_response(
            &mut querier,
            QueryMarkerResponse {
                marker: Some(Any {
                    type_url: "/provenance.marker.v1.MarkerAccount".to_string(),
                    value: MarkerAccount {
                        base_account: Some(BaseAccount {
                            address: "trading-marker-addr".to_string(),
                            pub_key: None,
                            account_number: 32,
                            sequence: 37,
                        }),
                        manager: "some-manager".to_string(),
                        access_control: vec![],
                        status: MarkerStatus::Active as i32,
                        denom: DEFAULT_TRADING_DENOM_NAME.to_string(),
                        supply: "10".to_string(),
                        marker_type: MarkerType::Restricted as i32,
                        supply_fixed: false,
                        allow_governance_control: false,
                        allow_forced_transfer: false,
                        required_attributes: vec![],
                    }
                    .to_proto_bytes(),
                }),
            },
        );
        let mut deps = mock_provenance_dependencies_with_custom_querier(querier);
        // Setup the trading marker to have a higher precision than the deposit, requiring some
        // remainder to be returned. Ex:
        // Sender wants to send 250, which equates to 2.50.  They don't actually have 250, but they
        // do have 200, which is allowed.  This should be allowed to proceed.
        test_instantiate_with_msg(
            deps.as_mut(),
            InstantiateMsg {
                deposit_marker: Denom::new(DEFAULT_DEPOSIT_DENOM_NAME, 1),
                trading_marker: Denom::new(DEFAULT_TRADING_DENOM_NAME, 3),
                ..InstantiateMsg::default()
            },
        );
        withdraw_trading(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked("sender"), &[]),
            250,
        )
        .expect("proper circumstances should derive a successful result");
    }
}