Skip to content

Commit 66ef23b

Browse files
aarltLeonardo Alt
authored and
Leonardo Alt
committed
[isoltest] Add support to query balance.
1 parent 1eff128 commit 66ef23b

7 files changed

+69
-5
lines changed

test/libsolidity/SemanticTest.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <optional>
3232
#include <stdexcept>
3333
#include <utility>
34+
#include <functional>
3435

3536
using namespace std;
3637
using namespace solidity;
@@ -61,6 +62,8 @@ SemanticTest::SemanticTest(
6162
m_enforceGasCost(_enforceGasCost),
6263
m_enforceGasCostMinValue(_enforceGasCostMinValue)
6364
{
65+
initializeBuiltins();
66+
6467
string choice = m_reader.stringSetting("compileViaYul", "default");
6568
if (choice == "also")
6669
{
@@ -122,6 +125,21 @@ SemanticTest::SemanticTest(
122125
}
123126
}
124127

128+
void SemanticTest::initializeBuiltins()
129+
{
130+
m_builtins["account_balance"] = [this](FunctionCall const& _call) -> std::optional<bytes>
131+
{
132+
soltestAssert(_call.arguments.parameters.size() == 1, "Account address expected.");
133+
h160 address = h160(_call.arguments.parameters.at(0).rawString);
134+
return util::toBigEndian(SolidityExecutionFramework::balanceAt(address));
135+
};
136+
m_builtins["contract_balance"] = [this](FunctionCall const& _call) -> std::optional<bytes>
137+
{
138+
soltestAssert(_call.arguments.parameters.empty(), "No arguments expected.");
139+
return util::toBigEndian(SolidityExecutionFramework::balanceAt(m_contractAddress));
140+
};
141+
}
142+
125143
TestCase::TestResult SemanticTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
126144
{
127145
TestResult result = TestResult::Success;

test/libsolidity/SemanticTest.h

+4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ class SemanticTest: public SolidityExecutionFramework, public EVMVersionRestrict
7777

7878
private:
7979
TestResult runTest(std::ostream& _stream, std::string const& _linePrefix, bool _formatted, bool _compileViaYul, bool _compileToEwasm);
80+
8081
bool checkGasCostExpectation(TestFunctionCall& io_test, bool _compileViaYul) const;
82+
83+
void initializeBuiltins();
84+
8185
SourceMap m_sources;
8286
std::size_t m_lineOffset;
8387
std::vector<TestFunctionCall> m_tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// contract_balance -> 1500
22+
// getAddress() -> 0xf01f7809444bd9a93a854361c6fae3f23d9e23db
23+
// account_balance: 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+
// contract_balance -> 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+
// contract_balance -> 1000000000000000000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
contract ClientReceipt {
2+
}
3+
// ====
4+
// compileViaYul: also
5+
// ----
6+
// contract_balance -> 0
7+
// account_balance: 0x0000000000000000000000000000000000000000 -> 0

test/libsolidity/semanticTests/payable/no_nonpayable_circumvention_by_modifier.sol

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ 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
// ----
1915
// f(), 27 wei -> FAILURE
20-
// balance() -> 0
16+
// contract_balance -> 0

0 commit comments

Comments
 (0)