Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Removed balance variable from ETHApp.sol (paritytech#567)
Browse files Browse the repository at this point in the history
* Removed non-required balance variable

* Added reject message

* removed require condition
  • Loading branch information
sumitsnk authored Jan 24, 2022
1 parent 794847a commit 27d42ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
10 changes: 0 additions & 10 deletions ethereum/contracts/ETHApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ contract ETHApp is RewardSource, AccessControl {
using ScaleCodec for uint256;
using ScaleCodec for uint32;

uint256 public balance;

mapping(ChannelId => Channel) public channels;

event Locked(
Expand Down Expand Up @@ -46,7 +44,6 @@ contract ETHApp is RewardSource, AccessControl {
Channel memory _basic,
Channel memory _incentivized
) {
balance = 0;

Channel storage c1 = channels[ChannelId.Basic];
c1.inbound = _basic.inbound;
Expand All @@ -73,8 +70,6 @@ contract ETHApp is RewardSource, AccessControl {
"Invalid channel ID"
);

balance = balance + msg.value;

emit Locked(msg.sender, _recipient, msg.value, _paraId);

bytes memory call;
Expand Down Expand Up @@ -105,12 +100,7 @@ contract ETHApp is RewardSource, AccessControl {
uint256 _amount
) public onlyRole(INBOUND_CHANNEL_ROLE) {
require(_amount > 0, "Must unlock a positive amount");
require(
balance >= _amount,
"ETH token balances insufficient to fulfill the unlock request"
);

balance = balance - _amount;
(bool success, ) = _recipient.call{value: _amount}("");
require(success, "Unable to send Ether");
emit Unlocked(_sender, _recipient, _amount);
Expand Down
23 changes: 16 additions & 7 deletions ethereum/test/test_eth_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ describe("ETHApp", function () {
});

it("should lock funds", async function () {
const beforeBalance = BigNumber(await this.app.balance());

const beforeBalance = BigNumber(await web3.eth.getBalance(this.app.address));
const amount = BigNumber(web3.utils.toWei("0.25", "ether"));

const tx = await lockupFunds(this.app, userOne, POLKADOT_ADDRESS, amount, ChannelId.Basic)
Expand All @@ -71,11 +72,8 @@ describe("ETHApp", function () {

// Confirm contract's balance has increased
const afterBalance = await web3.eth.getBalance(this.app.address);
afterBalance.should.be.bignumber.equal(amount);
afterBalance.should.be.bignumber.equal(beforeBalance.plus(amount));

// Confirm contract's locked balance state has increased by amount locked
const afterBalanceState = BigNumber(await this.app.balance());
afterBalanceState.should.be.bignumber.equal(beforeBalance.plus(amount));
});
})

Expand All @@ -98,9 +96,20 @@ describe("ETHApp", function () {
// expected amount to unlock
const amount = web3.utils.toWei("1", "ether");

const beforeBalance = BigNumber(await this.app.balance());
const beforeBalance = BigNumber(await web3.eth.getBalance(this.app.address));
const beforeRecipientBalance = BigNumber(await web3.eth.getBalance(recipient));

const unlockAmount = web3.utils.toBN( web3.utils.toWei("2", "ether")).add(web3.utils.toBN(1))

await this.app.unlock(
addressBytes(POLKADOT_ADDRESS),
recipient,
unlockAmount.toString(),
{
from: inboundChannel,
}
).should.be.rejectedWith(/Unable to send Ether/);

let { receipt } = await this.app.unlock(
addressBytes(POLKADOT_ADDRESS),
recipient,
Expand All @@ -121,7 +130,7 @@ describe("ETHApp", function () {
event.recipient.should.be.equal(recipient);
event.amount.eq(ethers.BigNumber.from(amount)).should.be.true;

const afterBalance = BigNumber(await this.app.balance());
const afterBalance = BigNumber(await web3.eth.getBalance(this.app.address));
const afterRecipientBalance = BigNumber(await web3.eth.getBalance(recipient));

afterBalance.should.be.bignumber.equal(beforeBalance.minus(amount));
Expand Down

0 comments on commit 27d42ff

Please sign in to comment.