dsa-connectors/test/mainnet/kyberswap/kyberswap.test.ts

168 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-06-13 17:24:06 +00:00
import hre from "hardhat";
import { expect } from "chai";
import { abis } from "../../../scripts/constant/abis";
import { addresses } from "../../../scripts/tests/mainnet/addresses";
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
import {
2023-06-14 14:36:35 +00:00
ConnectV2KyberAggregator__factory,
2023-06-13 17:24:06 +00:00
} from "../../../typechain"
import { parseEther } from "@ethersproject/units";
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
import { tokens } from "../../../scripts/tests/mainnet/tokens";
import { constants } from "../../../scripts/constant/constant";
import { addLiquidity } from "../../../scripts/tests/addLiquidity";
import BigNumber from "bignumber.js";
import axios from "axios";
const { web3, deployments, waffle, ethers } = hre;
const { provider, deployContract } = waffle;
describe("Kyberswap", function() {
const connectorName = "kyberswap-test";
let dsaWallet0: any;
let masterSigner: any;
let instaConnectorsV2: any;
let connector: any;
const wallets = provider.getWallets();
const [wallet0, wallet1, wallet2, wallet3] = wallets;
before(async () => {
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
// @ts-ignore
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
// blockNumber: 13300000,
},
},
],
});
masterSigner = await getMasterSigner();
const erc20 = abis.basic.erc20;
instaConnectorsV2 = await ethers.getContractAt(
abis.core.connectorsV2,
addresses.core.connectorsV2
);
connector = await deployAndEnableConnector({
connectorName,
2023-06-14 14:36:35 +00:00
contractArtifact: ConnectV2KyberAggregator__factory,
2023-06-13 17:24:06 +00:00
signer: masterSigner,
connectors: instaConnectorsV2,
});
// console.log("Connector address", connector.address);
});
it("Should have contracts deployed.", async function() {
expect(!!instaConnectorsV2.address).to.be.true;
expect(!!connector.address).to.be.true;
expect(!!masterSigner.address).to.be.true;
});
describe("DSA wallet setup", function() {
it("Should build DSA v2", async function() {
dsaWallet0 = await buildDSAv2(wallet0.address);
expect(!!dsaWallet0.address).to.be.true;
});
it("Deposit ETH into DSA wallet", async function() {
await wallet0.sendTransaction({
to: dsaWallet0.address,
value: ethers.utils.parseEther("10"),
});
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
ethers.utils.parseEther("10")
);
});
});
2023-06-14 14:36:35 +00:00
describe("Main", async function() {
2023-06-13 17:24:06 +00:00
it("should swap successfully", async function() {
2023-06-14 14:36:35 +00:00
const latestBlock = await hre.ethers.provider.getBlock("latest");
console.log('latestBlock: ', latestBlock.number)
2023-06-13 17:24:06 +00:00
async function getArg() {
const slippage = 1;
/* eth -> USDT */
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18
const sellTokenDecimals = 18;
const buyTokenAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7"; // USDT, decimals 6
const buyTokenDecimals = 6;
const amount = 2;
const srcAmount = new BigNumber(amount)
.times(new BigNumber(10).pow(sellTokenDecimals))
.toFixed(0);
const fromAddress = dsaWallet0.address;
2023-06-14 12:29:41 +00:00
let url = `https://aggregator-api.kyberswap.com/ethereum/api/v1/routes`;
2023-06-13 17:24:06 +00:00
let params = {
2023-06-14 12:29:41 +00:00
tokenIn: sellTokenAddress,
tokenOut: buyTokenAddress,
amountIn: srcAmount,
2023-06-13 17:24:06 +00:00
};
2023-06-14 12:29:41 +00:00
const routeSummary = await axios
2023-06-13 17:24:06 +00:00
.get(url, { params: params })
2023-06-14 14:36:35 +00:00
.then((response) => response.data.data.routeSummary);
2023-06-13 17:24:06 +00:00
let txConfig = {
2023-06-14 12:29:41 +00:00
routeSummary: routeSummary,
sender: fromAddress,
recipient: fromAddress,
2023-06-13 17:24:06 +00:00
};
2023-06-14 14:36:35 +00:00
2023-06-14 12:29:41 +00:00
let url2 = "https://aggregator-api.kyberswap.com/ethereum/api/v1/route/build";
2023-06-13 17:24:06 +00:00
const calldata = await axios
.post(url2, txConfig)
2023-06-14 14:36:35 +00:00
.then((response) => response.data.data);
2023-06-13 17:24:06 +00:00
function caculateUnitAmt(
buyAmount: any,
sellAmount: any,
buyDecimal: any,
sellDecimal: any,
maxSlippage: any
) {
let unitAmt: any;
unitAmt = new BigNumber(buyAmount)
.dividedBy(10 ** buyDecimal)
.dividedBy(new BigNumber(sellAmount).dividedBy(10 ** sellDecimal));
unitAmt = unitAmt.multipliedBy((100 - maxSlippage) / 100);
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
return unitAmt;
}
let unitAmt = caculateUnitAmt(
2023-06-14 12:29:41 +00:00
routeSummary.amountOut,
2023-06-13 17:24:06 +00:00
srcAmount,
buyTokenDecimals,
sellTokenDecimals,
1
);
2023-06-14 14:36:35 +00:00
console.log('unitAmt: ', unitAmt.toString())
2023-06-13 17:24:06 +00:00
return [
buyTokenAddress,
sellTokenAddress,
srcAmount,
unitAmt,
2023-06-14 14:36:35 +00:00
calldata.data,
2023-06-13 17:24:06 +00:00
0,
];
}
2023-06-14 14:36:35 +00:00
2023-06-13 17:24:06 +00:00
let arg = await getArg();
2023-06-14 14:36:35 +00:00
2023-06-13 17:24:06 +00:00
const spells = [
{
connector: connectorName,
method: "swap",
args: arg,
},
];
const tx = await dsaWallet0
.connect(wallet0)
.cast(...encodeSpells(spells), wallet1.address);
const receipt = await tx.wait();
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
ethers.utils.parseEther("8")
);
});
});
});