Skip to content

Commit 7def49c

Browse files
committed
[isoltest] Add support to query balance.
1 parent c180cc3 commit 7def49c

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

test/libsolidity/SemanticTest.cpp

+26-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,19 @@ SemanticTest::SemanticTest(string const& _filename, langutil::EVMVersion _evmVer
4949
m_enforceViaYul(enforceViaYul)
5050
{
5151
using namespace std::placeholders;
52-
m_builtins
53-
= {{"smoke",
54-
{
55-
{"test0()", std::bind(&SemanticTest::builtinSmokeTest, this, _1)},
56-
{"test1(uint256)", std::bind(&SemanticTest::builtinSmokeTest, this, _1)},
57-
{"test2(uint256,uint256)", std::bind(&SemanticTest::builtinSmokeTest, this, _1)},
58-
}}};
52+
m_builtins = {
53+
{"smoke",
54+
{
55+
{"test0()", std::bind(&SemanticTest::builtinSmokeTest, this, _1)},
56+
{"test1(uint256)", std::bind(&SemanticTest::builtinSmokeTest, this, _1)},
57+
{"test2(uint256,uint256)", std::bind(&SemanticTest::builtinSmokeTest, this, _1)},
58+
}},
59+
{"contract",
60+
{
61+
{"balance()", std::bind(&SemanticTest::builtinContractBalance, this, _1)},
62+
{"balance(uint256)", std::bind(&SemanticTest::builtinContractBalance, this, _1)},
63+
}},
64+
};
5965

6066
string choice = m_reader.stringSetting("compileViaYul", "default");
6167
if (choice == "also")
@@ -401,3 +407,16 @@ bytes SemanticTest::builtinSmokeTest(FunctionCall const& call)
401407
result += util::toBigEndian(u256{call.arguments.parameters.at(i).rawBytes});
402408
return result;
403409
}
410+
411+
bytes SemanticTest::builtinContractBalance(FunctionCall const& call)
412+
{
413+
assert(call.arguments.parameters.size() <= 1);
414+
if (call.arguments.parameters.empty())
415+
return util::toBigEndian(SolidityExecutionFramework::balanceAt(m_contractAddress));
416+
else
417+
{
418+
h160 address{util::fromHex(call.arguments.parameters.at(0).rawString)};
419+
return util::toBigEndian(SolidityExecutionFramework::balanceAt(address));
420+
}
421+
return bytes{};
422+
}

test/libsolidity/SemanticTest.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class SemanticTest: public SolidityExecutionFramework, public EVMVersionRestrict
6262
private:
6363
// builtin functions
6464
bytes builtinSmokeTest(FunctionCall const& call);
65+
bytes builtinContractBalance(FunctionCall const& call);
6566

6667
TestResult runTest(std::ostream& _stream, std::string const& _linePrefix, bool _formatted, bool _compileViaYul, bool _compileToEwasm);
6768
SourceMap m_sources;
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(), 1000 wei ->
21+
// contract.balance() -> 500
22+
// getAddress() -> 0xF01F7809444BD9A93A854361C6FAE3F23D9E23DB
23+
// contract.balance(uint256): 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,7 @@
1+
contract ClientReceipt {
2+
}
3+
// ====
4+
// compileViaYul: also
5+
// ----
6+
// contract.balance() -> 0
7+
// contract.balance(uint256): 0x0000000000000000000000000000000000000000 -> 0

0 commit comments

Comments
 (0)