/
lib.rs
261 lines (239 loc) · 6.84 KB
/
lib.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
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
use anchor_lang::prelude::*;
declare_id!("3fPCHGZzzBzcaBpq3Y7Nfj8W4AbA5wCVFyJghgcDMvmW");
#[program]
pub mod dap_protocol {
use super::*;
pub fn initialize_increment(
ctx: Context<InitializeIncrement>,
) -> Result<()> {
let increment = &mut ctx.accounts.increment;
increment.mint = ctx.accounts.mint.key();
increment.authority = ctx.accounts.payer.key();
Ok(())
}
pub fn publish_asset(
ctx: Context<PublishAsset>,
index: u8,
shadow: Pubkey,
) -> Result<()> {
let datum = &mut ctx.accounts.datum;
let increment_pda = &mut ctx.accounts.increment;
let tariff = &ctx.accounts.tariff;
let tariff_authority = &ctx.accounts.tariff_authority;
let payer = &ctx.accounts.payer;
// increment
let increment = increment_pda.increment + 1;
// assert that datum is produced in sequence
assert_eq!(increment, index);
increment_pda.increment = increment;
// mint
datum.mint = ctx.accounts.mint.key();
// authority
datum.authority = payer.key();
// shadow account
datum.shadow = shadow;
// filtered
datum.filtered = false;
// index
datum.index = index;
// tariff
Tariff::pay_tariff(
tariff,
tariff_authority,
payer
)
}
pub fn filter_asset(
ctx: Context<FilterAsset>,
_index: u8,
) -> Result<()> {
let datum = &mut ctx.accounts.datum;
// filter
datum.filtered = true;
Ok(())
}
pub fn initialize_tariff(
ctx: Context<InitializeTariff>,
) -> Result<()> {
let tariff = &mut ctx.accounts.tariff;
let payer = &mut ctx.accounts.payer;
// init
tariff.authority = payer.key();
tariff.tariff = 0;
Ok(())
}
pub fn transfer_tariff_authority(
ctx: Context<TransferTariffAuthority>,
) -> Result<()> {
let tariff = &mut ctx.accounts.tariff;
let to = &ctx.accounts.to;
// transfer authority
tariff.authority = to.key();
Ok(())
}
pub fn set_new_tariff(
ctx: Context<SetNewTariff>,
new_tariff: u64,
) -> Result<()> {
let tariff = &mut ctx.accounts.tariff;
// set new tariff
tariff.tariff = new_tariff;
Ok(())
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// DATUM ///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Accounts)]
pub struct InitializeIncrement<'info> {
#[account(init,
seeds = [mint.key().as_ref(), payer.key().as_ref()], bump,
payer = payer,
space = Increment::SPACE
)]
pub increment: Account<'info, Increment>,
#[account()]
/// CHECK: excluding check for spl-mint type
/// provides run-time benefits in avoiding additional
/// deserialization & binary dependencies
pub mint: UncheckedAccount<'info>,
#[account(mut)]
pub payer: Signer<'info>,
// system program
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(index: u8)]
pub struct PublishAsset<'info> {
#[account(init,
seeds = [mint.key().as_ref(), payer.key().as_ref(), & [index]], bump,
payer = payer,
space = Datum::SPACE
)]
pub datum: Account<'info, Datum>,
#[account(mut, seeds = [mint.key().as_ref(), payer.key().as_ref()], bump)]
pub increment: Account<'info, Increment>,
#[account()]
/// CHECK: excluding check for spl-mint type
/// provides run-time benefits in avoiding additional
/// deserialization & binary dependencies
pub mint: UncheckedAccount<'info>,
#[account(mut)]
pub payer: Signer<'info>,
#[account(seeds = [b"tarifftariff"], bump,
constraint = tariff.authority == tariff_authority.key()
)]
pub tariff: Account<'info, Tariff>,
#[account(mut)]
pub tariff_authority: SystemAccount<'info>,
// system program
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
#[instruction(index: u8)]
pub struct FilterAsset<'info> {
#[account(mut,
seeds = [mint.key().as_ref(), authority.key().as_ref(), & [index]],
bump
)]
pub datum: Account<'info, Datum>,
#[account()]
/// CHECK: excluding check for spl-mint type
/// provides run-time benefits in avoiding additional
/// deserialization & binary dependencies
pub mint: UncheckedAccount<'info>,
#[account()]
pub authority: Signer<'info>,
}
#[derive(Accounts)]
pub struct InitializeTariff<'info> {
#[account(init,
seeds = [b"tarifftariff"], bump,
payer = payer,
space = Tariff::SPACE
)]
pub tariff: Account<'info, Tariff>,
#[account(mut)]
pub payer: Signer<'info>,
// system program
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct TransferTariffAuthority<'info> {
#[account(mut,
seeds = [b"tarifftariff"], bump,
constraint = tariff.authority == from.key()
)]
pub tariff: Account<'info, Tariff>,
#[account()]
pub from: Signer<'info>,
#[account()]
pub to: SystemAccount<'info>,
}
#[derive(Accounts)]
pub struct SetNewTariff<'info> {
#[account(mut,
seeds = [b"tarifftariff"], bump,
constraint = tariff.authority == tariff_authority.key()
)]
pub tariff: Account<'info, Tariff>,
#[account()]
pub tariff_authority: Signer<'info>,
}
#[account]
pub struct Datum {
// target mint
pub mint: Pubkey,
// authority
pub authority: Pubkey,
// shadow account
pub shadow: Pubkey,
// filtered
pub filtered: bool,
// target index
pub index: u8,
}
impl Datum {
const SPACE: usize = 8 + 32 + 32 + 32 + 1 + 1;
}
#[account]
pub struct Increment {
// target mint
pub mint: Pubkey,
// authority
pub authority: Pubkey,
// increment total
pub increment: u8,
}
impl Increment {
const SPACE: usize = 8 + 32 + 32 + 1;
}
#[account]
pub struct Tariff {
pub authority: Pubkey,
pub tariff: u64,
}
impl Tariff {
const SPACE: usize = 8 + 32 + 8;
pub fn pay_tariff<'a>(
tariff: &Tariff, tariff_authority: &SystemAccount<'a>, payer: &Signer<'a>,
) -> Result<()> {
let ix = anchor_lang::solana_program::system_instruction::transfer(
&payer.key(),
&tariff_authority.key(),
tariff.tariff,
);
anchor_lang::solana_program::program::invoke(
&ix,
&[
payer.to_account_info(),
tariff_authority.to_account_info()
],
).map_err(Into::into)
}
}
#[error_code]
pub enum CustomErrors {
#[msg("decentralized assets should be immutable.")]
ImmutableAssets,
}