Skip to content

Commit f8d8802

Browse files
committed
[isoltest] Add support to query balance.
1 parent 2969bc0 commit f8d8802

6 files changed

+67
-5
lines changed

test/libsolidity/SemanticTest.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <algorithm>
2828
#include <cctype>
2929
#include <fstream>
30+
#include <functional>
3031
#include <memory>
3132
#include <optional>
3233
#include <stdexcept>
@@ -131,6 +132,19 @@ void SemanticTest::initializeBuiltins()
131132
{
132133
return util::toBigEndian(u256(0x1234));
133134
};
135+
soltestAssert(m_builtins.count("accountBalance") == 0, "");
136+
m_builtins["accountBalance"] = [this](FunctionCall const& _call) -> std::optional<bytes>
137+
{
138+
soltestAssert(_call.arguments.parameters.size() == 1, "Account address expected.");
139+
h160 address = h160(_call.arguments.parameters.at(0).rawString);
140+
return util::toBigEndian(SolidityExecutionFramework::balanceAt(address));
141+
};
142+
soltestAssert(m_builtins.count("contractBalance") == 0, "");
143+
m_builtins["contractBalance"] = [this](FunctionCall const& _call) -> std::optional<bytes>
144+
{
145+
soltestAssert(_call.arguments.parameters.empty(), "No arguments expected.");
146+
return util::toBigEndian(SolidityExecutionFramework::balanceAt(m_contractAddress));
147+
};
134148
}
135149

136150
TestCase::TestResult SemanticTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
contract Other {
2+
constructor() payable {
3+
}
4+
function getAddress() public returns (address) {
5+
return address(this);
6+
}
7+
}
8+
contract ClientReceipt {
9+
Other other;
10+
constructor() payable {
11+
other = new Other{value:500}();
12+
}
13+
function getAddress() public returns (address) {
14+
return other.getAddress();
15+
}
16+
}
17+
// ====
18+
// compileViaYul: also
19+
// ----
20+
// constructor(), 2000 wei ->
21+
// gas irOptimized: 191881
22+
// gas legacy: 235167
23+
// gas legacyOptimized: 180756
24+
// contractBalance -> 1500
25+
// gas irOptimized: 191881
26+
// gas legacy: 235167
27+
// gas legacyOptimized: 180756
28+
// getAddress() -> 0xf01f7809444bd9a93a854361c6fae3f23d9e23db
29+
// accountBalance: 0xf01f7809444bd9a93a854361c6fae3f23d9e23db -> 500
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
contract ClientReceipt {
2+
constructor() payable {}
3+
}
4+
// ====
5+
// compileViaYul: also
6+
// ----
7+
// constructor(), 1000 wei ->
8+
// contractBalance -> 1000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
contract ClientReceipt {
2+
constructor() payable {}
3+
}
4+
// ====
5+
// compileViaYul: also
6+
// ----
7+
// constructor(), 1 ether ->
8+
// contractBalance -> 1000000000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
contract ClientReceipt {
2+
}
3+
// ====
4+
// compileViaYul: also
5+
// ----
6+
// contractBalance -> 0
7+
// accountBalance: 0x0000000000000000000000000000000000000000 -> 0

test/libsolidity/semanticTests/payable/no_nonpayable_circumvention_by_modifier.sol

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ contract C {
88
function msgvalue() internal returns (uint) {
99
return msg.value;
1010
}
11-
// TODO: remove this helper function once isoltest supports balance checking
12-
function balance() external returns (uint) {
13-
return address(this).balance;
14-
}
1511
}
1612
// ====
1713
// compileViaYul: also
1814
// compileToEwasm: also
1915
// ----
2016
// f(), 27 wei -> FAILURE
21-
// balance() -> 0
17+
// contractBalance -> 0

0 commit comments

Comments
 (0)