Skip to content

Commit

Permalink
Merge pull request #149 from 0xjoaovpsantos/issue-119-adding-receiver…
Browse files Browse the repository at this point in the history
…-parameter-accept-swap-function

feat: [ISSUE-119] feat: adding a receiver as parameter in the accept swap function
  • Loading branch information
0xneves authored Jan 16, 2024
2 parents 0c785b0 + 70d32bf commit af3370f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions contracts/Swaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 @@ -77,7 +77,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 @@ -56,7 +56,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
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.allowed, 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
2 changes: 1 addition & 1 deletion test/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ export async function deploy(contractName: any, signer: any) {

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

0 comments on commit af3370f

Please sign in to comment.