Stop instrumenting receive() for statements / fns (to save gas) (#517)
parent
1ba04ac038
commit
f1fb8a0f49
@ -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; |
||||
} |
||||
} |
@ -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); |
||||
}); |
||||
}); |
||||
|
Loading…
Reference in new issue