Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: await #27

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/factory/Exchange.vy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(_token: IERC20, _factory: Factory):
@external
def initialize():
# Anyone can safely call this function because of EXTCODEHASH
self.factory.register()
extcall self.factory.register()


# NOTE: This contract restricts trading to only be done by the factory.
Expand All @@ -31,12 +31,12 @@ def initialize():
@external
def receive(_from: address, _amt: uint256):
assert msg.sender == self.factory.address
success: bool = self.token.transferFrom(_from, self, _amt)
success: bool = extcall self.token.transferFrom(_from, self, _amt)
assert success


@external
def transfer(_to: address, _amt: uint256):
assert msg.sender == self.factory.address
success: bool = self.token.transfer(_to, _amt)
success: bool = extcall self.token.transfer(_to, _amt)
assert success
6 changes: 3 additions & 3 deletions examples/factory/Factory.vy
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def register():
# NOTE: Should do checks that it hasn't already been set,
# which has to be rectified with any upgrade strategy.
exchange: Exchange = Exchange(msg.sender)
self.exchanges[exchange.token()] = exchange
self.exchanges[staticcall exchange.token()] = exchange


@external
def trade(_token1: IERC20, _token2: IERC20, _amt: uint256):
# Perform a straight exchange of token1 to token 2 (1:1 price)
# NOTE: Any practical implementation would need to solve the price oracle problem
self.exchanges[_token1].receive(msg.sender, _amt)
self.exchanges[_token2].transfer(msg.sender, _amt)
extcall self.exchanges[_token1].receive(msg.sender, _amt)
extcall self.exchanges[_token2].transfer(msg.sender, _amt)
20 changes: 10 additions & 10 deletions examples/tokens/ERC4626.vy
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def transferFrom(sender: address, receiver: address, amount: uint256) -> bool:
@view
@external
def totalAssets() -> uint256:
return self.asset.balanceOf(self)
return staticcall self.asset.balanceOf(self)


@view
Expand All @@ -91,7 +91,7 @@ def _convertToAssets(shareAmount: uint256) -> uint256:

# NOTE: `shareAmount = 0` is extremely rare case, not optimizing for it
# NOTE: `totalAssets = 0` is extremely rare case, not optimizing for it
return shareAmount * self.asset.balanceOf(self) // totalSupply
return shareAmount * staticcall self.asset.balanceOf(self) // totalSupply


@view
Expand All @@ -104,7 +104,7 @@ def convertToAssets(shareAmount: uint256) -> uint256:
@internal
def _convertToShares(assetAmount: uint256) -> uint256:
totalSupply: uint256 = self.totalSupply
totalAssets: uint256 = self.asset.balanceOf(self)
totalAssets: uint256 = staticcall self.asset.balanceOf(self)
if totalAssets == 0 or totalSupply == 0:
return assetAmount # 1:1 price

Expand Down Expand Up @@ -133,7 +133,7 @@ def previewDeposit(assets: uint256) -> uint256:
@external
def deposit(assets: uint256, receiver: address=msg.sender) -> uint256:
shares: uint256 = self._convertToShares(assets)
self.asset.transferFrom(msg.sender, self, assets)
extcall self.asset.transferFrom(msg.sender, self, assets)

self.totalSupply += shares
self.balanceOf[receiver] += shares
Expand All @@ -153,7 +153,7 @@ def previewMint(shares: uint256) -> uint256:
assets: uint256 = self._convertToAssets(shares)

# NOTE: Vyper does lazy eval on `and`, so this avoids SLOADs most of the time
if assets == 0 and self.asset.balanceOf(self) == 0:
if assets == 0 and staticcall self.asset.balanceOf(self) == 0:
return shares # NOTE: Assume 1:1 price if nothing deposited yet

return assets
Expand All @@ -163,10 +163,10 @@ def previewMint(shares: uint256) -> uint256:
def mint(shares: uint256, receiver: address=msg.sender) -> uint256:
assets: uint256 = self._convertToAssets(shares)

if assets == 0 and self.asset.balanceOf(self) == 0:
if assets == 0 and staticcall self.asset.balanceOf(self) == 0:
assets = shares # NOTE: Assume 1:1 price if nothing deposited yet

self.asset.transferFrom(msg.sender, self, assets)
extcall self.asset.transferFrom(msg.sender, self, assets)

self.totalSupply += shares
self.balanceOf[receiver] += shares
Expand Down Expand Up @@ -206,7 +206,7 @@ def withdraw(assets: uint256, receiver: address=msg.sender, owner: address=msg.s
self.totalSupply -= shares
self.balanceOf[owner] -= shares

self.asset.transfer(receiver, assets)
extcall self.asset.transfer(receiver, assets)
log IERC4626.Withdraw(msg.sender, receiver, owner, assets, shares)
return shares

Expand All @@ -232,7 +232,7 @@ def redeem(shares: uint256, receiver: address=msg.sender, owner: address=msg.sen
self.totalSupply -= shares
self.balanceOf[owner] -= shares

self.asset.transfer(receiver, assets)
extcall self.asset.transfer(receiver, assets)
log IERC4626.Withdraw(msg.sender, receiver, owner, assets, shares)
return assets

Expand All @@ -241,4 +241,4 @@ def redeem(shares: uint256, receiver: address=msg.sender, owner: address=msg.sen
def DEBUG_steal_tokens(amount: uint256):
# NOTE: This is the primary method of mocking share price changes
# do not put in production code!!!
self.asset.transfer(msg.sender, amount)
extcall self.asset.transfer(msg.sender, amount)
2 changes: 1 addition & 1 deletion examples/tokens/ERC721.vy
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def safeTransferFrom(
"""
self._transferFrom(_from, _to, _tokenId, msg.sender)
if _to.is_contract: # check if `_to` is a contract address
returnValue: bytes4 = ERC721Receiver(_to).onERC721Received(msg.sender, _from, _tokenId, _data)
returnValue: bytes4 = extcall ERC721Receiver(_to).onERC721Received(msg.sender, _from, _tokenId, _data)
# Throws if transfer destination is a contract which does not implement 'onERC721Received'
assert returnValue == method_id("onERC721Received(address,address,uint256,bytes)", output_type=bytes4)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ def bar() -> bool: view

@external
def foo(a: address):
staticcall Bar(a).bar(1)
a: bool = staticcall Bar(a).bar(1)
""",
"""
interface Bar:
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/codegen/calling_convention/test_return.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ def foo(addr: address) -> Foo:
return Foo(
a=1,
b=2,
c=IBar(addr).bar().a,
c=(staticcall IBar(addr).bar()).a,
d=4,
e=5
)
Expand Down Expand Up @@ -693,7 +693,7 @@ def foo(addr: address) -> Foo:
b=2,
c=(staticcall IBar(addr).bar()).a,
d=4,
e=(staticcall IBar(addr).baz(IBar(addr).bar()).b)
e=(staticcall IBar(addr).baz(staticcall IBar(addr).bar())).b
)
"""

Expand Down Expand Up @@ -732,7 +732,7 @@ def baz(a: uint256) -> uint256: view
@external
def foo(addr: address) -> Foo:
return Foo(
a=staticcall IBar(addr).baz(IBar(addr).bar()).a
a=(staticcall IBar(addr).baz(staticcall IBar(addr).bar())).a
)
"""

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/codegen/types/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test(addr: address) -> (int128, address, String[10]):
a: int128 = 0
b: address = empty(address)
c: String[10] = ""
(a, b, c) = Test(addr).out_literals()
(a, b, c) = staticcall Test(addr).out_literals()
return a, b,c
"""
c1 = get_contract_with_gas_estimation(contract_1)
Expand Down
28 changes: 11 additions & 17 deletions tests/functional/syntax/exceptions/test_constancy_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,37 +83,31 @@ def b():
"""
interface A:
def bar() -> uint16: view

@external
@pure
def test(to:address):
a:A = A(to)
x:uint16 = a.bar()
""",
"""
interface A:
def bar() -> uint16: view
@external
@pure
def test(to:address):
a:A = A(to)
a.bar()
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
def test(to: address):
a: A = A(to)
x: uint16 = staticcall a.bar()
""",
"""
interface A:
def bar() -> uint16: nonpayable

@external
@view
def test(to:address):
a:A = A(to)
x:uint16 = a.bar()
def test(to: address):
a: A = A(to)
x: uint16 = extcall a.bar()
""",
"""
interface A:
def bar() -> uint16: nonpayable

@external
@view
def test(to:address):
a:A = A(to)
def test(to: address):
a: A = A(to)
a.bar()
""",
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/syntax/modules/test_initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ def foo() -> (uint256, uint256): nonpayable

@internal
def foo() -> uint256:
lib1.counter[1][2], self.something = Foo(msg.sender).foo()
lib1.counter[1][2], self.something = extcall Foo(msg.sender).foo()
"""
main = """
import lib1
Expand Down Expand Up @@ -630,7 +630,7 @@ def foo() -> (uint256, uint256): nonpayable

@internal
def write_tuple():
self.counter[1][2], self.something = Foo(msg.sender).foo()
self.counter[1][2], self.something = extcall Foo(msg.sender).foo()
"""
lib2 = """
import lib1
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/syntax/test_address_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def out_literals() -> address : view

@external
def foo(x: address) -> Bytes[4]:
return slice(Test(x).out_literals().code, 0, 4)
return slice((staticcall Test(x).out_literals()).code, 0, 4)
""",
],
)
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/syntax/test_for_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def kick(): nonpayable
@external
def kick_foos():
for foo: Foo in self.foos:
foo.kick()
extcall foo.kick()
""",
]

Expand Down
12 changes: 6 additions & 6 deletions tests/functional/syntax/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def test_interfaces_fail(bad_code):
b: IERC20
@external
def test(input: address):
assert self.b.totalSupply() == IERC20(input).totalSupply()
assert staticcall self.b.totalSupply() == staticcall IERC20(input).totalSupply()
""",
"""
from ethereum.ercs import IERC20
Expand All @@ -243,10 +243,10 @@ def getExchange(token_addr: address) -> address: view

@external
def test():
assert self.factory.getExchange(self.token.address) == self
exchange: address = self.factory.getExchange(self.token.address)
assert staticcall self.factory.getExchange(self.token.address) == self
exchange: address = staticcall self.factory.getExchange(self.token.address)
assert exchange == self.token.address
assert self.token.totalSupply() > 0
assert staticcall self.token.totalSupply() > 0
""",
"""
interface Foo:
Expand Down Expand Up @@ -326,7 +326,7 @@ def append(a: uint256): payable
@external
def bar(x: address):
a: Foo = Foo(x)
a.append(1)
extcall a.append(1)
""",
"""
interface Foo:
Expand All @@ -335,7 +335,7 @@ def pop(): payable
@external
def foo(x: address):
a: Foo = Foo(x)
a.pop()
extcall a.pop()
""",
"""
interface ITestInterface:
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/syntax/test_return_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def foo() -> (uint256, uint256): view

@external
def foo(a: address) -> (uint256, uint256, uint256, uint256, uint256):
return 1, 2, Foo(a).foo()[0], 4, 5
return 1, 2, (staticcall Foo(a).foo())[0], 4, 5
"""

c = get_contract(code)
Expand Down Expand Up @@ -132,7 +132,7 @@ def bar(a: uint256) -> uint256: view

@external
def foo(a: address) -> (uint256, uint256, uint256, uint256, uint256):
return 1, 2, Foo(a).foo()[0], 4, Foo(a).bar(Foo(a).foo()[1])
return 1, 2, (staticcall Foo(a).foo())[0], 4, staticcall Foo(a).bar((staticcall Foo(a).foo())[1])
"""

c = get_contract(code)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/cli/vyper_compile/test_compile_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def foo() -> {alias}.FooStruct:

@external
def bar(a: address) -> {alias}.FooStruct:
return {alias}(a).bar()
return extcall {alias}(a).bar()
"""

INTERFACE_CODE = """
Expand Down Expand Up @@ -172,7 +172,7 @@ def be_known() -> FooStruct:

@external
def know_thyself(a: address) -> ISelf.FooStruct:
return ISelf(a).be_known()
return extcall ISelf(a).be_known()

@external
def be_known() -> ISelf.FooStruct:
Expand All @@ -192,11 +192,11 @@ def test_another_interface_implementation(import_stmt_foo, alias, tmp_path, make

@external
def foo(a: address) -> {alias}.FooStruct:
return {alias}(a).foo()
return extcall {alias}(a).foo()

@external
def bar(_foo: address) -> {alias}.FooStruct:
return {alias}(_foo).bar()
return extcall {alias}(_foo).bar()
"""
make_file("contracts/IFoo.vyi", INTERFACE_CODE)
baz = make_file("contracts/Baz.vy", baz_code)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/cli/vyper_json/test_compile_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@external
def foo(a: address) -> bool:
return IBar(a).bar(1)
return extcall IBar(a).bar(1)

@external
def baz() -> uint256:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/cli/vyper_json/test_parse_args_vyperjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@external
def foo(a: address) -> bool:
return IBar(a).bar(1)
return extcall IBar(a).bar(1)
"""

BAR_CODE = """
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/compiler/test_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def foo():

@external
def bar():
ifoo(msg.sender).foo()
extcall ifoo(msg.sender).foo()
"""
input_bundle = make_input_bundle({"ifoo.vyi": ifoo})
out = compile_code(main, input_bundle=input_bundle, output_formats=["abi"])
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/compiler/test_pre_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def bar() -> int128: nonpayable
@external
def foo(contract_address: address) -> int128:
self.bar_contract = Bar(contract_address)
return self.bar_contract.bar()
return extcall self.bar_contract.bar()
"""

c1 = get_contract(contract_1)
Expand Down
Loading