mirror of
				https://github.com/Instadapp/dsa-connectors.git
				synced 2024-07-29 22:37:00 +00:00 
			
		
		
		
	added test for 0x
This commit is contained in:
		
							parent
							
								
									714a4d91b1
								
							
						
					
					
						commit
						19d8d85eaf
					
				
							
								
								
									
										59
									
								
								test/0x/api.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								test/0x/api.js
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,59 @@ | |||
| import axios from "axios"; | ||||
| import { childLogger } from "./logger"; | ||||
| 
 | ||||
| const logger = childLogger("utils/api"); | ||||
| 
 | ||||
| function getApi() { | ||||
|   const api = axios.create({}); | ||||
| 
 | ||||
|   api.interceptors.request.use( | ||||
|     (request) => { | ||||
|       logger.info({ | ||||
|         msg: "Request to external API", | ||||
|         req: { | ||||
|           method: request.method, | ||||
|           url: request.url, | ||||
|           params: request.params, | ||||
|           headers: request.headers, | ||||
|         }, | ||||
|       }); | ||||
| 
 | ||||
|       return request; | ||||
|     }, | ||||
|     (error) => { | ||||
|       logger.error({ msg: "Error while requesting external API", error }); | ||||
| 
 | ||||
|       return Promise.reject(error); | ||||
|     } | ||||
|   ); | ||||
| 
 | ||||
|   api.interceptors.response.use( | ||||
|     (response) => { | ||||
|       logger.info({ | ||||
|         msg: "Response from external API", | ||||
|         req: { | ||||
|           method: response.config.method, | ||||
|           url: response.config.url, | ||||
|           params: response.config.params, | ||||
|         }, | ||||
|         res: { | ||||
|           statusCode: response.status, | ||||
|           statusText: response.statusText, | ||||
|           headers: response.config.headers, | ||||
|           data: response.data, | ||||
|         }, | ||||
|       }); | ||||
| 
 | ||||
|       return response; | ||||
|     }, | ||||
|     (error) => { | ||||
|       logger.error({ msg: "Error response from external API", error }); | ||||
| 
 | ||||
|       return Promise.reject(error); | ||||
|     } | ||||
|   ); | ||||
| 
 | ||||
|   return api; | ||||
| } | ||||
| 
 | ||||
| export default getApi(); | ||||
							
								
								
									
										173
									
								
								test/0x/zeroEx.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										173
									
								
								test/0x/zeroEx.test.js
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,173 @@ | |||
| const { expect } = require("chai"); | ||||
| const hre = require("hardhat"); | ||||
| const axios = require("axios"); | ||||
| const { web3, deployments, waffle, ethers } = hre; | ||||
| const { provider, deployContract } = waffle; | ||||
| const BigNumber = require("bignumber.js"); | ||||
| 
 | ||||
| // import axpi from "./api";
 | ||||
| 
 | ||||
| const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js"); | ||||
| const buildDSAv2 = require("../../scripts/buildDSAv2"); | ||||
| const encodeSpells = require("../../scripts/encodeSpells.js"); | ||||
| const getMasterSigner = require("../../scripts/getMasterSigner"); | ||||
| const addLiquidity = require("../../scripts/addLiquidity"); | ||||
| 
 | ||||
| const addresses = require("../../scripts/constant/addresses"); | ||||
| const abis = require("../../scripts/constant/abis"); | ||||
| const constants = require("../../scripts/constant/constant"); | ||||
| const tokens = require("../../scripts/constant/tokens"); | ||||
| 
 | ||||
| const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/0x/main.sol/ConnectV2ZeroEx.json"); | ||||
| 
 | ||||
| describe("ZeroEx", function() { | ||||
|   const connectorName = "zeroEx-test"; | ||||
| 
 | ||||
|   let dsaWallet0; | ||||
|   let masterSigner; | ||||
|   let instaConnectorsV2; | ||||
|   let connector; | ||||
| 
 | ||||
|   const wallets = provider.getWallets(); | ||||
|   const [wallet0, wallet1, wallet2, wallet3] = wallets; | ||||
| 
 | ||||
|   before(async () => { | ||||
|     // await hre.network.provider.request({
 | ||||
|     //   method: "hardhat_reset",
 | ||||
|     //   params: [
 | ||||
|     //     {
 | ||||
|     //       forking: {
 | ||||
|     //         jsonRpcUrl: hre.config.networks.hardhat.forking.url,
 | ||||
|     //         blockNumber: 13300000,
 | ||||
|     //       },
 | ||||
|     //     },
 | ||||
|     //   ],
 | ||||
|     // });
 | ||||
|     masterSigner = await getMasterSigner(wallet3); | ||||
|     instaConnectorsV2 = await ethers.getContractAt( | ||||
|       abis.core.connectorsV2, | ||||
|       addresses.core.connectorsV2 | ||||
|     ); | ||||
|     connector = await deployAndEnableConnector({ | ||||
|       connectorName, | ||||
|       contractArtifact: connectV2CompoundArtifacts, | ||||
|       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 and DAI 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") | ||||
|       ); | ||||
| 
 | ||||
|       // await addLiquidity(
 | ||||
|       //     "dai",
 | ||||
|       //     dsaWallet0.address,
 | ||||
|       //     ethers.utils.parseUnits("100000", 18)
 | ||||
|       // );
 | ||||
|       // expect(await ether  s.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("100000", 18));
 | ||||
|     }); | ||||
|   }); | ||||
| 
 | ||||
|   describe("Main", function() { | ||||
|     it("should swap", async function() { | ||||
|       async function getArg() { | ||||
|         const slippage = 0.5; | ||||
| 
 | ||||
|         /* MATIC -> USDT */ | ||||
|         const sellTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Matic,  decimals 18
 | ||||
|         const sellTokenDecimals = 18; | ||||
|         const buyTokenAddress = "0xc2132D05D31c914a87C6611C10748AEb04B58e8F"; // USDT, decimals 6
 | ||||
|         const buyTokenDecimals = 6; | ||||
|         const amount = 1; | ||||
| 
 | ||||
|         const srcAmount = new BigNumber(amount) | ||||
|           .times(new BigNumber(10).pow(sellTokenDecimals)) | ||||
|           .toFixed(0); | ||||
|         console.log(srcAmount); | ||||
| 
 | ||||
|         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 buyTokenAmount = await axios | ||||
|           .get(url, { params: params }) | ||||
|           .then((data) => data.data.buyAmount); | ||||
| 
 | ||||
|         const calldata = await axios | ||||
|           .get(url, { params: params }) | ||||
|           .then((data) => data.data.data); | ||||
| 
 | ||||
|         console.log(calldata); | ||||
| 
 | ||||
|         let caculateUnitAmt = () => { | ||||
|           const buyTokenAmountRes = new BigNumber(buyTokenAmount) | ||||
|             .dividedBy(new BigNumber(10).pow(buyTokenDecimals)) | ||||
|             .toFixed(8); | ||||
| 
 | ||||
|           let unitAmt = 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(); | ||||
| 
 | ||||
|         console.log("unitAmt - " + unitAmt); | ||||
| 
 | ||||
|         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), wallet1.address); | ||||
|       const receipt = await tx.wait(); | ||||
|       console.log(receipt); | ||||
|       expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( | ||||
|         ethers.utils.parseEther("9") | ||||
|       ); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 bhavik
						bhavik