This contract can work with any ERC20 token.
- Symbol: Customizable
- Decimals: 18
- Initial Supply: Customizable
- constructor: Sets the deployer as the owner and mints the initial supply to the owner.
- Deploy the ERC20 token contract.
- The deployer will receive the initial supply of tokens.
OrderBasedSwap is a contract that allows users to create and fulfill token swap orders. Users can deposit tokens and specify the tokens they want in return.
- Order Structure: Contains depositor, tokenIn, amountIn, tokenOut, amountOut, and fulfilled status.
- Events:
OrderCreated
andOrderFulfilled
.
- createOrder: Allows users to create a new swap order.
- fulfillOrder: Allows users to fulfill an existing swap order.
- getOrder: Returns the details of a specific order.
- Deploy the OrderBasedSwap contract with the addresses of the tokens that will be used for swapping.
- Transfer an initial supply of tokens to the OrderBasedSwap contract.
- Users can create orders by calling the
createOrder
function and specifying the tokens and amounts. - Other users can fulfill orders by calling the
fulfillOrder
function.
The tests for the contracts are written in TypeScript using the Hardhat framework. The tests cover deployment, order creation, order fulfillment, and edge cases.
- Deployment: Ensures that the contracts are deployed properly and the initial supply of tokens is transferred to the OrderBasedSwap contract.
- Order Creation: Tests the creation of swap orders.
- Order Fulfillment: Tests the fulfillment of swap orders.
- Get Order: Tests retrieving order details.
- Revert on Fulfilled Order: Ensures that fulfilling an already fulfilled order reverts.
- Install dependencies:
npm install
- Run tests:
npx hardhat test
import { expect } from "chai";
import { ethers } from "hardhat";
describe("OrderBasedSwap", function () {
let orderBasedSwap: OrderBasedSwap;
beforeEach(async function () {
const [deployer] = await ethers.getSigners();
orderBasedSwap = await new OrderBasedSwap(deployer.address).deploy();
});
describe("Deployment", function () {
it("Should set the right owner", async function () {
expect(await orderBasedSwap.owner()).to.equal(deployer.address);
});
});
describe("Order Creation", function () {
it("Should create an order", async function () {
await orderBasedSwap.createOrder("0x123", "0x456", 100, "0x789", 200);
const order = await orderBasedSwap.getOrder(0);
expect(order.depositor).to.equal(deployer.address);
expect(order.tokenIn).to.equal("0x123");
expect(order.amountIn).to.equal(100);
expect(order.tokenOut).to.equal("0x789");
expect(order.amountOut).to.equal(200);
expect(order.fulfilled).to.be.false;
});
});
describe("Order Fulfillment", function () {
it("Should fulfill an order", async function () {
await orderBasedSwap.createOrder("0x123", "0x456", 100, "0x789", 200);
await orderBasedSwap.fulfillOrder(0, deployer.address);
const order = await orderBasedSwap.getOrder(0);
expect(order.fulfilled).to.be.true;
});
});
});