85 lines
3.8 KiB
TypeScript
85 lines
3.8 KiB
TypeScript
import { expect } from "chai";
|
|
import { ethers } from "hardhat";
|
|
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";
|
|
|
|
describe("AyrisSplitter", function () {
|
|
async function deploySplitterFixture() {
|
|
const [owner, platformAccount, merchantAccount, customerAccount] = await ethers.getSigners();
|
|
|
|
const Splitter = await ethers.getContractFactory("AyrisSplitter");
|
|
const splitter = await Splitter.deploy(platformAccount.address);
|
|
|
|
const MockERC20 = await ethers.getContractFactory("MockERC20");
|
|
const mockToken = await MockERC20.deploy("Tether USD", "USDT");
|
|
|
|
// Mint 1000 USDT to customer
|
|
await mockToken.mint(customerAccount.address, ethers.parseUnits("1000", 18));
|
|
|
|
return { splitter, mockToken, owner, platformAccount, merchantAccount, customerAccount };
|
|
}
|
|
|
|
describe("Deployment", function () {
|
|
it("Should set the right platform address", async function () {
|
|
const { splitter, platformAccount } = await loadFixture(deploySplitterFixture);
|
|
expect(await splitter.platformAddress()).to.equal(platformAccount.address);
|
|
});
|
|
});
|
|
|
|
describe("Native Payments (ETH/MATIC)", function () {
|
|
it("Should split native currency exactly 99% to merchant and 1% to platform", async function () {
|
|
const { splitter, merchantAccount, platformAccount, customerAccount } = await loadFixture(deploySplitterFixture);
|
|
|
|
const paymentAmount = ethers.parseEther("1.0"); // 1 ETH/MATIC
|
|
|
|
const initialMerchantBalance = await ethers.provider.getBalance(merchantAccount.address);
|
|
const initialPlatformBalance = await ethers.provider.getBalance(platformAccount.address);
|
|
|
|
// Customer sends 1 ETH to contract using payNative
|
|
await splitter.connect(customerAccount).payNative(merchantAccount.address, { value: paymentAmount });
|
|
|
|
const finalMerchantBalance = await ethers.provider.getBalance(merchantAccount.address);
|
|
const finalPlatformBalance = await ethers.provider.getBalance(platformAccount.address);
|
|
|
|
// 1 ETH * 1% = 0.01 ETH
|
|
const expectedPlatformShare = ethers.parseEther("0.01");
|
|
// 1 ETH * 99% = 0.99 ETH
|
|
const expectedMerchantShare = ethers.parseEther("0.99");
|
|
|
|
expect(finalMerchantBalance - initialMerchantBalance).to.equal(expectedMerchantShare);
|
|
expect(finalPlatformBalance - initialPlatformBalance).to.equal(expectedPlatformShare);
|
|
});
|
|
});
|
|
|
|
describe("Token Payments (USDT/USDC)", function () {
|
|
it("Should split ERC20 tokens exactly 99% to merchant and 1% to platform", async function () {
|
|
const { splitter, mockToken, merchantAccount, platformAccount, customerAccount } = await loadFixture(deploySplitterFixture);
|
|
|
|
const paymentAmount = ethers.parseUnits("100", 18); // 100 USDT
|
|
|
|
// Customer must approve the splitter contract first
|
|
await mockToken.connect(customerAccount).approve(await splitter.getAddress(), paymentAmount);
|
|
|
|
const initialMerchantBalance = await mockToken.balanceOf(merchantAccount.address);
|
|
const initialPlatformBalance = await mockToken.balanceOf(platformAccount.address);
|
|
|
|
// Customer executes payment
|
|
await splitter.connect(customerAccount).payToken(
|
|
await mockToken.getAddress(),
|
|
paymentAmount,
|
|
merchantAccount.address
|
|
);
|
|
|
|
const finalMerchantBalance = await mockToken.balanceOf(merchantAccount.address);
|
|
const finalPlatformBalance = await mockToken.balanceOf(platformAccount.address);
|
|
|
|
// 100 USDT * 1% = 1 USDT
|
|
const expectedPlatformShare = ethers.parseUnits("1", 18);
|
|
// 100 USDT * 99% = 99 USDT
|
|
const expectedMerchantShare = ethers.parseUnits("99", 18);
|
|
|
|
expect(finalMerchantBalance - initialMerchantBalance).to.equal(expectedMerchantShare);
|
|
expect(finalPlatformBalance - initialPlatformBalance).to.equal(expectedPlatformShare);
|
|
});
|
|
});
|
|
});
|