-
Notifications
You must be signed in to change notification settings - Fork 23
create simple contract for multi send #114
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
Open
0xleal
wants to merge
3
commits into
master
Choose a base branch
from
create-multisend-contract
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,33 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.24; | ||
|
|
||
| import "@openzeppelin/contracts/access/Ownable.sol"; | ||
| import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
|
||
| contract MultiSendERC20 is Ownable { | ||
| uint8 public arrayLimit; | ||
| ERC20 public token; | ||
|
|
||
| event Multisended(uint256 total, address token); | ||
|
|
||
| constructor(address _owner, address _token) Ownable(_owner) { | ||
| arrayLimit = 200; | ||
| token = ERC20(_token); | ||
| } | ||
|
|
||
| function setArrayLimit(uint8 _arrayLimit) external onlyOwner { | ||
| arrayLimit = _arrayLimit; | ||
| } | ||
|
|
||
| function multisendToken(address[] calldata _recipients, uint256[] calldata _amounts) public { | ||
| uint256 total = 0; | ||
| require(_recipients.length <= arrayLimit, "Array length exceeds limit"); | ||
| uint8 i = 0; | ||
| for (i; i < _recipients.length; i++) { | ||
| require(token.transferFrom(msg.sender, _recipients[i], _amounts[i]), "Transfer failed"); | ||
| total += _amounts[i]; | ||
| } | ||
|
|
||
| emit Multisended(total, address(token)); | ||
| } | ||
| } | ||
This file contains hidden or 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,65 @@ | ||
| import chai from "chai"; | ||
| import { ethers, waffle } from "hardhat"; | ||
| import { solidity } from "ethereum-waffle"; | ||
|
|
||
| import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; | ||
|
|
||
| import { MultiSendERC20, ERC20Mock } from "../../../typechain-types"; | ||
| import { Artifacts } from "../../shared"; | ||
|
|
||
| chai.use(solidity); | ||
|
|
||
| const { expect } = chai; | ||
| const { deployContract } = waffle; | ||
|
|
||
| describe("MultiSendERC20", () => { | ||
| let admin: SignerWithAddress; | ||
| let recipientOne: SignerWithAddress; | ||
| let recipientTwo: SignerWithAddress; | ||
| let recipientThree: SignerWithAddress; | ||
|
|
||
| let token: ERC20Mock; | ||
| let multiSend: MultiSendERC20; | ||
|
|
||
| beforeEach(async () => { | ||
| [admin, recipientOne, recipientTwo, recipientThree] = await ethers.getSigners(); | ||
| token = (await deployContract(admin, Artifacts.ERC20Mock, [admin.address])) as ERC20Mock; | ||
| }); | ||
|
|
||
| async function builder() { | ||
| return deployContract(admin, Artifacts.MultiSendERC20, [admin.address, token.address]); | ||
| } | ||
|
|
||
| describe("Deploy base contract", () => { | ||
| beforeEach(async () => { | ||
| multiSend = (await builder()) as MultiSendERC20; | ||
| }); | ||
|
|
||
| it("deploys and set ups the initial state correctly", async () => { | ||
| expect(await multiSend.token()).to.eq(token.address); | ||
| expect(await multiSend.owner()).to.eq(admin.address); | ||
| expect(await multiSend.arrayLimit()).to.eq(200); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Transfer behaviour", () => { | ||
| beforeEach(async () => { | ||
| multiSend = (await builder()) as MultiSendERC20; | ||
| await token.approve(multiSend.address, ethers.utils.parseEther("1000000000")); | ||
| }); | ||
|
|
||
| it("Should transfer tokens between one account", async function () { | ||
| await multiSend.connect(admin).multisendToken([recipientOne.address], [50]); | ||
| const recipientOneBalance = await token.balanceOf(recipientOne.address); | ||
| expect(recipientOneBalance).to.equal(50); | ||
| }); | ||
|
|
||
| it("Should transfer tokens between multiple accounts", async function () { | ||
| await multiSend.connect(admin).multisendToken([recipientOne.address, recipientTwo.address], [50, 100]); | ||
| const recipientOneBalance = await token.balanceOf(recipientOne.address); | ||
| const recipientTwoBalance = await token.balanceOf(recipientTwo.address); | ||
| expect(recipientOneBalance).to.equal(50); | ||
| expect(recipientTwoBalance).to.equal(100); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you want this to be called only by the
owner?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need, since the tokens are sent from the message owner we don't need that control. The only thing I want to control is the limit of how many can be sent at a time