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

feat: [ISSUE-119] feat: adding a receiver as parameter in the accept swap function #149

4 changes: 2 additions & 2 deletions contracts/Swaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {
/**
* @dev See {ISwaplace-acceptSwap}.
*/
function acceptSwap(uint256 swapId) public returns (bool) {
function acceptSwap(uint256 swapId, address receiver) public returns (bool) {
Swap memory swap = _swaps[swapId];

if (swap.allowed != address(0) && swap.allowed != msg.sender) {
Expand Down Expand Up @@ -85,7 +85,7 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {
for (uint256 i = 0; i < assets.length; ) {
ITransfer(assets[i].addr).transferFrom(
swap.owner,
msg.sender,
receiver,
assets[i].amountOrId
);
unchecked {
Expand Down
2 changes: 1 addition & 1 deletion contracts/echidna/TestSwaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract TestSwaplace is TestFactory {
_token.approve(address(_swaplace), type(uint256).max);

uint256 lastId = _swaplace.totalSwaps();
return (_swaplace.acceptSwap(lastId));
return (_swaplace.acceptSwap(lastId, address(0)));
}

function echidna_id_overflow() public view returns (bool) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/ISwaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ interface ISwaplace {
* NOTE: The expiry is set to 0, because if the Swap is expired it
* will revert, preventing reentrancy attacks.
*/
function acceptSwap(uint256 swapId) external returns (bool);
function acceptSwap(uint256 swapId, address receiver) external returns (bool);

/**
* @dev Cancels an active Swap by setting the expiry to zero.
Expand Down
4 changes: 2 additions & 2 deletions scripts/acceptSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import abi from "../artifacts/contracts/Swaplace.sol/Swaplace.json";

export async function main() {
// Get the first account from the list of accounts
const [signer] = await ethers.getSigners();
const [signer, receiver] = await ethers.getSigners();

// Get the Swaplace address from .env file
const swaplaceAddress: string = process.env.SWAPLACE_ADDRESS || "";
Expand All @@ -15,7 +15,7 @@ export async function main() {
const swapId = 1;

// Accept the swap
const tx = await Swaplace.acceptSwap(swapId);
const tx = await Swaplace.acceptSwap(swapId, receiver.address);

// Wait for the transaction to be mined
await tx.wait();
Expand Down
33 changes: 33 additions & 0 deletions scripts/approveToken.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script is not part of the proposal and should not be committed

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ethers } from "hardhat";
import { ERC20ABI } from "../test/utils/utils";

export async function main() {
// Get the first account from the list of accounts
const [signer] = await ethers.getSigners();

// Get the Swaplace address from .env file
const swaplaceAddress: string = process.env.SWAPLACE_ADDRESS || "";

// Get the token contract, replace to the token that you want approve
const tokenContract = "";

// Get the token instance
const Token = new ethers.Contract(tokenContract, ERC20ABI, signer);

// Get the value to approve token, replace to value that you need
const valueApproveToken = BigInt(99999 * 10 ** 18);

// Make the approve
const tx = Token.approve(swaplaceAddress, valueApproveToken);

// Wait for the transaction to be mined
await tx.wait();

// Log the transaction hash
console.log("\nTransaction Hash: ", tx);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
31 changes: 25 additions & 6 deletions test/TestSwaplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("Swaplace", async function () {
let deployer: SignerWithAddress;
let owner: SignerWithAddress;
let acceptee: SignerWithAddress;
let receiver: SignerWithAddress;

// Solidity address(0)
const zeroAddress = ethers.constants.AddressZero;
Expand Down Expand Up @@ -45,7 +46,7 @@ describe("Swaplace", async function () {
}

before(async () => {
[deployer, owner, acceptee] = await ethers.getSigners();
[deployer, owner, acceptee, receiver] = await ethers.getSigners();
Swaplace = await deploy("Swaplace", deployer);
MockERC20 = await deploy("MockERC20", deployer);
MockERC721 = await deploy("MockERC721", deployer);
Expand Down Expand Up @@ -275,6 +276,7 @@ describe("Swaplace", async function () {
await expect(
await Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.emit(Swaplace, "SwapAccepted")
Expand Down Expand Up @@ -307,6 +309,7 @@ describe("Swaplace", async function () {
await expect(
await Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.emit(Swaplace, "SwapAccepted")
Expand All @@ -330,6 +333,7 @@ describe("Swaplace", async function () {
await expect(
await Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.emit(Swaplace, "SwapAccepted")
Expand All @@ -344,13 +348,17 @@ describe("Swaplace", async function () {
await expect(
await Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.emit(Swaplace, "SwapAccepted")
.withArgs(await Swaplace.totalSwaps(), acceptee.address);

await expect(
Swaplace.connect(acceptee).acceptSwap(await Swaplace.totalSwaps()),
Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.be.revertedWithCustomError(Swaplace, `InvalidExpiry`)
.withArgs(0);
Expand All @@ -362,7 +370,10 @@ describe("Swaplace", async function () {
await network.provider.send("evm_increaseTime", [swap.expiry * 2]);

await expect(
Swaplace.connect(owner).acceptSwap(await Swaplace.totalSwaps()),
Swaplace.connect(owner).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.be.revertedWithCustomError(Swaplace, `InvalidExpiry`)
.withArgs(swap.expiry);
Expand All @@ -374,7 +385,10 @@ describe("Swaplace", async function () {
await Swaplace.connect(owner).createSwap(swap);

await expect(
Swaplace.connect(acceptee).acceptSwap(await Swaplace.totalSwaps()),
Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
).to.be.revertedWith(`ERC721: caller is not token owner or approved`);
});

Expand All @@ -393,7 +407,10 @@ describe("Swaplace", async function () {
.withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry);

await expect(
Swaplace.connect(acceptee).acceptSwap(await Swaplace.totalSwaps()),
Swaplace.connect(acceptee).acceptSwap(
await Swaplace.totalSwaps(),
receiver.address,
),
)
.to.be.revertedWithCustomError(Swaplace, "InvalidAddress")
.withArgs(acceptee.address);
Expand All @@ -418,7 +435,9 @@ describe("Swaplace", async function () {

it("Should not be able to {acceptSwap} a canceled a Swap", async function () {
const lastSwap = await Swaplace.totalSwaps();
await expect(Swaplace.connect(owner).acceptSwap(lastSwap))
await expect(
Swaplace.connect(owner).acceptSwap(lastSwap, receiver.address),
)
.to.be.revertedWithCustomError(Swaplace, `InvalidExpiry`)
.withArgs(0);
});
Expand Down
16 changes: 16 additions & 0 deletions test/utils/utils.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition is not part of the issue and should be added in this PR

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,23 @@ export async function deploy(contractName: any, signer: any) {
return Contract;
}

/**
* @dev ABI of ERC20 standard
*/
export const ERC20ABI = [
"function name() public view returns (string)",
"function symbol() public view returns (string)",
"function decimals() public view returns (uint8)",
"function totalSupply() public view returns (uint256)",
"function balanceOf(address _owner) public view returns (uint256 balance)",
"function transfer(address _to, uint256 _value) public returns (bool success)",
"function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)",
"function approve(address _spender, uint256 _value) public returns (bool success)",
"function allowance(address _owner, address _spender) public view returns (uint256 remaining)",
];

module.exports = {
blocktimestamp,
deploy,
ERC20ABI,
};
Loading