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 ee4c6c4
Show file tree
Hide file tree
Showing 19 changed files with 1,456 additions and 807 deletions.
39 changes: 39 additions & 0 deletions packages/api/examples/radiant-v2/borrow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as api from '@protocolink/api';
import * as logics from '@protocolink/logics';

// interface BorrowFields {
// interestRateMode: logics.radiantv2.InterestRateMode;
// output: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// }

// interface BorrowLogic {
// rid: string;
// fields: BorrowFields;
// }

// TODO: unskip this after @protocolink/api radiant updated
// (async () => {
// const chainId = 42161;

// const tokenList = await api.protocols.radiantv2.getBorrowTokenList(chainId);
// const underlyingToken = tokenList[0];
// console.log('underlyingToken :>> ', JSON.stringify(underlyingToken, null, 2));

// const borrowLogic = await api.protocols.radiantv2.newBorrowLogic({
// interestRateMode: logics.radiantv2.InterestRateMode.variable,
// output: {
// token: underlyingToken,
// amount: '10',
// },
// });
// console.log('borrowLogic :>> ', JSON.stringify(borrowLogic, null, 2));
// })();
72 changes: 72 additions & 0 deletions packages/api/examples/radiant-v2/deposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as api from '@protocolink/api';

// interface DepositParams {
// input: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// tokenOut: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// }

// interface DepositFields {
// input: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// output: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// }

// interface DepositLogic {
// rid: string;
// fields: DepositFields;
// }

// TODO: unskip this after @protocolink/api radiant updated
// (async () => {
// const chainId = 42161;

// const tokenList = await api.protocols.radiantv2.getDepositTokenList(chainId);
// const underlyingToken = tokenList[0][0];
// const rToken = tokenList[0][1];
// console.log('underlyingToken :>> ', JSON.stringify(underlyingToken, null, 2));
// console.log('rToken :>> ', JSON.stringify(rToken, null, 2));

// const depositQuotation = await api.protocols.radiantv2.getDepositQuotation(chainId, {
// input: {
// token: underlyingToken,
// amount: '10',
// },
// tokenOut: rToken,
// });
// console.log('depositQuotation :>> ', JSON.stringify(depositQuotation, null, 2));

// const depositLogic = await api.protocols.radiantv2.newDepositLogic(depositQuotation);
// console.log('depositLogic :>> ', JSON.stringify(depositLogic, null, 2));
// })();
53 changes: 53 additions & 0 deletions packages/api/examples/radiant-v2/flash-loan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as api from '@protocolink/api';

// import * as common from '@protocolink/common';

// interface FlashLoanLogicFields {
// id: string;
// outputs: common.TokenAmounts;
// isLoan: boolean;
// }

// interface FlashLoanFields {
// id: string;
// outputs: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// }[];
// isLoan: boolean;
// }

// interface FlashLoanLogic {
// rid: string;
// fields: FlashLoanFields;
// }

// TODO: unskip this after @protocolink/api radiant updated
// (async () => {
// const chainId = 42161;

// const tokenList = await api.protocols.radiantv2.getFlashLoanTokenList(chainId);
// const underlyingToken = tokenList[0];
// console.log('underlyingToken :>> ', JSON.stringify(underlyingToken, null, 2));

// const outputs = [
// {
// token: underlyingToken,
// amount: '10000',
// },
// ];

// const [flashLoanLoanLogic, flashLoanRepayLogic] = api.protocols.radiantv2.newFlashLoanLogicPair(outputs);
// const logics = [flashLoanLoanLogic];
// // logics.push(swapLogic)
// // logics.push(supplyLogic)
// // logics.push(...)
// logics.push(flashLoanRepayLogic);
// console.log('logics :>> ', JSON.stringify(logics, null, 2));
// })();
54 changes: 54 additions & 0 deletions packages/api/examples/radiant-v2/repay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as api from '@protocolink/api';
import * as logics from '@protocolink/logics';

// interface RepayParams {
// tokenIn: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// borrower: string;
// interestRateMode: logics.radiantv2.InterestRateMode;
// }

// interface RepayFields {
// input: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// borrower: string;
// interestRateMode: logics.radiantv2.InterestRateMode;
// }

// interface RepayLogic {
// rid: string;
// fields: RepayFields;
// }

// TODO: unskip this after @protocolink/api radiant updated
// (async () => {
// const chainId = 42161;
// const account = '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa';

// const tokenList = await api.protocols.radiantv2.getRepayTokenList(chainId);
// const underlyingToken = tokenList[0];
// console.log('underlyingToken :>> ', JSON.stringify(underlyingToken, null, 2));

// const repayQuotation = await api.protocols.radiantv2.getRepayQuotation(chainId, {
// borrower: account,
// tokenIn: underlyingToken,
// interestRateMode: logics.radiantv2.InterestRateMode.variable,
// });
// console.log('repayQuotation :>> ', JSON.stringify(repayQuotation, null, 2));

// const repayLogic = await api.protocols.radiantv2.newRepayLogic(repayQuotation);
// console.log('repayLogic :>> ', JSON.stringify(repayLogic, null, 2));
// })();
72 changes: 72 additions & 0 deletions packages/api/examples/radiant-v2/withdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as api from '@protocolink/api';

// interface WithdrawParams {
// input: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// tokenOut: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// }

// interface WithdrawFields {
// input: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// output: {
// token: {
// chainId: number;
// address: string;
// decimals: number;
// symbol: string;
// name: string;
// };
// amount: string;
// };
// }

// interface WithdrawLogic {
// rid: string;
// fields: WithdrawFields;
// }

// TODO: unskip this after @protocolink/api radiant updated
// (async () => {
// const chainId = 42161;

// const tokenList = await api.protocols.radiantv2.getWithdrawTokenList(chainId);
// const rToken = tokenList[0][0];
// const underlyingToken = tokenList[0][1];
// console.log('rToken :>> ', JSON.stringify(rToken, null, 2));
// console.log('underlyingToken :>> ', JSON.stringify(underlyingToken, null, 2));

// const withdrawQuotation = await api.protocols.radiantv2.getWithdrawQuotation(chainId, {
// input: {
// token: rToken,
// amount: '10',
// },
// tokenOut: underlyingToken,
// });
// console.log('withdrawQuotation :>> ', JSON.stringify(withdrawQuotation, null, 2));

// const withdrawLogic = await api.protocols.radiantv2.newWithdrawLogic(withdrawQuotation);
// console.log('withdrawLogic :>> ', JSON.stringify(withdrawLogic, null, 2));
// })();
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
Loading

0 comments on commit ee4c6c4

Please sign in to comment.