Merge pull request #164 from Instadapp/1inch-Avalanche

[Avalanche] Created 1inch connector
This commit is contained in:
Thrilok kumar 2022-01-21 01:36:35 +05:30 committed by GitHub
commit fcb4cfb1ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 326 additions and 1 deletions

View File

@ -0,0 +1,12 @@
pragma solidity ^0.7.0;
contract Events {
event LogSell(
address indexed buyToken,
address indexed sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 getId,
uint256 setId
);
}

View File

@ -0,0 +1,13 @@
pragma solidity ^0.7.0;
import { TokenInterface } from "../../common/interfaces.sol";
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
abstract contract Helpers is DSMath, Basic {
/**
* @dev 1Inch Address
*/
address internal constant oneInchAddr = 0x1111111254fb6c44bAC0beD2854e76F90643097d;
}

View File

@ -0,0 +1,30 @@
pragma solidity ^0.7.0;
import { TokenInterface } from "../../common/interfaces.sol";
interface OneInchInterace {
function swap(
TokenInterface fromToken,
TokenInterface toToken,
uint256 fromTokenAmount,
uint256 minReturnAmount,
uint256 guaranteedAmount,
address payable referrer,
address[] calldata callAddresses,
bytes calldata callDataConcat,
uint256[] calldata starts,
uint256[] calldata gasLimitsAndValues
)
external
payable
returns (uint256 returnAmount);
}
struct OneInchData {
TokenInterface sellToken;
TokenInterface buyToken;
uint _sellAmt;
uint _buyAmt;
uint unitAmt;
bytes callData;
}

View File

@ -0,0 +1,111 @@
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/**
* @title 1InchV4.
* @dev On-chain DEX Aggregator.
*/
// import files from common directory
import { TokenInterface , MemoryInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { OneInchInterace, OneInchData } from "./interface.sol";
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract OneInchResolver is Helpers, Events {
/**
* @dev 1inch API swap handler
* @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol
* @param avaxAmt - Avax to swap for .value()
*/
function oneInchSwap(
OneInchData memory oneInchData,
uint avaxAmt
) internal returns (uint buyAmt) {
TokenInterface buyToken = oneInchData.buyToken;
(uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken);
uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt);
uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18));
uint initalBal = getTokenBal(buyToken);
// solium-disable-next-line security/no-call-value
(bool success, ) = oneInchAddr.call{value: avaxAmt}(oneInchData.callData);
if (!success) revert("1Inch-swap-failed");
uint finalBal = getTokenBal(buyToken);
buyAmt = sub(finalBal, initalBal);
require(_slippageAmt <= buyAmt, "Too much slippage");
}
}
abstract contract OneInchResolverHelpers is OneInchResolver {
/**
* @dev Gets the swapping data from 1inch's API.
* @param oneInchData Struct with multiple swap data defined in interfaces.sol
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function _sell(
OneInchData memory oneInchData,
uint setId
) internal returns (OneInchData memory) {
TokenInterface _sellAddr = oneInchData.sellToken;
uint avaxAmt;
if (address(_sellAddr) == avaxAddr) {
avaxAmt = oneInchData._sellAmt;
} else {
approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt);
}
oneInchData._buyAmt = oneInchSwap(oneInchData, avaxAmt);
setUint(setId, oneInchData._buyAmt);
return oneInchData;
}
}
abstract contract OneInch is OneInchResolverHelpers {
/**
* @dev Sell Avax/ERC20_Token using 1Inch.
* @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain.
* @param buyAddr The address of the token to buy.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr The address of the token to sell.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt The amount of the token to sell.
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
* @param callData Data from 1inch API.
* @param setId ID stores the amount of token brought.
*/
function sell(
address buyAddr,
address sellAddr,
uint sellAmt,
uint unitAmt,
bytes calldata callData,
uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) {
OneInchData memory oneInchData = OneInchData({
buyToken: TokenInterface(buyAddr),
sellToken: TokenInterface(sellAddr),
unitAmt: unitAmt,
callData: callData,
_sellAmt: sellAmt,
_buyAmt: 0
});
oneInchData = _sell(oneInchData, setId);
_eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId);
}
}
contract ConnectV2OneInchV4Avalanche is OneInch {
string public name = "1Inch-v4";
}

View File

@ -109,7 +109,6 @@ const config: HardhatUserConfig = {
forking: {
url: String(getNetworkUrl(String(process.env.networkType))),
},
gasPrice: 25000000000,
},
mainnet: createConfig("mainnet"),
polygon: createConfig("polygon"),

View File

@ -0,0 +1,160 @@
import hre from "hardhat";
import axios from "axios";
import { expect } from "chai";
const { ethers } = hre; //check
import { BigNumber } from "bignumber.js";
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
import { addresses } from "../../../scripts/tests/avalanche/addresses";
import { abis } from "../../../scripts/constant/abis";
import {ConnectV2OneInchV4Avalanche__factory } from "../../../typechain";
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
import type { Signer, Contract } from "ethers";
describe("1Inch", function() {
const connectorName = "1inch-connector-test";
let dsaWallet0: Contract;
let wallet0: Signer, wallet1: Signer;
let masterSigner: Signer;
let instaConnectorsV2: Contract;
let connector: Contract;
before(async () => {
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
// @ts-ignore
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
},
},
],
});
[wallet0, wallet1] = await ethers.getSigners();
masterSigner = await getMasterSigner();
instaConnectorsV2 = await ethers.getContractAt(
abis.core.connectorsV2,
addresses.core.connectorsV2
);
connector = await deployAndEnableConnector({
connectorName,
contractArtifact: ConnectV2OneInchV4Avalanche__factory,
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(!!(await masterSigner.getAddress())).to.be.true;
});
describe("DSA wallet setup", function() {
it("Should build DSA v2", async function() {
dsaWallet0 = await buildDSAv2(await wallet0.getAddress());
expect(!!dsaWallet0.address).to.be.true;
});
it("Deposit avax 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")
);
});
});
describe("Main", function() {
it("should swap the tokens", async function() {
let buyTokenAmount: any;
async function getArg() {
// const slippage = 0.5;
/* avax -> dai */
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // avax, decimals 18
const sellTokenDecimals = 18;
const buyTokenAddress = "0xd586e7f844cea2f87f50152665bcbc2c279d8d70"; // dai, decimals 18
const buyTokenDecimals = 18;
const amount = 1;
const srcAmount = new BigNumber(amount)
.times(new BigNumber(10).pow(sellTokenDecimals))
.toFixed(0);
let url = `https://api.1inch.exchange/v4.0/43114/swap`;
const params = {
toTokenAddress: buyTokenAddress,
fromTokenAddress: sellTokenAddress,
amount: "1000000000000000000", // Always denominated in wei
fromAddress: dsaWallet0.address,
slippage:1
};
const response = await axios
.get(url, { params: params })
.then((data: any) => data);
buyTokenAmount = response.data.toTokenAmount;
const calldata = response.data.tx.data;
let caculateUnitAmt = () => {
const buyTokenAmountRes = new BigNumber(buyTokenAmount)
.dividedBy(new BigNumber(10).pow(buyTokenDecimals))
.toFixed(8);
let unitAmt: any = new BigNumber(buyTokenAmountRes).dividedBy(
new BigNumber(amount)
);
unitAmt = unitAmt.multipliedBy((100 - 0.3) / 100);
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
return unitAmt;
};
let unitAmt = caculateUnitAmt();
return [
buyTokenAddress,
sellTokenAddress,
srcAmount,
unitAmt,
calldata,
0,
];
}
let arg = await getArg();
const spells = [
{
connector: connectorName,
method: "sell",
args: arg,
},
];
const tx = await dsaWallet0
.connect(wallet0)
.cast(...encodeSpells(spells), await wallet1.getAddress());
const receipt = await tx.wait();
const daiToken = await ethers.getContractAt(
er20abi,
"0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063" // dai address
);
expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte(
buyTokenAmount
);
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
ethers.utils.parseEther("9")
);
});
});
});