forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayable.fe
More file actions
143 lines (116 loc) · 3.95 KB
/
Copy pathpayable.fe
File metadata and controls
143 lines (116 loc) · 3.95 KB
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 std::evm::{Evm, Ctx, Address}
pub contract PayableBox uses (ctx: Ctx) {
#[payable]
init() {}
recv PayableMsg {
#[payable]
Fund {} {}
GetBalance {} -> u256
uses (ctx)
{
ctx.selfbalance()
}
}
}
pub contract NonPayableBox {
init() {}
recv NonPayableMsg {
Ping {} -> u256 {
1
}
}
}
pub contract NoInitBox {
recv NoInitMsg {
Ping {} -> u256 {
7
}
}
}
msg PayableMsg {
#[selector = sol("fund()")]
Fund,
#[selector = sol("getBalance()")]
GetBalance -> u256,
}
msg NonPayableMsg {
#[selector = sol("ping()")]
Ping -> u256,
}
msg NoInitMsg {
#[selector = sol("ping()")]
Ping -> u256,
}
// Test: non-payable recv arm works with zero value
#[test]
fn test_non_payable_zero_value() uses (evm: mut Evm) {
let addr = evm.create2<NonPayableBox>(value: 0, args: (), salt: 0)
assert(addr.inner != 0)
let result: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: NonPayableMsg::Ping {})
assert(result == 1)
}
// Test: non-payable recv arm reverts when ETH is sent
#[test(should_revert, balance = 1000000000)]
fn test_non_payable_rejects_value() uses (evm: mut Evm) {
let addr = evm.create2<NonPayableBox>(value: 0, args: (), salt: 0)
assert(addr.inner != 0)
// This should revert because Ping is not #[payable]
evm.call(addr: addr, gas: 100000, value: 1, message: NonPayableMsg::Ping {})
}
// Test: payable recv arm works with zero value
#[test]
fn test_payable_zero_value() uses (evm: mut Evm) {
let addr = evm.create2<PayableBox>(value: 0, args: (), salt: 0)
assert(addr.inner != 0)
// Fund is #[payable], should work with zero value too
evm.call(addr: addr, gas: 100000, value: 0, message: PayableMsg::Fund {})
}
// Test: payable init creates contract successfully
#[test]
fn test_payable_init() uses (evm: mut Evm) {
let addr = evm.create2<PayableBox>(value: 0, args: (), salt: 0)
assert(addr.inner != 0)
}
// Test: non-payable init reverts when ETH is sent during deployment
#[test(should_revert, balance = 1000000000)]
fn test_non_payable_init_rejects_value() uses (evm: mut Evm) {
// NonPayableBox init is not #[payable], deploying with value should revert
evm.create2<NonPayableBox>(value: 1, args: (), salt: 0)
}
// Test: contract without init deploys with zero value
#[test]
fn test_no_init_zero_value() uses (evm: mut Evm) {
let addr = evm.create2<NoInitBox>(value: 0, args: (), salt: 0)
assert(addr.inner != 0)
let result: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: NoInitMsg::Ping {})
assert(result == 7)
}
// Test: contract without init rejects ETH during deployment
#[test(should_revert, balance = 1000000000)]
fn test_no_init_rejects_value() uses (evm: mut Evm) {
evm.create2<NoInitBox>(value: 1, args: (), salt: 0)
}
// Test: payable recv arm accepts non-zero ETH
#[test(balance = 1000000000)]
fn test_payable_accepts_value() uses (evm: mut Evm) {
let addr = evm.create2<PayableBox>(value: 0, args: (), salt: 0)
assert(addr.inner != 0)
// Fund is #[payable], should succeed with non-zero value
evm.call(addr: addr, gas: 100000, value: 1, message: PayableMsg::Fund {})
}
// Test: payable init accepts ETH at deployment
#[test(balance = 1000000000)]
fn test_payable_init_accepts_value() uses (evm: mut Evm) {
let addr = evm.create2<PayableBox>(value: 1, args: (), salt: 0)
assert(addr.inner != 0)
}
// Test: selfbalance reflects ETH sent to contract
#[test(balance = 1000000000)]
fn test_selfbalance() uses (evm: mut Evm) {
let addr = evm.create2<PayableBox>(value: 0, args: (), salt: 0)
// Fund the contract with 500 wei
evm.call(addr: addr, gas: 100000, value: 500, message: PayableMsg::Fund {})
// Query the balance via ctx.selfbalance()
let balance: u256 = evm.call(addr: addr, gas: 100000, value: 0, message: PayableMsg::GetBalance {})
assert(balance == 500)
}