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

Block tx from contracts #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions contracts/ito.sol
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ contract HappyTokenPool is Initializable {
)
public payable returns (uint256 swapped) {

require(tx.origin == msg.sender, "not a real user");
uint128 from_value = input_total;
Pool storage pool = pool_by_id[id];
Packed1 memory packed1 = pool.packed1;
Expand Down
23 changes: 23 additions & 0 deletions contracts/test/MultiCall.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity >=0.5.0;

import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";

contract Multicall is IERC721Receiver {
struct Call {
address target;
bytes callData;
uint256 value;
}

function aggregate(Call[] memory calls) external payable {
for(uint256 i = 0; i < calls.length; i++) {
(bool success, bytes memory ret) = calls[i].target.call{ value: calls[i].value}(calls[i].callData);
require(success, "Multicall: call failed");
}
}

function onERC721Received(address, address, uint256, bytes calldata)
public override pure returns(bytes4) {
return this.onERC721Received.selector;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions test/TestTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ let snapshotId;
let testTokenADeployed;
let testTokenBDeployed;
let testTokenCDeployed;
let multiCallContract;

let HappyTokenPool;
let happyTokenPoolDeployed;
Expand Down Expand Up @@ -70,6 +71,11 @@ describe('HappyTokenPool', () => {
HappyTokenPool = await ethers.getContractFactory('HappyTokenPool');
HappyTokenPoolProxy = await upgrades.deployProxy(HappyTokenPool, [base_timestamp]);
happyTokenPoolDeployed = new ethers.Contract(HappyTokenPoolProxy.address, itoJsonABI.abi, creator);
{
const factory = await ethers.getContractFactory('Multicall');
const nContract = await factory.deploy();
multiCallContract = await nContract.deployed();
}
});

beforeEach(async () => {
Expand Down Expand Up @@ -681,6 +687,31 @@ describe('HappyTokenPool', () => {
}
});

it('Should swap tx from contracts not work', async () => {
fpp.total_tokens = BigNumber('100e18').toFixed();
fpp.limit = BigNumber('50e18').toFixed();
const { id: pool_id } = await getResultFromPoolFill(happyTokenPoolDeployed, fpp);

// 0.004 ETH => 40 TESTA
approve_amount = BigNumber('4e15').toFixed();
exchange_amount = approve_amount;
var vr = getVerification(PASSWORD, multiCallContract.address);
const encodedSwapData = itoInterface.encodeFunctionData('swap', [
pool_id,
vr.verification,
0,
exchange_amount,
[pool_id],
]);
await expect(
multiCallContract
.connect(signers[4])
.aggregate([[happyTokenPoolDeployed.address, encodedSwapData, approve_amount]], {
value: approve_amount,
}),
).to.be.rejectedWith('Multicall: call failed');
});

it('Should swap the remaining token when the amount of swap token is greater than total token', async () => {
const ratio = 10 ** 10;
fpp.exchange_ratios = [1, ratio];
Expand Down