Skip to content

Commit

Permalink
feat: add radiant v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Lu committed Sep 26, 2023
1 parent 47e43d3 commit c49426f
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 44 deletions.
79 changes: 40 additions & 39 deletions packages/api/examples/utility/custom-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,46 @@ import * as common from '@protocolink/common';
// fields: CustomDataFields;
// }

(async () => {
const chainId = 1;
const account = '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa';
// TODO: unskip this after we set up 1inch API v5.2 (The public Swap API api.1inch.io is deprecated.)
// (async () => {
// const chainId = 1;
// const account = '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa';

// 1. get quotation from 1inch api
const fromToken = {
chainId: 1,
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
decimals: 6,
symbol: 'USDC',
name: 'USD Coin',
};
const toToken = {
chainId: 1,
address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
decimals: 18,
symbol: 'DAI',
name: 'Dai Stablecoin',
};
const input = new common.TokenAmount(fromToken, '100');
// // 1. get quotation from 1inch api
// const fromToken = {
// chainId: 1,
// address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
// decimals: 6,
// symbol: 'USDC',
// name: 'USD Coin',
// };
// const toToken = {
// chainId: 1,
// address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
// decimals: 18,
// symbol: 'DAI',
// name: 'Dai Stablecoin',
// };
// const input = new common.TokenAmount(fromToken, '100');

const { data } = await axios.get(`https://api.1inch.io/v5.0/${chainId}/swap`, {
params: {
fromTokenAddress: fromToken.address,
toTokenAddress: toToken.address,
amount: input.amountWei.toString(),
fromAddress: account,
slippage: 1,
disableEstimate: true,
},
});
console.log('data :>> ', JSON.stringify(data));
const output = new common.TokenAmount(toToken).setWei(data.toTokenAmount);
// const { data } = await axios.get(`https://api.1inch.io/v5.0/${chainId}/swap`, {
// params: {
// fromTokenAddress: fromToken.address,
// toTokenAddress: toToken.address,
// amount: input.amountWei.toString(),
// fromAddress: account,
// slippage: 1,
// disableEstimate: true,
// },
// });
// console.log('data :>> ', JSON.stringify(data));
// const output = new common.TokenAmount(toToken).setWei(data.toTokenAmount);

const customDataLogic = await api.protocols.utility.newCustomDataLogic({
inputs: [input],
outputs: [output],
to: data.tx.to,
data: data.tx.data,
});
console.log('customDataLogic :>> ', JSON.stringify(customDataLogic, null, 2));
})();
// const customDataLogic = await api.protocols.utility.newCustomDataLogic({
// inputs: [input],
// outputs: [output],
// to: data.tx.to,
// data: data.tx.data,
// });
// console.log('customDataLogic :>> ', JSON.stringify(customDataLogic, null, 2));
// })();
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"@protocolink/common": "^0.3.0",
"@protocolink/core": "^0.4.0",
"@protocolink/logics": "^0.4.0",
"@protocolink/logics": "^0.4.1",
"@types/lodash": "^4.14.195",
"@types/uuid": "^9.0.2",
"@uniswap/permit2-sdk": "^1.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/protocols/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * as aavev3 from './aave-v3';
export * as balancerv2 from './balancer-v2';
export * as compoundv3 from './compound-v3';
export * as paraswapv5 from './paraswap-v5';
export * as radiantv2 from './radiant-v2';
export * as syncswap from './syncswap';
export * as uniswapv3 from './uniswap-v3';
export * as utility from './utility';
16 changes: 16 additions & 0 deletions packages/api/src/protocols/radiant-v2/borrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Logic } from 'src/types';
import * as common from '@protocolink/common';
import { getProtocolTokenList } from 'src/api';
import * as logics from '@protocolink/logics';

export type BorrowFields = common.Declasifying<logics.radiantv2.BorrowLogicFields>;

export type BorrowLogic = Logic<BorrowFields>;

export async function getBorrowTokenList(chainId: number): Promise<logics.radiantv2.BorrowLogicTokenList> {
return getProtocolTokenList(chainId, logics.radiantv2.BorrowLogic.rid);
}

export function newBorrowLogic(fields: BorrowFields): BorrowLogic {
return { rid: logics.radiantv2.BorrowLogic.rid, fields };
}
25 changes: 25 additions & 0 deletions packages/api/src/protocols/radiant-v2/deposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Logic } from 'src/types';
import * as common from '@protocolink/common';
import { getProtocolTokenList, quote } from 'src/api';
import * as logics from '@protocolink/logics';

export type DepositParams = common.Declasifying<logics.radiantv2.DepositLogicParams>;

export type DepositFields = common.Declasifying<logics.radiantv2.DepositLogicFields>;

export type DepositLogic = Logic<DepositFields>;

export async function getDepositTokenList(chainId: number): Promise<logics.radiantv2.DepositLogicTokenList> {
return getProtocolTokenList(chainId, logics.radiantv2.DepositLogic.rid);
}

export async function getDepositQuotation(
chainId: number,
params: DepositParams
): Promise<logics.radiantv2.DepositLogicFields> {
return quote(chainId, logics.radiantv2.DepositLogic.rid, params);
}

export function newDepositLogic(fields: DepositFields): DepositLogic {
return { rid: logics.radiantv2.DepositLogic.rid, fields };
}
27 changes: 27 additions & 0 deletions packages/api/src/protocols/radiant-v2/flash-loan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FlashLoanFields, FlashLoanLogic } from 'src/types';
import * as common from '@protocolink/common';
import { getProtocolTokenList, quote } from 'src/api';
import * as logics from '@protocolink/logics';
import { v4 as uuid } from 'uuid';

export type FlashLoanParams = common.Declasifying<logics.radiantv2.FlashLoanLogicParams>;

export async function getFlashLoanTokenList(chainId: number): Promise<logics.radiantv2.FlashLoanLogicTokenList> {
return getProtocolTokenList(chainId, logics.radiantv2.FlashLoanLogic.rid);
}

export async function getFlashLoanQuotation(
chainId: number,
params: FlashLoanParams
): Promise<logics.radiantv2.FlashLoanLogicQuotation> {
return quote(chainId, logics.radiantv2.FlashLoanLogic.rid, params);
}

export function newFlashLoanLogic(fields: FlashLoanFields): FlashLoanLogic {
return { rid: logics.radiantv2.FlashLoanLogic.rid, fields };
}

export function newFlashLoanLogicPair(loans: FlashLoanFields['loans']): [FlashLoanLogic, FlashLoanLogic] {
const id = uuid();
return [newFlashLoanLogic({ id, loans, isLoan: true }), newFlashLoanLogic({ id, loans, isLoan: false })];
}
5 changes: 5 additions & 0 deletions packages/api/src/protocols/radiant-v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './borrow';
export * from './deposit';
export * from './flash-loan';
export * from './repay';
export * from './withdraw';
25 changes: 25 additions & 0 deletions packages/api/src/protocols/radiant-v2/repay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Logic } from 'src/types';
import * as common from '@protocolink/common';
import { getProtocolTokenList, quote } from 'src/api';
import * as logics from '@protocolink/logics';

export type RepayParams = common.Declasifying<logics.radiantv2.RepayLogicParams>;

export type RepayFields = common.Declasifying<logics.radiantv2.RepayLogicFields>;

export type RepayLogic = Logic<RepayFields>;

export async function getRepayTokenList(chainId: number): Promise<logics.radiantv2.RepayLogicTokenList> {
return getProtocolTokenList(chainId, logics.radiantv2.RepayLogic.rid);
}

export async function getRepayQuotation(
chainId: number,
params: RepayParams
): Promise<logics.radiantv2.RepayLogicFields> {
return quote(chainId, logics.radiantv2.RepayLogic.rid, params);
}

export function newRepayLogic(fields: RepayFields): RepayLogic {
return { rid: logics.radiantv2.RepayLogic.rid, fields };
}
25 changes: 25 additions & 0 deletions packages/api/src/protocols/radiant-v2/withdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Logic } from 'src/types';
import * as common from '@protocolink/common';
import { getProtocolTokenList, quote } from 'src/api';
import * as logics from '@protocolink/logics';

export type WithdrawParams = common.Declasifying<logics.radiantv2.WithdrawLogicParams>;

export type WithdrawFields = common.Declasifying<logics.radiantv2.WithdrawLogicFields>;

export type WithdrawLogic = Logic<WithdrawFields>;

export async function getWithdrawTokenList(chainId: number): Promise<logics.radiantv2.WithdrawLogicTokenList> {
return getProtocolTokenList(chainId, logics.radiantv2.WithdrawLogic.rid);
}

export async function getWithdrawQuotation(
chainId: number,
params: WithdrawParams
): Promise<logics.radiantv2.WithdrawLogicFields> {
return quote(chainId, logics.radiantv2.WithdrawLogic.rid, params);
}

export function newWithdrawLogic(fields: WithdrawFields): WithdrawLogic {
return { rid: logics.radiantv2.WithdrawLogic.rid, fields };
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1080,10 +1080,10 @@
node-addon-api "^3.2.1"
node-gyp-build "^4.3.0"

"@protocolink/logics@^0.4.0":
version "0.4.0"
resolved "https://registry.yarnpkg.com/@protocolink/logics/-/logics-0.4.0.tgz#a58c685daf64c0279d005e45673ae2e246f0c01f"
integrity sha512-k79ztS1nNtQXv5+KzUFeVQkqYoPMPMU7o147mEb43jVPd+jgzv3OGuZsLfAGD8+QGD4Kx72ecDKH5yTXA9TK2A==
"@protocolink/logics@^0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@protocolink/logics/-/logics-0.4.1.tgz#cdc9f7e2b7c4fba66c260231d447cc58b5984cd6"
integrity sha512-RQf8sYWuefsxU5Nk/euFASR3UTa61IMqKllRZZSk+9L1/megh4swnyFWyBbx13XjAe3wcaXF5SGjzlNVp4EZfQ==
dependencies:
"@paraswap/sdk" "^6.2.2"
"@protocolink/common" "^0.3.0"
Expand Down

0 comments on commit c49426f

Please sign in to comment.