-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop instrumenting receive() for statements / fns (to save gas) (#517)
- Loading branch information
Showing
5 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pragma solidity ^0.6.0; | ||
|
||
contract B_Wallet { | ||
|
||
event Deposit(address indexed _sender, uint _value, bytes data); | ||
|
||
receive() external payable | ||
{ | ||
if (msg.value > 0) | ||
emit Deposit(msg.sender, msg.value, msg.data); | ||
} | ||
|
||
function transferPayment(uint payment, address payable recipient) public { | ||
recipient.transfer(payment); | ||
} | ||
|
||
function sendPayment(uint payment, address payable recipient) public { | ||
require(recipient.send(payment), 'sendPayment failed'); | ||
} | ||
|
||
function getBalance() public view returns(uint){ | ||
return address(this).balance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const Wallet = artifacts.require('B_Wallet'); | ||
|
||
contract('B_Wallet', accounts => { | ||
it('should should allow transfers and sends', async () => { | ||
const walletA = await Wallet.new(); | ||
const walletB = await Wallet.new(); | ||
|
||
await walletA.sendTransaction({ | ||
value: web3.utils.toBN(500), from: accounts[0], | ||
}); | ||
|
||
await walletA.sendPayment(50, walletB.address, { | ||
from: accounts[0], | ||
}); | ||
|
||
await walletA.transferPayment(50, walletB.address, { | ||
from: accounts[0], | ||
}); | ||
|
||
// Also try transferring 0, for branch hit | ||
await walletA.transferPayment(0, walletB.address, { | ||
from: accounts[0], | ||
}); | ||
|
||
// Throws invalid opcode if compiled w/ solc >= 0.5.14 & default EVM version | ||
const balance = await walletB.getBalance(); | ||
assert.equal(balance.toNumber(), 100); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters