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

batch payments contract #881

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ jobs:
ALCHEMY_URL: ${{secrets.ALCHEMY_URL}}
- name: Delete dbg files
run: find ./artifacts/* -name "*.dbg.json" -type f -delete
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: coverage
name: coverage-contracts
path: coverage/

slither:
Expand Down Expand Up @@ -104,9 +104,9 @@ jobs:
if: ${{ success() && github.actor != 'dependabot[bot]' }}
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: coverage
name: coverage-contracts
path: coverage/
- uses: paambaati/codeclimate-action@v2.7.5
env:
Expand Down
38 changes: 38 additions & 0 deletions contracts/rewards/BatchPayments.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright BigchainDB GmbH and Ocean Protocol contributors
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0


pragma solidity 0.8.12;


interface IERC20 {
function transfer(address to, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}


contract BatchPayments {
function sendEther(address[] memory list, uint256[] memory amounts) external payable {
require(list.length == amounts.length,"Arrays must have same length");
for (uint256 i = 0; i < list.length; i++)
payable(list[i]).transfer(amounts[i]);
uint256 balance = address(this).balance;
// make sure that we return any excess to the caller
// Later TODO: Check for gas
if (balance > 23000)
payable(msg.sender).call{value: balance}("");
//payable(msg.sender).transfer(balance);
}
Dismissed Show dismissed Hide dismissed
Comment on lines +16 to +26

Check warning

Code scanning / Slither

Low-level calls Warning


function sendToken(IERC20 token, address[] memory list, uint256[] memory amounts) external {
require(list.length == amounts.length,"Arrays must have same length");
uint256 total = 0;
uint256 i;
for (i = 0; i < list.length; i++)
total += amounts[i];
require(token.transferFrom(msg.sender, address(this), total));
for (i = 0; i < list.length; i++)
require(token.transfer(list[i], amounts[i]));
}
Dismissed Show dismissed Hide dismissed
}
136 changes: 136 additions & 0 deletions test/unit/rewards/BatchPayments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const { expect } = require('chai');
const { ethers } = require("hardhat");
const { json } = require('hardhat/internal/core/params/argumentTypes');
const { web3 } = require("@openzeppelin/test-helpers/src/setup");

// Start test block
describe('Batch Payments tests', function () {
let Mock20Contract;
let Mock20DecimalsContract;
let BatchPaymentsContract;
let signers;
before(async function () {
// Get the contractOwner and collector address
signers = await ethers.getSigners();
const MockErc20 = await ethers.getContractFactory('MockERC20');
const MockErc20Decimals = await ethers.getContractFactory('MockERC20Decimals');
const BatchPayments = await ethers.getContractFactory('BatchPayments');
Mock20Contract = await MockErc20.deploy(signers[0].address,"MockERC20", 'MockERC20');
Mock20DecimalsContract = await MockErc20Decimals.deploy("Mock6Digits", 'Mock6Digits', 6);
BatchPaymentsContract = await BatchPayments.deploy();
console.log("Batch:" + BatchPaymentsContract.address)
console.log("Mock20Contract:" + Mock20Contract.address)

});


// Test cases
it('Check contract deployment', async function () {
expect(await BatchPaymentsContract.address).to.exist;

});

it('ERC20 - Should transfer tokens in batch', async function () {
const addresses = [signers[1].address, signers[2].address, signers[3].address];
const amounts = [web3.utils.toWei("100"), web3.utils.toWei("200"), web3.utils.toWei("300")];

// Approve the BatchPayments contract to transfer tokens
await Mock20Contract.approve(BatchPaymentsContract.address, web3.utils.toWei("10000"));

// Perform the batch transfer
await BatchPaymentsContract.sendToken(Mock20Contract.address, addresses, amounts);

// Check balances
expect(await Mock20Contract.balanceOf(signers[1].address)).to.equal(web3.utils.toWei("100"));
expect(await Mock20Contract.balanceOf(signers[2].address)).to.equal(web3.utils.toWei("200"));
expect(await Mock20Contract.balanceOf(signers[3].address)).to.equal(web3.utils.toWei("300"));
});

it('ERC20 - Should revert if arrays length mismatch', async function () {
const addresses = [signers[1].address, signers[2].address];
const amounts = [web3.utils.toWei("100"), web3.utils.toWei("200"), web3.utils.toWei("300")];

// Approve the BatchPayments contract to transfer tokens
await Mock20Contract.approve(BatchPaymentsContract.address, web3.utils.toWei("10000"));

// Perform the batch transfer
await expect(BatchPaymentsContract.sendToken(Mock20Contract.address, addresses, amounts)).to.be.revertedWith("Arrays must have same length");

});

it('ERC20 - Should revert if transfer fails(no prior allowence)', async function () {
const addresses = [signers[1].address, signers[2].address, signers[3].address];
const amounts = [web3.utils.toWei("100"), web3.utils.toWei("200"), web3.utils.toWei("1300")];

// Approve the BatchPayments contract to transfer tokens - insufficient amount
await Mock20Contract.approve(BatchPaymentsContract.address, web3.utils.toWei("1000"));

// Perform the batch transfer
await expect(BatchPaymentsContract.sendToken(Mock20Contract.address, addresses, amounts)).to.be.revertedWith("ERC20: insufficient allowance");

});

it('ERC20 - Should handle tokens with decimals correctly', async function () {
const addresses = [signers[1].address, signers[2].address, signers[3].address];
const amounts = [
ethers.utils.parseUnits("100", 6),
ethers.utils.parseUnits("200", 6),
ethers.utils.parseUnits("300", 6)
];

// Approve the BatchPayments contract to transfer tokens
await Mock20DecimalsContract.approve(BatchPaymentsContract.address, ethers.utils.parseUnits("10000", 6));

// Perform the batch transfer
await BatchPaymentsContract.sendToken(Mock20DecimalsContract.address, addresses, amounts);

// Check balances
expect(await Mock20DecimalsContract.balanceOf(signers[1].address)).to.equal(ethers.utils.parseUnits("100", 6));
expect(await Mock20DecimalsContract.balanceOf(signers[2].address)).to.equal(ethers.utils.parseUnits("200", 6));
expect(await Mock20DecimalsContract.balanceOf(signers[3].address)).to.equal(ethers.utils.parseUnits("300", 6));
});

it('NATIVE - Should transfer ETH in batch', async function () {
const addresses = [signers[1].address, signers[2].address, signers[3].address];
const amounts = [ethers.utils.parseEther("0.1"), ethers.utils.parseEther("0.2"), ethers.utils.parseEther("0.3")];

const initialBalances = await Promise.all(addresses.map(addr => ethers.provider.getBalance(addr)));

// Perform the batch transfer
await BatchPaymentsContract.sendEther(addresses, amounts, { value: ethers.utils.parseEther("0.6") });

// Check balances
const finalBalances = await Promise.all(addresses.map(addr => ethers.provider.getBalance(addr)));
expect(finalBalances[0]).to.equal(initialBalances[0].add(amounts[0]));
expect(finalBalances[1]).to.equal(initialBalances[1].add(amounts[1]));
expect(finalBalances[2]).to.equal(initialBalances[2].add(amounts[2]));
});

it('NATIVE - Should transfer ETH in batch and return remaining', async function () {
const owner = signers[0]
const addresses = [signers[1].address, signers[2].address, signers[3].address];
const amounts = [ethers.utils.parseEther("0.1"), ethers.utils.parseEther("0.2"), ethers.utils.parseEther("0.3")];

const initialOwnerBalance = await ethers.provider.getBalance(owner.address);
const initialContractBalance = await ethers.provider.getBalance(BatchPaymentsContract.address);
const initialBalances = await Promise.all(addresses.map(addr => ethers.provider.getBalance(addr)));

expect(initialContractBalance).to.equal(0);
// Perform the batch transfer
await BatchPaymentsContract.sendEther(addresses, amounts, { value: ethers.utils.parseEther("1.6") });

// Check balances
const finalBalances = await Promise.all(addresses.map(addr => ethers.provider.getBalance(addr)));
const finalOwnerBalance = await ethers.provider.getBalance(owner.address);
const finalContractBalance = await ethers.provider.getBalance(BatchPaymentsContract.address);
expect(finalBalances[0]).to.equal(initialBalances[0].add(amounts[0]));
expect(finalBalances[1]).to.equal(initialBalances[1].add(amounts[1]));
expect(finalBalances[2]).to.equal(initialBalances[2].add(amounts[2]));

expect(finalContractBalance).to.equal(0);
const diff=finalOwnerBalance.sub(initialOwnerBalance)
expect(Number(diff)).to.be.lessThan(Number(ethers.utils.parseEther("0.61")))
});


});
Loading