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
use crate::{
    error::ErrorCode,
    state::{GatingConfig, MarketState, SellingResourceState, MINIMUM_BALANCE_FOR_SYSTEM_ACCS},
    utils::*,
    CreateMarket,
};
use anchor_lang::{
    prelude::*,
    solana_program::{program::invoke, system_instruction},
};
use anchor_spl::token::accessor;

impl<'info> CreateMarket<'info> {
    pub fn process(
        &mut self,
        _treasury_owner_bump: u8,
        name: String,
        description: String,
        mutable: bool,
        price: u64,
        pieces_in_one_wallet: Option<u64>,
        start_date: u64,
        end_date: Option<u64>,
        gating_config: Option<GatingConfig>,
        remaining_accounts: &[AccountInfo<'info>],
    ) -> Result<()> {
        let market = &mut self.market;
        let store = &self.store;
        let selling_resource_owner = &self.selling_resource_owner;
        let selling_resource = &mut self.selling_resource;
        let mint = self.mint.to_account_info();
        let treasury_holder = self.treasury_holder.to_account_info();
        let owner = &self.owner;

        if name.len() > NAME_MAX_LEN {
            return Err(ErrorCode::NameIsTooLong.into());
        }

        if description.len() > DESCRIPTION_MAX_LEN {
            return Err(ErrorCode::DescriptionIsTooLong.into());
        }

        // Pieces in one wallet cannot be greater than Max Supply value
        if pieces_in_one_wallet.is_some()
            && selling_resource.max_supply.is_some()
            && pieces_in_one_wallet.unwrap() > selling_resource.max_supply.unwrap()
        {
            return Err(ErrorCode::PiecesInOneWalletIsTooMuch.into());
        }

        // Only new just created selling resource can be used to create market
        if selling_resource.state != SellingResourceState::Created {
            return Err(ErrorCode::SellingResourceAlreadyTaken.into());
        }

        // start_date cannot be in the past
        if start_date < Clock::get().unwrap().unix_timestamp as u64 {
            return Err(ErrorCode::StartDateIsInPast.into());
        }

        // end_date should not be greater than start_date
        if end_date.is_some() && start_date > end_date.unwrap() {
            return Err(ErrorCode::EndDateIsEarlierThanBeginDate.into());
        }

        if let Some(gating_data) = &gating_config {
            if let Some(gating_time) = gating_data.gating_time {
                if gating_time < start_date {
                    return Err(ErrorCode::WrongGatingDate.into());
                }
                if let Some(end_date) = end_date {
                    if gating_time > end_date {
                        return Err(ErrorCode::WrongGatingDate.into());
                    }
                }
            }

            if remaining_accounts.len() != 1 {
                return Err(ErrorCode::CollectionMintMissing.into());
            }

            let collection_mint = &remaining_accounts[0];

            if collection_mint.key != &gating_data.collection
                || collection_mint.owner != &spl_token::id()
            {
                return Err(ErrorCode::WrongCollectionMintKey.into());
            }
        }

        let is_native = mint.key() == System::id();

        if !is_native {
            if mint.owner != &anchor_spl::token::ID
                || treasury_holder.owner != &anchor_spl::token::ID
            {
                return Err(ProgramError::IllegalOwner.into());
            }

            if accessor::mint(&treasury_holder)? != *mint.key {
                return Err(ProgramError::InvalidAccountData.into());
            }

            if accessor::authority(&treasury_holder)? != owner.key() {
                return Err(ProgramError::InvalidAccountData.into());
            }
        } else {
            // for native SOL we use PDA as a treasury holder
            // because of security reasons(only program can spend this SOL)
            if treasury_holder.key != owner.key {
                return Err(ProgramError::InvalidAccountData.into());
            }

            // we need fund treasury holder account such as it will hold some metadata with SOL balance
            invoke(
                &system_instruction::transfer(
                    &selling_resource_owner.key(),
                    &treasury_holder.key(),
                    MINIMUM_BALANCE_FOR_SYSTEM_ACCS,
                ),
                &[
                    selling_resource_owner.to_account_info(),
                    treasury_holder.to_account_info(),
                ],
            )?;
        }

        // Check selling resource ownership
        assert_keys_equal(selling_resource.owner, selling_resource_owner.key())?;

        market.store = store.key();
        market.selling_resource = selling_resource.key();
        market.treasury_mint = mint.key();
        market.treasury_holder = treasury_holder.key();
        market.treasury_owner = owner.key();
        market.owner = selling_resource_owner.key();
        market.name = puffed_out_string(name, NAME_MAX_LEN);
        market.description = puffed_out_string(description, DESCRIPTION_MAX_LEN);
        market.mutable = mutable;
        market.price = price;
        market.pieces_in_one_wallet = pieces_in_one_wallet;
        market.start_date = start_date;
        market.end_date = end_date;
        market.state = MarketState::Created;
        market.gatekeeper = gating_config;
        selling_resource.state = SellingResourceState::InUse;

        Ok(())
    }
}