mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
commit
bcc4862d70
12
contracts/avalanche/connectors/0x/events.sol
Normal file
12
contracts/avalanche/connectors/0x/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
65
contracts/avalanche/connectors/0x/helpers.sol
Normal file
65
contracts/avalanche/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,65 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev 0x Address
|
||||
*/
|
||||
address internal constant zeroExAddr =
|
||||
0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
|
||||
|
||||
function _swapHelper(ZeroExData memory zeroExData, uint256 avaxAmt)
|
||||
internal
|
||||
returns (uint256 buyAmt)
|
||||
{
|
||||
TokenInterface buyToken = zeroExData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||
buyToken,
|
||||
zeroExData.sellToken
|
||||
);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, zeroExData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(
|
||||
_buyDec,
|
||||
wmul(zeroExData.unitAmt, _sellAmt18)
|
||||
);
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
// solium-disable-next-line security/no-call-value
|
||||
(bool success, ) = zeroExAddr.call{value: avaxAmt}(zeroExData.callData);
|
||||
if (!success) revert("0x-swap-failed");
|
||||
|
||||
uint256 finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(ZeroExData memory zeroExData, uint256 setId)
|
||||
internal
|
||||
returns (ZeroExData memory)
|
||||
{
|
||||
TokenInterface _sellAddr = zeroExData.sellToken;
|
||||
|
||||
uint256 avaxAmt;
|
||||
if (address(_sellAddr) == avaxAddr) {
|
||||
avaxAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, avaxAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
12
contracts/avalanche/connectors/0x/interface.sol
Normal file
12
contracts/avalanche/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
64
contracts/avalanche/connectors/0x/main.sol
Normal file
64
contracts/avalanche/connectors/0x/main.sol
Normal file
|
@ -0,0 +1,64 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title 0x.
|
||||
* @dev On-chain DEX Aggregator.
|
||||
*/
|
||||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract ZeroEx is Helpers {
|
||||
/**
|
||||
* @notice Swap tokens on 0x
|
||||
* @dev Sell Avax/ERC20_Token using 0x.
|
||||
* @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 0x API.
|
||||
* @param setId ID stores the amount of token brought.
|
||||
*/
|
||||
|
||||
function swap(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256 unitAmt,
|
||||
bytes calldata callData,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
ZeroExData memory zeroExData = ZeroExData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
zeroExData = _swap(zeroExData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
zeroExData._buyAmt,
|
||||
zeroExData._sellAmt,
|
||||
0,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2ZeroExAvalanche is ZeroEx {
|
||||
string public name = "0x-V4";
|
||||
}
|
12
contracts/mainnet/connectors/0x/events.sol
Normal file
12
contracts/mainnet/connectors/0x/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
65
contracts/mainnet/connectors/0x/helpers.sol
Normal file
65
contracts/mainnet/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,65 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev 0x Address
|
||||
*/
|
||||
address internal constant zeroExAddr =
|
||||
0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
|
||||
|
||||
function _swapHelper(ZeroExData memory zeroExData, uint256 ethAmt)
|
||||
internal
|
||||
returns (uint256 buyAmt)
|
||||
{
|
||||
TokenInterface buyToken = zeroExData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||
buyToken,
|
||||
zeroExData.sellToken
|
||||
);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, zeroExData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(
|
||||
_buyDec,
|
||||
wmul(zeroExData.unitAmt, _sellAmt18)
|
||||
);
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
// solium-disable-next-line security/no-call-value
|
||||
(bool success, ) = zeroExAddr.call{value: ethAmt}(zeroExData.callData);
|
||||
if (!success) revert("0x-swap-failed");
|
||||
|
||||
uint256 finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(ZeroExData memory zeroExData, uint256 setId)
|
||||
internal
|
||||
returns (ZeroExData memory)
|
||||
{
|
||||
TokenInterface _sellAddr = zeroExData.sellToken;
|
||||
|
||||
uint256 ethAmt;
|
||||
if (address(_sellAddr) == ethAddr) {
|
||||
ethAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, ethAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
12
contracts/mainnet/connectors/0x/interface.sol
Normal file
12
contracts/mainnet/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
64
contracts/mainnet/connectors/0x/main.sol
Normal file
64
contracts/mainnet/connectors/0x/main.sol
Normal file
|
@ -0,0 +1,64 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title 0x.
|
||||
* @dev On-chain DEX Aggregator.
|
||||
*/
|
||||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract ZeroEx is Helpers {
|
||||
/**
|
||||
* @notice Swap tokens on 0x
|
||||
* @dev Sell ETH/ERC20_Token using 0x.
|
||||
* @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAmt The amount of the token to sell.
|
||||
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
|
||||
* @param callData Data from 0x API.
|
||||
* @param setId ID stores the amount of token brought.
|
||||
*/
|
||||
|
||||
function swap(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256 unitAmt,
|
||||
bytes calldata callData,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
ZeroExData memory zeroExData = ZeroExData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
zeroExData = _swap(zeroExData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
zeroExData._buyAmt,
|
||||
zeroExData._sellAmt,
|
||||
0,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2ZeroEx is ZeroEx {
|
||||
string public name = "0x-V4";
|
||||
}
|
12
contracts/polygon/connectors/0x/events.sol
Normal file
12
contracts/polygon/connectors/0x/events.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
contract Events {
|
||||
event LogSwap(
|
||||
address indexed buyToken,
|
||||
address indexed sellToken,
|
||||
uint256 buyAmt,
|
||||
uint256 sellAmt,
|
||||
uint256 getId,
|
||||
uint256 setId
|
||||
);
|
||||
}
|
67
contracts/polygon/connectors/0x/helpers.sol
Normal file
67
contracts/polygon/connectors/0x/helpers.sol
Normal file
|
@ -0,0 +1,67 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
* @dev 0x Address
|
||||
*/
|
||||
address internal constant zeroExAddr =
|
||||
0xDef1C0ded9bec7F1a1670819833240f027b25EfF;
|
||||
|
||||
function _swapHelper(ZeroExData memory zeroExData, uint256 maticAmt)
|
||||
internal
|
||||
returns (uint256 buyAmt)
|
||||
{
|
||||
TokenInterface buyToken = zeroExData.buyToken;
|
||||
(uint256 _buyDec, uint256 _sellDec) = getTokensDec(
|
||||
buyToken,
|
||||
zeroExData.sellToken
|
||||
);
|
||||
uint256 _sellAmt18 = convertTo18(_sellDec, zeroExData._sellAmt);
|
||||
uint256 _slippageAmt = convert18ToDec(
|
||||
_buyDec,
|
||||
wmul(zeroExData.unitAmt, _sellAmt18)
|
||||
);
|
||||
|
||||
uint256 initalBal = getTokenBal(buyToken);
|
||||
|
||||
// solium-disable-next-line security/no-call-value
|
||||
(bool success, ) = zeroExAddr.call{value: maticAmt}(
|
||||
zeroExData.callData
|
||||
);
|
||||
if (!success) revert("0x-swap-failed");
|
||||
|
||||
uint256 finalBal = getTokenBal(buyToken);
|
||||
|
||||
buyAmt = sub(finalBal, initalBal);
|
||||
|
||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||
}
|
||||
|
||||
function _swap(ZeroExData memory zeroExData, uint256 setId)
|
||||
internal
|
||||
returns (ZeroExData memory)
|
||||
{
|
||||
TokenInterface _sellAddr = zeroExData.sellToken;
|
||||
|
||||
uint256 maticAmt;
|
||||
if (address(_sellAddr) == maticAddr) {
|
||||
maticAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
||||
zeroExData._buyAmt = _swapHelper(zeroExData, maticAmt);
|
||||
setUint(setId, zeroExData._buyAmt);
|
||||
|
||||
return zeroExData;
|
||||
}
|
||||
}
|
12
contracts/polygon/connectors/0x/interface.sol
Normal file
12
contracts/polygon/connectors/0x/interface.sol
Normal file
|
@ -0,0 +1,12 @@
|
|||
pragma solidity ^0.7.0;
|
||||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
uint256 _sellAmt;
|
||||
uint256 _buyAmt;
|
||||
uint256 unitAmt;
|
||||
bytes callData;
|
||||
}
|
64
contracts/polygon/connectors/0x/main.sol
Normal file
64
contracts/polygon/connectors/0x/main.sol
Normal file
|
@ -0,0 +1,64 @@
|
|||
pragma solidity ^0.7.0;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title 0x.
|
||||
* @dev On-chain DEX Aggregator.
|
||||
*/
|
||||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
abstract contract ZeroEx is Helpers {
|
||||
/**
|
||||
* @notice Swap tokens on 0x
|
||||
* @dev Sell matic/ERC20_Token using 0x.
|
||||
* @param buyAddr The address of the token to buy.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAddr The address of the token to sell.(For matic: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||
* @param sellAmt The amount of the token to sell.
|
||||
* @param unitAmt The amount of buyAmt/sellAmt with slippage.
|
||||
* @param callData Data from 0x API.
|
||||
* @param setId ID stores the amount of token brought.
|
||||
*/
|
||||
|
||||
function swap(
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256 unitAmt,
|
||||
bytes calldata callData,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
payable
|
||||
returns (string memory _eventName, bytes memory _eventParam)
|
||||
{
|
||||
ZeroExData memory zeroExData = ZeroExData({
|
||||
buyToken: TokenInterface(buyAddr),
|
||||
sellToken: TokenInterface(sellAddr),
|
||||
unitAmt: unitAmt,
|
||||
callData: callData,
|
||||
_sellAmt: sellAmt,
|
||||
_buyAmt: 0
|
||||
});
|
||||
|
||||
zeroExData = _swap(zeroExData, setId);
|
||||
|
||||
_eventName = "LogSwap(address,address,uint256,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
zeroExData._buyAmt,
|
||||
zeroExData._sellAmt,
|
||||
0,
|
||||
setId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
contract ConnectV2ZeroExPolygon is ZeroEx {
|
||||
string public name = "0x-V4";
|
||||
}
|
|
@ -105,6 +105,7 @@ const config: HardhatUserConfig = {
|
|||
forking: {
|
||||
url: String(getNetworkUrl(String(process.env.networkType))),
|
||||
},
|
||||
gasPrice: 25000000000,
|
||||
},
|
||||
mainnet: createConfig("mainnet"),
|
||||
polygon: createConfig("polygon"),
|
||||
|
@ -117,8 +118,8 @@ const config: HardhatUserConfig = {
|
|||
sources: "./contracts",
|
||||
tests: "./test",
|
||||
},
|
||||
etherscan: {
|
||||
apiKey: getScanApiKey(getNetworkUrl(String(process.env.networkType))),
|
||||
etherscan: {
|
||||
apiKey: getScanApiKey(String(process.env.networkType)),
|
||||
},
|
||||
typechain: {
|
||||
outDir: "typechain",
|
||||
|
|
|
@ -23,6 +23,7 @@ async function main() {
|
|||
"Uniswap-V3-Staker-v1.1" : "ConnectV2UniswapV3StakerPolygon",
|
||||
"Paraswap-v5" : "ConnectV2ParaswapV5Polygon"
|
||||
"1INCH-V4" : "ConnectV2OneInchV4Polygon",
|
||||
"ZEROEX-A": "ConnectV2ZeroExAvalanche",
|
||||
};
|
||||
|
||||
const addressMapping: Record<string, string> = {};
|
||||
|
|
|
@ -8,15 +8,14 @@ export const deployConnector = async (connectorName: string) => {
|
|||
console.log(`${connectorName} Deployed: ${connector.address}`);
|
||||
|
||||
try {
|
||||
await hre.run("verify:verify", {
|
||||
address: connector.address,
|
||||
constructorArguments: []
|
||||
}
|
||||
)
|
||||
} catch (error) {
|
||||
console.log(`Failed to verify: ${connectorName}@${connector.address}`)
|
||||
console.log(error)
|
||||
console.log()
|
||||
}
|
||||
await hre.run("verify:verify", {
|
||||
address: connector.address,
|
||||
constructorArguments: [],
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Failed to verify: ${connectorName}@${connector.address}`);
|
||||
console.log(error);
|
||||
console.log();
|
||||
}
|
||||
return connector.address;
|
||||
};
|
||||
|
|
|
@ -5,7 +5,7 @@ export const addresses: Record<string, any> = {
|
|||
// "INSTAPOOL-A": "0x8f1e38c53af7bD2b2bE01b9580911b7Cca504F1b",
|
||||
},
|
||||
core: {
|
||||
connectorsV2: "0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B",
|
||||
instaIndex: "0x1eE00C305C51Ff3bE60162456A9B533C07cD9288",
|
||||
connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14",
|
||||
instaIndex: "0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25",
|
||||
},
|
||||
};
|
||||
|
|
159
test/avalanche/0x/zeroEx.test.ts
Normal file
159
test/avalanche/0x/zeroEx.test.ts
Normal file
|
@ -0,0 +1,159 @@
|
|||
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 {ConnectV2ZeroExAvalanche__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
describe("ZeroEx", function() {
|
||||
const connectorName = "zeroEx-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: ConnectV2ZeroExAvalanche__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 matic 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;
|
||||
/* matic -> dai */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // matic, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; // 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://avalanche.api.0x.org/swap/v1/quote`;
|
||||
|
||||
const params = {
|
||||
buyToken: "DAI",
|
||||
sellToken: "AVAX",
|
||||
sellAmount: "1000000000000000000", // Always denominated in wei
|
||||
};
|
||||
const response = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data: any) => data);
|
||||
|
||||
console.log(response);
|
||||
buyTokenAmount = response.data.buyAmount;
|
||||
const calldata = response.data.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: "swap",
|
||||
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")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
161
test/mainnet/0x/zeroEx.test.ts
Normal file
161
test/mainnet/0x/zeroEx.test.ts
Normal file
|
@ -0,0 +1,161 @@
|
|||
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/mainnet/addresses";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import { ConnectV2ZeroEx, ConnectV2ZeroEx__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
describe("ZeroEx", function() {
|
||||
const connectorName = "zeroEx-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: ConnectV2ZeroEx__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 ETH into DSA wallet", async function() {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10"),
|
||||
});
|
||||
// console.log(dsaWallet0.address);
|
||||
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;
|
||||
/* Eth -> dai */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // eth, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; // dai, decimals 18
|
||||
const buyTokenDecimals = 18;
|
||||
const amount = 1;
|
||||
|
||||
const srcAmount = new BigNumber(amount)
|
||||
.times(new BigNumber(10).pow(sellTokenDecimals))
|
||||
.toFixed(0);
|
||||
|
||||
const fromAddress = dsaWallet0.address;
|
||||
|
||||
let url = `https://api.0x.org/swap/v1/quote`;
|
||||
|
||||
const params = {
|
||||
buyToken: "DAI",
|
||||
sellToken: "ETH",
|
||||
sellAmount: "1000000000000000000", // Always denominated in wei
|
||||
};
|
||||
|
||||
const response = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data: any) => data);
|
||||
|
||||
buyTokenAmount = response.data.buyAmount;
|
||||
const calldata = response.data.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: "swap",
|
||||
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,
|
||||
"0x6b175474e89094c44da98b954eedeac495271d0f" // 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")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
159
test/polygon/0x/zeroEx.test.ts
Normal file
159
test/polygon/0x/zeroEx.test.ts
Normal file
|
@ -0,0 +1,159 @@
|
|||
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/polygon/addresses";
|
||||
import { abis } from "../../../scripts/constant/abis";
|
||||
import {ConnectV2ZeroExPolygon__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
describe("ZeroEx", function() {
|
||||
const connectorName = "zeroEx-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: ConnectV2ZeroExPolygon__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 matic 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;
|
||||
/* matic -> dai */
|
||||
const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // matic, decimals 18
|
||||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; // 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://polygon.api.0x.org/swap/v1/quote`;
|
||||
|
||||
const params = {
|
||||
buyToken: "DAI",
|
||||
sellToken: "MATIC",
|
||||
sellAmount: "1000000000000000000", // Always denominated in wei
|
||||
};
|
||||
|
||||
const response = await axios
|
||||
.get(url, { params: params })
|
||||
.then((data: any) => data);
|
||||
|
||||
buyTokenAmount = response.data.buyAmount;
|
||||
const calldata = response.data.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: "swap",
|
||||
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")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user