mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
updated-contract
This commit is contained in:
parent
043e873aaa
commit
612980c52b
|
@ -3,7 +3,7 @@ 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, zeroExInterface} from "./interface.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
|
@ -50,12 +50,9 @@ contract Helpers is DSMath, Basic {
|
|||
if (address(_sellAddr) == avaxAddr) {
|
||||
avaxAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
address transformWallet = address(
|
||||
zeroExInterface(zeroExAddr).getTransformWallet()
|
||||
);
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
transformWallet,
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,40 +2,6 @@ pragma solidity ^0.7.0;
|
|||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
interface zeroExInterface {
|
||||
function getTransformWallet() external view returns (IFlashWallet wallet);
|
||||
}
|
||||
|
||||
interface IFlashWallet {
|
||||
/// @dev Execute an arbitrary call. Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @param value Ether to attach to the call.
|
||||
/// @return resultData The data returned by the call.
|
||||
function executeCall(
|
||||
address payable target,
|
||||
bytes calldata callData,
|
||||
uint256 value
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Execute an arbitrary delegatecall, in the context of this puppet.
|
||||
/// Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @return resultData The data returned by the call.
|
||||
function executeDelegateCall(
|
||||
address payable target,
|
||||
bytes calldata callData
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Allows the puppet to receive ETH.
|
||||
receive() external payable;
|
||||
|
||||
/// @dev Fetch the immutable owner/deployer of this contract.
|
||||
/// @return owner_ The immutable owner/deployer/
|
||||
function owner() external view returns (address owner_);
|
||||
}
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
|
|
|
@ -8,7 +8,7 @@ pragma experimental ABIEncoderV2;
|
|||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData, zeroExInterface} from "./interface.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ 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, zeroExInterface} from "./interface.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
/**
|
||||
|
@ -50,12 +50,9 @@ contract Helpers is DSMath, Basic {
|
|||
if (address(_sellAddr) == ethAddr) {
|
||||
ethAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
address transformWallet = address(
|
||||
zeroExInterface(zeroExAddr).getTransformWallet()
|
||||
);
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
transformWallet,
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,40 +2,6 @@ pragma solidity ^0.7.0;
|
|||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
interface zeroExInterface {
|
||||
function getTransformWallet() external view returns (IFlashWallet wallet);
|
||||
}
|
||||
|
||||
interface IFlashWallet {
|
||||
/// @dev Execute an arbitrary call. Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @param value Ether to attach to the call.
|
||||
/// @return resultData The data returned by the call.
|
||||
function executeCall(
|
||||
address payable target,
|
||||
bytes calldata callData,
|
||||
uint256 value
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Execute an arbitrary delegatecall, in the context of this puppet.
|
||||
/// Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @return resultData The data returned by the call.
|
||||
function executeDelegateCall(
|
||||
address payable target,
|
||||
bytes calldata callData
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Allows the puppet to receive ETH.
|
||||
receive() external payable;
|
||||
|
||||
/// @dev Fetch the immutable owner/deployer of this contract.
|
||||
/// @return owner_ The immutable owner/deployer/
|
||||
function owner() external view returns (address owner_);
|
||||
}
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
|
|
|
@ -8,7 +8,7 @@ pragma experimental ABIEncoderV2;
|
|||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData, zeroExInterface} from "./interface.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ pragma solidity ^0.7.0;
|
|||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
import {DSMath} from "../../common/math.sol";
|
||||
import {ZeroExData, zeroExInterface} from "./interface.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Basic} from "../../common/basic.sol";
|
||||
|
||||
contract Helpers is DSMath, Basic {
|
||||
|
@ -52,12 +52,9 @@ contract Helpers is DSMath, Basic {
|
|||
if (address(_sellAddr) == maticAddr) {
|
||||
maticAmt = zeroExData._sellAmt;
|
||||
} else {
|
||||
address transformWallet = address(
|
||||
zeroExInterface(zeroExAddr).getTransformWallet()
|
||||
);
|
||||
approve(
|
||||
TokenInterface(_sellAddr),
|
||||
transformWallet,
|
||||
zeroExAddr,
|
||||
zeroExData._sellAmt
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,43 +2,6 @@ pragma solidity ^0.7.0;
|
|||
|
||||
import {TokenInterface} from "../../common/interfaces.sol";
|
||||
|
||||
interface zeroExInterface {
|
||||
function getTransformWallet() external view returns (IFlashWallet wallet);
|
||||
}
|
||||
|
||||
interface IFlashWallet {
|
||||
/// @dev Execute an arbitrary call. Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @param value Ether to attach to the call.
|
||||
/// @return resultData The data returned by the call.
|
||||
|
||||
function executeCall(
|
||||
address payable target,
|
||||
bytes calldata callData,
|
||||
uint256 value
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Execute an arbitrary delegatecall, in the context of this puppet.
|
||||
/// Only an authority can call this.
|
||||
/// @param target The call target.
|
||||
/// @param callData The call data.
|
||||
/// @return resultData The data returned by the call.
|
||||
|
||||
function executeDelegateCall(
|
||||
address payable target,
|
||||
bytes calldata callData
|
||||
) external payable returns (bytes memory resultData);
|
||||
|
||||
/// @dev Allows the puppet to receive ETH.
|
||||
receive() external payable;
|
||||
|
||||
/// @dev Fetch the immutable owner/deployer of this contract.
|
||||
/// @return owner_ The immutable owner/deployer/
|
||||
|
||||
function owner() external view returns (address owner_);
|
||||
}
|
||||
|
||||
struct ZeroExData {
|
||||
TokenInterface sellToken;
|
||||
TokenInterface buyToken;
|
||||
|
|
|
@ -8,7 +8,7 @@ pragma experimental ABIEncoderV2;
|
|||
|
||||
import {TokenInterface, MemoryInterface} from "../../common/interfaces.sol";
|
||||
import {Stores} from "../../common/stores.sol";
|
||||
import {ZeroExData, zeroExInterface} from "./interface.sol";
|
||||
import {ZeroExData} from "./interface.sol";
|
||||
import {Helpers} from "./helpers.sol";
|
||||
import {Events} from "./events.sol";
|
||||
|
||||
|
|
|
@ -99,6 +99,7 @@ const config: HardhatUserConfig = {
|
|||
forking: {
|
||||
url: String(getNetworkUrl(String(process.env.networkType))),
|
||||
},
|
||||
gasPrice: 25000000000,
|
||||
},
|
||||
mainnet: createConfig("mainnet"),
|
||||
polygon: createConfig("polygon"),
|
||||
|
@ -112,7 +113,7 @@ const config: HardhatUserConfig = {
|
|||
tests: "./test",
|
||||
},
|
||||
etherscan: {
|
||||
apiKey: getScanApiKey(getNetworkUrl(String(process.env.networkType))),
|
||||
apiKey: getScanApiKey(String(process.env.networkType)),
|
||||
},
|
||||
typechain: {
|
||||
outDir: "typechain",
|
||||
|
|
|
@ -5,19 +5,20 @@ async function main() {
|
|||
const accounts = await ethers.getSigners();
|
||||
|
||||
const connectMapping: Record<string, string> = {
|
||||
"1INCH-A": "ConnectV2OneInch",
|
||||
"1INCH-B": "ConnectV2OneProto",
|
||||
"AAVE-V1-A": "ConnectV2AaveV1",
|
||||
"AAVE-V2-A": "ConnectV2AaveV2",
|
||||
"AUTHORITY-A": "ConnectV2Auth",
|
||||
"BASIC-A": "ConnectV2Basic",
|
||||
"COMP-A": "ConnectV2COMP",
|
||||
"COMPOUND-A": "ConnectV2Compound",
|
||||
"DYDX-A": "ConnectV2Dydx",
|
||||
"FEE-A": "ConnectV2Fee",
|
||||
"GELATO-A": "ConnectV2Gelato",
|
||||
"MAKERDAO-A": "ConnectV2Maker",
|
||||
"UNISWAP-A": "ConnectV2UniswapV2",
|
||||
// "1INCH-A": "ConnectV2OneInch",
|
||||
// "1INCH-B": "ConnectV2OneProto",
|
||||
// "AAVE-V1-A": "ConnectV2AaveV1",
|
||||
// "AAVE-V2-A": "ConnectV2AaveV2",
|
||||
// "AUTHORITY-A": "ConnectV2Auth",
|
||||
// "BASIC-A": "ConnectV2Basic",
|
||||
// "COMP-A": "ConnectV2COMP",
|
||||
// "COMPOUND-A": "ConnectV2Compound",
|
||||
// "DYDX-A": "ConnectV2Dydx",
|
||||
// "FEE-A": "ConnectV2Fee",
|
||||
// "GELATO-A": "ConnectV2Gelato",
|
||||
// "MAKERDAO-A": "ConnectV2Maker",
|
||||
// "UNISWAP-A": "ConnectV2UniswapV2",
|
||||
"ZEROEX-A": "ConnectV2ZeroExAvalanche",
|
||||
};
|
||||
|
||||
const addressMapping: Record<string, string> = {};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { ethers } from "hardhat";
|
||||
import hre, { ethers } from "hardhat";
|
||||
|
||||
export const deployConnector = async (connectorName: string) => {
|
||||
const Connector = await ethers.getContractFactory(connectorName);
|
||||
|
@ -6,5 +6,16 @@ export const deployConnector = async (connectorName: string) => {
|
|||
await connector.deployed();
|
||||
|
||||
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();
|
||||
}
|
||||
return connector.address;
|
||||
};
|
||||
|
|
|
@ -6,10 +6,6 @@ export const addresses: Record<string, any> = {
|
|||
},
|
||||
core: {
|
||||
connectorsV2: "0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14",
|
||||
<<<<<<< HEAD
|
||||
instaIndex: " 0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25",
|
||||
=======
|
||||
instaIndex: "0x6CE3e607C808b4f4C26B7F6aDAeB619e49CAbb25",
|
||||
>>>>>>> 7dcd5072bc40856e46d14a8c58daea372a8b049b
|
||||
},
|
||||
};
|
||||
|
|
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")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -9,7 +9,7 @@ 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, ConnectV2ZeroExPolygon__factory } from "../../../typechain";
|
||||
import {ConnectV2ZeroExPolygon__factory } from "../../../typechain";
|
||||
import er20abi from "../../../scripts/constant/abi/basics/erc20.json";
|
||||
import type { Signer, Contract } from "ethers";
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user