-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathlib.rs
264 lines (223 loc) · 8.87 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
262
263
264
//! A smart contract which demonstrates behavior of the `self.env().transfer()` function.
//! It transfers some of it's balance to the caller.
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#![allow(clippy::new_without_default)]
#[ink::contract]
pub mod give_me {
/// No storage is needed for this simple contract.
#[ink(storage)]
pub struct GiveMe {}
impl GiveMe {
/// Creates a new instance of this contract.
#[ink(constructor, payable)]
pub fn new() -> Self {
Self {}
}
/// Transfers `value` amount of tokens to the caller.
///
/// # Errors
///
/// - Panics in case the requested transfer exceeds the contract balance.
/// - Panics in case the requested transfer would have brought this contract's
/// balance below the minimum balance (i.e. the chain's existential deposit).
/// - Panics in case the transfer failed for another reason.
#[ink(message)]
pub fn give_me(&mut self, value: Balance) {
ink::env::debug_println!("requested value: {}", value);
ink::env::debug_println!("contract balance: {}", self.env().balance());
assert!(value <= self.env().balance(), "insufficient funds!");
if self.env().transfer(self.env().caller(), value).is_err() {
panic!(
"requested transfer failed. this can be the case if the contract does not\
have sufficient free funds or if the transfer would have brought the\
contract's balance below minimum balance."
)
}
}
/// Asserts that the token amount sent as payment with this call
/// is exactly `10`. This method will fail otherwise, and the
/// transaction would then be reverted.
///
/// # Note
///
/// The method needs to be annotated with `payable`; only then it is
/// allowed to receive value as part of the call.
#[ink(message, payable, selector = 0xCAFEBABE)]
pub fn was_it_ten(&self) {
ink::env::debug_println!(
"received payment: {}",
self.env().transferred_value()
);
assert!(self.env().transferred_value() == 10, "payment was not ten");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[ink::test]
fn transfer_works() {
// given
let contract_balance = 100;
let accounts = default_accounts();
let mut give_me = create_contract(contract_balance);
// when
set_sender(accounts.eve);
set_balance(accounts.eve, 0);
give_me.give_me(80);
// then
assert_eq!(get_balance(accounts.eve), 80);
}
#[ink::test]
#[should_panic(expected = "insufficient funds!")]
fn transfer_fails_insufficient_funds() {
// given
let contract_balance = 100;
let accounts = default_accounts();
let mut give_me = create_contract(contract_balance);
// when
set_sender(accounts.eve);
give_me.give_me(120);
// then
// `give_me` must already have panicked here
}
#[ink::test]
fn test_transferred_value() {
use ink::codegen::Env;
// given
let accounts = default_accounts();
let give_me = create_contract(100);
let contract_account = give_me.env().account_id();
// when
// Push the new execution context which sets initial balances and
// sets Eve as the caller
set_balance(accounts.eve, 100);
set_balance(contract_account, 0);
set_sender(accounts.eve);
// then
// we use helper macro to emulate method invocation coming with payment,
// and there must be no panic
ink::env::pay_with_call!(give_me.was_it_ten(), 10);
// and
// balances should be changed properly
let contract_new_balance = get_balance(contract_account);
let caller_new_balance = get_balance(accounts.eve);
assert_eq!(caller_new_balance, 100 - 10);
assert_eq!(contract_new_balance, 10);
}
#[ink::test]
#[should_panic(expected = "payment was not ten")]
fn test_transferred_value_must_fail() {
// given
let accounts = default_accounts();
let give_me = create_contract(100);
// when
// Push the new execution context which sets Eve as caller and
// the `mock_transferred_value` as the value which the contract
// will see as transferred to it.
set_sender(accounts.eve);
ink::env::test::set_value_transferred::<ink::env::DefaultEnvironment>(13);
// then
give_me.was_it_ten();
}
/// Creates a new instance of `GiveMe` with `initial_balance`.
///
/// Returns the `contract_instance`.
fn create_contract(initial_balance: Balance) -> GiveMe {
let accounts = default_accounts();
set_sender(accounts.alice);
set_balance(contract_id(), initial_balance);
GiveMe::new()
}
fn contract_id() -> AccountId {
ink::env::test::callee::<ink::env::DefaultEnvironment>()
}
fn set_sender(sender: AccountId) {
ink::env::test::set_caller::<ink::env::DefaultEnvironment>(sender);
}
fn default_accounts(
) -> ink::env::test::DefaultAccounts<ink::env::DefaultEnvironment> {
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>()
}
fn set_balance(account_id: AccountId, balance: Balance) {
ink::env::test::set_account_balance::<ink::env::DefaultEnvironment>(
account_id, balance,
)
}
fn get_balance(account_id: AccountId) -> Balance {
ink::env::test::get_account_balance::<ink::env::DefaultEnvironment>(
account_id,
)
.expect("Cannot get account balance")
}
}
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::{
ChainBackend,
ContractsBackend,
};
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[ink_e2e::test]
async fn e2e_sending_value_to_give_me_must_fail<Client: E2EBackend>(
mut client: Client,
) -> E2EResult<()> {
// given
let mut constructor = GiveMeRef::new();
let contract = client
.instantiate("contract_transfer", &ink_e2e::alice(), &mut constructor)
.value(1000)
.submit()
.await
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<GiveMe>();
// when
let transfer = call_builder.give_me(120);
let call_res = client
.call(&ink_e2e::bob(), &transfer)
.value(10)
.submit()
.await;
// then
if let Err(ink_e2e::Error::CallDryRun(dry_run)) = call_res {
assert!(dry_run.debug_message.contains("paid an unpayable message"))
} else {
panic!("Paying an unpayable message should fail")
}
Ok(())
}
#[ink_e2e::test]
async fn e2e_contract_must_transfer_value_to_sender<Client: E2EBackend>(
mut client: Client,
) -> E2EResult<()> {
// given
let mut constructor = GiveMeRef::new();
let contract = client
.instantiate("contract_transfer", &ink_e2e::bob(), &mut constructor)
.value(1337)
.submit()
.await
.expect("instantiate failed");
let mut call_builder = contract.call_builder::<GiveMe>();
let balance_before: Balance = client
.free_balance(contract.account_id.clone())
.await
.expect("getting balance failed");
// when
let transfer = call_builder.give_me(120);
let call_res = client
.call(&ink_e2e::eve(), &transfer)
.submit()
.await
.expect("call failed");
// then
assert!(call_res.debug_message().contains("requested value: 120\n"));
let balance_after: Balance = client
.free_balance(contract.account_id.clone())
.await
.expect("getting balance failed");
assert_eq!(balance_before - balance_after, 120);
Ok(())
}
}
}