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
use {
super::Queue,
anchor_lang::{prelude::*, AnchorDeserialize},
std::convert::TryFrom,
};
const SEED_FEE: &[u8] = b"fee";
#[account]
#[derive(Debug)]
pub struct Fee {
pub balance: u64,
pub withholding: u64,
pub worker: Pubkey,
}
impl Fee {
pub fn pubkey(worker: Pubkey) -> Pubkey {
Pubkey::find_program_address(&[SEED_FEE, worker.as_ref()], &crate::ID).0
}
}
impl TryFrom<Vec<u8>> for Fee {
type Error = Error;
fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
Fee::try_deserialize(&mut data.as_slice())
}
}
pub trait FeeAccount {
fn pubkey(&self) -> Pubkey;
fn init(&mut self, worker: Pubkey) -> Result<()>;
fn claim_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()>;
fn claim_withholding(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()>;
fn escrow_balance(&mut self, amount: u64, queue: &mut Account<Queue>) -> Result<()>;
fn escrow_withholding(&mut self, amount: u64, queue: &mut Account<Queue>) -> Result<()>;
}
impl FeeAccount for Account<'_, Fee> {
fn pubkey(&self) -> Pubkey {
Fee::pubkey(self.worker)
}
fn init(&mut self, worker: Pubkey) -> Result<()> {
self.worker = worker;
self.balance = 0;
self.withholding = 0;
Ok(())
}
fn claim_balance(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()> {
self.balance = self.balance.checked_sub(amount).unwrap();
**self.to_account_info().try_borrow_mut_lamports()? = self
.to_account_info()
.lamports()
.checked_sub(amount)
.unwrap();
**pay_to.to_account_info().try_borrow_mut_lamports()? = pay_to
.to_account_info()
.lamports()
.checked_add(amount)
.unwrap();
Ok(())
}
fn claim_withholding(&mut self, amount: u64, pay_to: &mut SystemAccount) -> Result<()> {
self.withholding = self.withholding.checked_sub(amount).unwrap();
**self.to_account_info().try_borrow_mut_lamports()? = self
.to_account_info()
.lamports()
.checked_sub(amount)
.unwrap();
**pay_to.to_account_info().try_borrow_mut_lamports()? = pay_to
.to_account_info()
.lamports()
.checked_add(amount)
.unwrap();
Ok(())
}
fn escrow_withholding(&mut self, amount: u64, queue: &mut Account<Queue>) -> Result<()> {
self.withholding = self.withholding.checked_add(amount).unwrap();
**queue.to_account_info().try_borrow_mut_lamports()? = queue
.to_account_info()
.lamports()
.checked_sub(amount)
.unwrap();
**self.to_account_info().try_borrow_mut_lamports()? = self
.to_account_info()
.lamports()
.checked_add(amount)
.unwrap();
Ok(())
}
fn escrow_balance(&mut self, amount: u64, queue: &mut Account<Queue>) -> Result<()> {
self.balance = self.balance.checked_add(amount).unwrap();
**queue.to_account_info().try_borrow_mut_lamports()? = queue
.to_account_info()
.lamports()
.checked_sub(amount)
.unwrap();
**self.to_account_info().try_borrow_mut_lamports()? = self
.to_account_info()
.lamports()
.checked_add(amount)
.unwrap();
Ok(())
}
}