Skip to content

Commit

Permalink
Merge pull request #3 from EnsoFinance/various-features-and-clean-up
Browse files Browse the repository at this point in the history
FE-1233 and refactor, clean-up
  • Loading branch information
andre-meyer authored Aug 11, 2023
2 parents 05631bc + f2f97d1 commit 178ca8c
Show file tree
Hide file tree
Showing 21 changed files with 324 additions and 81 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Enso Finance

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ensofinance/use-defi",
"version": "0.2.1",
"version": "0.2.2",
"description": "React hooks library to use with enso shortcuts infrastructure",
"main": "index.js",
"scripts": {
Expand All @@ -15,7 +15,7 @@
},
"keywords": [],
"author": "@ensofinance",
"license": "ISC",
"license": "MIT",
"peerDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -24,9 +24,10 @@
"wagmi": "^1.3.9"
},
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"axios": "^1.4.0",
"dotenv": "^16.3.1",
"query-string": "^8.1.0",
"@ethersproject/providers": "^5.7.2"
"query-string": "^8.1.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.2",
Expand Down
16 changes: 12 additions & 4 deletions playground/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useExecutePosition, usePositions } from '@ensofinance/use-defi';
import { useMemo } from 'react';
import { useExecutePosition, useMultichainRoute, usePositions } from '@ensofinance/use-defi';
import { parseUnits } from 'viem';

import { UseExecutePositionArgs } from '../../src/hooks/useExecutePosition/types';

import './App.css';
import Connect from './Connect';
import LoadingGuard from './LoadingGuard';
Expand All @@ -19,6 +16,8 @@ function App() {
token: tokenOut,
});

// if (positions) console.log(positions[0]);

const {
executeRoute,
executeApprovalsOrTransfers,
Expand All @@ -34,6 +33,14 @@ function App() {
},
});

const { execute: executeMultichainRoute } = useMultichainRoute({
tokenIn,
tokenOut: '0xae7ab96520de3a18e5e111b5eaab095312d7fe84',
destinationChainId: 1,
sourceChainId: 42161,
amountIn: amount,
});

const hasRoute = !!executionDetails;

return (
Expand All @@ -50,6 +57,7 @@ function App() {
</div>
)}
</LoadingGuard>
<button onClick={executeMultichainRoute}>Execute Multichain Route</button>
<LoadingGuard isLoading={executePositionStatus === 'loading'}>
{hasRoute ? (
<>
Expand Down
52 changes: 45 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default [
preventAssignment: true,
}),
peerDepsExternal(),
resolve(),
resolve({ jsnext: true, preferBuiltins: true, browser: true }),
commonjs(),
esbuild({
jsx: 'transform', // default, or 'preserve'
Expand Down
21 changes: 11 additions & 10 deletions src/api/approve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import axios from 'axios';
import queryString from 'query-string';

import { ENSO_API } from '../constants';
import { API_AllowancesOptions, API_AllowancesResponse, API_ApproveOptions, API_ApproveResponse } from '../types/api';
import { API_Response } from '../types/enso';
import { parseApiErrorOrReturn } from '../utils/parseApiError';

export const getEnsoApiApprove = async (options: API_ApproveOptions): Promise<API_ApproveResponse | undefined> => {
const queryParams = {
Expand All @@ -11,12 +14,11 @@ export const getEnsoApiApprove = async (options: API_ApproveOptions): Promise<AP
amount: options.amount,
};

const response = await fetch(`${ENSO_API}/api/v1/wallet/approve?${queryString.stringify(queryParams)}`);
const { data } = await axios.get<API_Response<API_ApproveResponse>>(
`${ENSO_API}/api/v1/wallet/approve?${queryString.stringify(queryParams)}`,
);

const json = (await response.json()) as API_ApproveResponse;

if (!json.tx) throw new Error('No valid response');
return json;
return parseApiErrorOrReturn(data);
};

export const getEnsoApiAllowance = async (
Expand All @@ -27,10 +29,9 @@ export const getEnsoApiAllowance = async (
fromAddress: options.fromAddress,
};

const response = await fetch(`${ENSO_API}/api/v1/wallet/approvals?${queryString.stringify(queryParams)}`);

const json = (await response.json()) as API_AllowancesResponse;
const { data } = await axios.get<API_Response<API_AllowancesResponse>>(
`${ENSO_API}/api/v1/wallet/approvals?${queryString.stringify(queryParams)}`,
);

if (!Array.isArray(json)) throw new Error('No valid response');
return json;
return parseApiErrorOrReturn(data);
};
28 changes: 28 additions & 0 deletions src/api/multichain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axios from 'axios';

import { ENSO_API } from '../constants';
import { API_MultichainOptions, API_MultichainResponse } from '../types/api';
import { API_Response } from '../types/enso';
import { parseApiErrorOrReturn } from '../utils/parseApiError';

export const getEnsoApiMultichainRoute = async (
options: API_MultichainOptions,
): Promise<API_MultichainResponse | undefined> => {
const { data } = await axios.post<API_Response<API_MultichainResponse>>(
`${ENSO_API}/api/experimental/multichain/shortcut/route/${options.sourceChainId}/${options.destinationChainId}/${options.fromAddress}`,
JSON.stringify({
tokenIn: options.tokenIn,
tokenOut: options.tokenOut,
amountIn: options.amountIn,
slippage: options.slippage,
}),
{
headers: {
Authorization: `Bearer ${options.apiKey}`,
'Content-Type': 'application/json',
},
},
);

return parseApiErrorOrReturn(data);
};
18 changes: 8 additions & 10 deletions src/api/positions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { USE_POSITIONS_DATA_SOURCE } from '../constants';
import { Position } from '../types/api';

export type QueryMetaPositionsArgs = Record<never, never>;
export type QueryPositionsResponse = Position[];
import axios from 'axios';

export const getEnsoApiPositions = async (): Promise<QueryPositionsResponse> => {
const response = await fetch(USE_POSITIONS_DATA_SOURCE);
import { USE_POSITIONS_DATA_SOURCE } from '../constants';
import { API_GetPositionsResponse } from '../types/api';
import { API_Response } from '../types/enso';
import { parseApiErrorOrReturn } from '../utils/parseApiError';

const json = (await response.json()) as QueryPositionsResponse;
export const getEnsoApiPositions = async (): Promise<API_GetPositionsResponse> => {
const { data } = await axios.get<API_Response<API_GetPositionsResponse>>(USE_POSITIONS_DATA_SOURCE);

if (!Array.isArray(json)) throw new Error('No valid response');
return json;
return parseApiErrorOrReturn(data);
};
Loading

0 comments on commit 178ca8c

Please sign in to comment.