Skip to content

Commit 6488ab5

Browse files
committed
Added unwrap lamport rust-legacy tests
1 parent 721a16a commit 6488ab5

1 file changed

Lines changed: 338 additions & 0 deletions

File tree

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
mod program_test;
2+
use {
3+
program_test::{TestContext, TokenContext},
4+
solana_program_test::tokio,
5+
solana_sdk::{
6+
instruction::InstructionError, signature::Signer, signer::keypair::Keypair,
7+
transaction::TransactionError, transport::TransportError,
8+
},
9+
spl_token_2022_interface::error::TokenError,
10+
spl_token_client::token::TokenError as TokenClientError,
11+
};
12+
13+
#[derive(PartialEq)]
14+
enum TestMode {
15+
Regular,
16+
WithImmutableOwner,
17+
}
18+
19+
async fn run_basic_unwrap_lamports(context: TestContext, test_mode: TestMode) {
20+
let TokenContext {
21+
token, alice, bob, ..
22+
} = context.token_context.unwrap();
23+
24+
let amount = 1000000000;
25+
26+
let alice_account = Keypair::new();
27+
match test_mode {
28+
TestMode::WithImmutableOwner => {
29+
token
30+
.wrap(
31+
&alice_account.pubkey(),
32+
&alice.pubkey(),
33+
amount,
34+
&[&alice_account],
35+
)
36+
.await
37+
.unwrap();
38+
}
39+
TestMode::Regular => {
40+
token
41+
.wrap_with_mutable_ownership(
42+
&alice_account.pubkey(),
43+
&alice.pubkey(),
44+
amount,
45+
&[&alice_account],
46+
)
47+
.await
48+
.unwrap();
49+
}
50+
}
51+
let alice_account = alice_account.pubkey();
52+
let bob_account = Keypair::new();
53+
match test_mode {
54+
TestMode::WithImmutableOwner => {
55+
token
56+
.wrap(
57+
&bob_account.pubkey(),
58+
&bob.pubkey(),
59+
amount,
60+
&[&bob_account],
61+
)
62+
.await
63+
.unwrap();
64+
}
65+
TestMode::Regular => {
66+
token
67+
.wrap_with_mutable_ownership(
68+
&bob_account.pubkey(),
69+
&bob.pubkey(),
70+
amount,
71+
&[&bob_account],
72+
)
73+
.await
74+
.unwrap();
75+
}
76+
}
77+
let bob_account = bob_account.pubkey();
78+
79+
// unwrap Some(1) lamports is ok
80+
token
81+
.unwrap_lamports(
82+
&alice_account,
83+
&bob_account,
84+
&alice.pubkey(),
85+
Some(1),
86+
&[&alice],
87+
)
88+
.await
89+
.unwrap();
90+
91+
// unwrap too much lamports is not ok
92+
let error = token
93+
.unwrap_lamports(
94+
&alice_account,
95+
&bob_account,
96+
&alice.pubkey(),
97+
Some(amount),
98+
&[&alice],
99+
)
100+
.await
101+
.unwrap_err();
102+
assert_eq!(
103+
error,
104+
TokenClientError::Client(Box::new(TransportError::TransactionError(
105+
TransactionError::InstructionError(
106+
0,
107+
InstructionError::Custom(TokenError::InsufficientFunds as u32)
108+
)
109+
)))
110+
);
111+
112+
// wrong signer
113+
let error = token
114+
.unwrap_lamports(
115+
&alice_account,
116+
&bob_account,
117+
&bob.pubkey(),
118+
Some(1),
119+
&[&bob],
120+
)
121+
.await
122+
.unwrap_err();
123+
assert_eq!(
124+
error,
125+
TokenClientError::Client(Box::new(TransportError::TransactionError(
126+
TransactionError::InstructionError(
127+
0,
128+
InstructionError::Custom(TokenError::OwnerMismatch as u32)
129+
)
130+
)))
131+
);
132+
133+
// unwrap None lamports is ok
134+
token
135+
.unwrap_lamports(
136+
&alice_account,
137+
&bob_account,
138+
&alice.pubkey(),
139+
None,
140+
&[&alice],
141+
)
142+
.await
143+
.unwrap();
144+
}
145+
146+
#[tokio::test]
147+
async fn basic() {
148+
let mut context = TestContext::new().await;
149+
context.init_token_with_native_mint().await.unwrap();
150+
run_basic_unwrap_lamports(context, TestMode::Regular).await;
151+
}
152+
153+
#[tokio::test]
154+
async fn basic_with_extensions() {
155+
let mut context = TestContext::new().await;
156+
context.init_token_with_native_mint().await.unwrap();
157+
run_basic_unwrap_lamports(context, TestMode::WithImmutableOwner).await;
158+
}
159+
160+
async fn run_self_unwrap_lamports(context: TestContext, test_mode: TestMode) {
161+
let TokenContext { token, alice, .. } = context.token_context.unwrap();
162+
163+
let amount = 1000000000;
164+
165+
let alice_account = Keypair::new();
166+
match test_mode {
167+
TestMode::WithImmutableOwner => {
168+
token
169+
.wrap(
170+
&alice_account.pubkey(),
171+
&alice.pubkey(),
172+
amount,
173+
&[&alice_account],
174+
)
175+
.await
176+
.unwrap();
177+
}
178+
TestMode::Regular => {
179+
token
180+
.wrap_with_mutable_ownership(
181+
&alice_account.pubkey(),
182+
&alice.pubkey(),
183+
amount,
184+
&[&alice_account],
185+
)
186+
.await
187+
.unwrap();
188+
}
189+
}
190+
let alice_account = alice_account.pubkey();
191+
192+
// unwrap Some(1) lamports is ok
193+
token
194+
.unwrap_lamports(
195+
&alice_account,
196+
&alice_account,
197+
&alice.pubkey(),
198+
Some(1),
199+
&[&alice],
200+
)
201+
.await
202+
.unwrap();
203+
204+
// unwrap too much lamports is not ok
205+
let error = token
206+
.unwrap_lamports(
207+
&alice_account,
208+
&alice_account,
209+
&alice.pubkey(),
210+
Some(amount),
211+
&[&alice],
212+
)
213+
.await
214+
.unwrap_err();
215+
assert_eq!(
216+
error,
217+
TokenClientError::Client(Box::new(TransportError::TransactionError(
218+
TransactionError::InstructionError(
219+
0,
220+
InstructionError::Custom(TokenError::InsufficientFunds as u32)
221+
)
222+
)))
223+
);
224+
225+
// unwrap None lamports is ok
226+
token
227+
.unwrap_lamports(
228+
&alice_account,
229+
&alice_account,
230+
&alice.pubkey(),
231+
None,
232+
&[&alice],
233+
)
234+
.await
235+
.unwrap();
236+
}
237+
238+
#[tokio::test]
239+
async fn self_unwrap_lamports() {
240+
let mut context = TestContext::new().await;
241+
context.init_token_with_native_mint().await.unwrap();
242+
run_self_unwrap_lamports(context, TestMode::Regular).await;
243+
}
244+
245+
#[tokio::test]
246+
async fn self_unwrap_lamports_with_extension() {
247+
let mut context = TestContext::new().await;
248+
context.init_token_with_native_mint().await.unwrap();
249+
run_basic_unwrap_lamports(context, TestMode::WithImmutableOwner).await;
250+
}
251+
252+
async fn run_self_owned_unwrap_lamports(context: TestContext, test_mode: TestMode) {
253+
let TokenContext {
254+
token, alice, bob, ..
255+
} = context.token_context.unwrap();
256+
257+
let amount = 1000000000;
258+
259+
match test_mode {
260+
TestMode::WithImmutableOwner => {
261+
token
262+
.wrap(&alice.pubkey(), &alice.pubkey(), amount, &[&alice])
263+
.await
264+
.unwrap();
265+
}
266+
TestMode::Regular => {
267+
token
268+
.wrap_with_mutable_ownership(&alice.pubkey(), &alice.pubkey(), amount, &[&alice])
269+
.await
270+
.unwrap();
271+
}
272+
}
273+
let alice_account = alice.pubkey();
274+
let bob_account = Keypair::new();
275+
match test_mode {
276+
TestMode::WithImmutableOwner => {
277+
token
278+
.wrap(
279+
&bob_account.pubkey(),
280+
&bob.pubkey(),
281+
amount,
282+
&[&bob_account],
283+
)
284+
.await
285+
.unwrap();
286+
}
287+
TestMode::Regular => {
288+
token
289+
.wrap_with_mutable_ownership(
290+
&bob_account.pubkey(),
291+
&bob.pubkey(),
292+
amount,
293+
&[&bob_account],
294+
)
295+
.await
296+
.unwrap();
297+
}
298+
}
299+
let bob_account = bob_account.pubkey();
300+
301+
// unwrap Some(1) lamports is ok
302+
token
303+
.unwrap_lamports(
304+
&alice_account,
305+
&bob_account,
306+
&alice.pubkey(),
307+
Some(1),
308+
&[&alice],
309+
)
310+
.await
311+
.unwrap();
312+
313+
// self unwrap None lamports is ok
314+
token
315+
.unwrap_lamports(
316+
&alice_account,
317+
&alice_account,
318+
&alice.pubkey(),
319+
None,
320+
&[&alice],
321+
)
322+
.await
323+
.unwrap();
324+
}
325+
326+
#[tokio::test]
327+
async fn self_owned() {
328+
let mut context = TestContext::new().await;
329+
context.init_token_with_native_mint().await.unwrap();
330+
run_self_owned_unwrap_lamports(context, TestMode::Regular).await;
331+
}
332+
333+
#[tokio::test]
334+
async fn self_owned_with_extensions() {
335+
let mut context = TestContext::new().await;
336+
context.init_token_with_native_mint().await.unwrap();
337+
run_self_owned_unwrap_lamports(context, TestMode::WithImmutableOwner).await;
338+
}

0 commit comments

Comments
 (0)