-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
149 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "./MulticallTokenMock.sol"; | ||
|
||
contract MulticallTest { | ||
function testReturnValues(MulticallTokenMock multicallToken, address[] calldata recipients, uint256[] calldata amounts) external { | ||
bytes[] memory calls = new bytes[](recipients.length); | ||
for (uint i = 0; i < recipients.length; i++) { | ||
calls[i] = abi.encodeWithSignature("transfer(address,uint256)", recipients[i], amounts[i]); | ||
} | ||
|
||
bytes[] memory results = multicallToken.multicall(calls); | ||
for (uint i = 0; i < results.length; i++) { | ||
require(abi.decode(results[i], (bool))); | ||
} | ||
} | ||
} |
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,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../utils/Multicall.sol"; | ||
import "./ERC20Mock.sol"; | ||
|
||
contract MulticallTokenMock is ERC20Mock, Multicall { | ||
constructor (uint256 initialBalance) ERC20Mock("MulticallToken", "BCT", msg.sender, initialBalance) {} | ||
} |
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,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "./Address.sol"; | ||
|
||
/** | ||
* @dev Provides a function to batch together multiple calls in a single external call. | ||
*/ | ||
abstract contract Multicall { | ||
/** | ||
* @dev Receives and executes a batch of function calls on this contract. | ||
*/ | ||
function multicall(bytes[] calldata data) external returns (bytes[] memory results) { | ||
results = new bytes[](data.length); | ||
for (uint i = 0; i < data.length; i++) { | ||
results[i] = Address.functionDelegateCall(address(this), data[i]); | ||
} | ||
return results; | ||
} | ||
} |
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,57 @@ | ||
const { BN, expectRevert } = require('@openzeppelin/test-helpers'); | ||
const MulticallTokenMock = artifacts.require('MulticallTokenMock'); | ||
|
||
contract('MulticallToken', function (accounts) { | ||
const [deployer, alice, bob] = accounts; | ||
const amount = 12000; | ||
|
||
beforeEach(async function () { | ||
this.multicallToken = await MulticallTokenMock.new(new BN(amount), { from: deployer }); | ||
}); | ||
|
||
it('batches function calls', async function () { | ||
expect(await this.multicallToken.balanceOf(alice)).to.be.bignumber.equal(new BN('0')); | ||
expect(await this.multicallToken.balanceOf(bob)).to.be.bignumber.equal(new BN('0')); | ||
|
||
await this.multicallToken.multicall([ | ||
this.multicallToken.contract.methods.transfer(alice, amount / 2).encodeABI(), | ||
this.multicallToken.contract.methods.transfer(bob, amount / 3).encodeABI(), | ||
], { from: deployer }); | ||
|
||
expect(await this.multicallToken.balanceOf(alice)).to.be.bignumber.equal(new BN(amount / 2)); | ||
expect(await this.multicallToken.balanceOf(bob)).to.be.bignumber.equal(new BN(amount / 3)); | ||
}); | ||
|
||
it('returns an array with the result of each call', async function () { | ||
const MulticallTest = artifacts.require('MulticallTest'); | ||
const multicallTest = await MulticallTest.new({ from: deployer }); | ||
await this.multicallToken.transfer(multicallTest.address, amount, { from: deployer }); | ||
expect(await this.multicallToken.balanceOf(multicallTest.address)).to.be.bignumber.equal(new BN(amount)); | ||
|
||
const recipients = [alice, bob]; | ||
const amounts = [amount / 2, amount / 3].map(n => new BN(n)); | ||
|
||
await multicallTest.testReturnValues(this.multicallToken.address, recipients, amounts); | ||
}); | ||
|
||
it('reverts previous calls', async function () { | ||
expect(await this.multicallToken.balanceOf(alice)).to.be.bignumber.equal(new BN('0')); | ||
|
||
const call = this.multicallToken.multicall([ | ||
this.multicallToken.contract.methods.transfer(alice, amount).encodeABI(), | ||
this.multicallToken.contract.methods.transfer(bob, amount).encodeABI(), | ||
], { from: deployer }); | ||
|
||
await expectRevert(call, 'ERC20: transfer amount exceeds balance'); | ||
expect(await this.multicallToken.balanceOf(alice)).to.be.bignumber.equal(new BN('0')); | ||
}); | ||
|
||
it('bubbles up revert reasons', async function () { | ||
const call = this.multicallToken.multicall([ | ||
this.multicallToken.contract.methods.transfer(alice, amount).encodeABI(), | ||
this.multicallToken.contract.methods.transfer(bob, amount).encodeABI(), | ||
], { from: deployer }); | ||
|
||
await expectRevert(call, 'ERC20: transfer amount exceeds balance'); | ||
}); | ||
}); |