Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
amish kohli authored and amish kohli committed Jan 22, 2025
1 parent 3a593db commit 8d4d230
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Set up .env file
run: |
echo "SUPABASE_URL=${{ secrets.SUPABASE_URL }}" > .env
echo "SUPABASE_URL=${{ secrets.SUPABASE_API_URL }}" > .env
echo "SUPABASE_ANON_KEY=${{ secrets.SUPABASE_ANON_KEY }}" >> .env
- name: Build Docker Image
Expand Down
116 changes: 88 additions & 28 deletions src/routes/ionic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,18 @@ function getChainId(chain: string): number {
return 1923;
case 'soneium':
return 1868;
// Add any other chains you need
default:
throw new Error(`Unsupported chain: ${chain}`);
}
}

// Helper function to check if chain is native token chain
function isNativeTokenChain(chain: string): boolean {
const nativeTokenChains = ['mode','optimism', 'base', 'bob', 'fraxtal', 'lisk', 'ink', 'superseed', 'worldchain', 'swell', 'soneium'];
return nativeTokenChains.includes(chain.toLowerCase());
}

// Update the interface for the request body
interface IonicRequestBody {
sender: string;
Expand Down Expand Up @@ -129,6 +136,12 @@ function formatDecimal(num: number): string {
return num.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 18 });
}

// Helper function to convert scientific notation to decimal string
function toFullDecimalString(num: number): string {
// Convert to string without scientific notation
return num.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 18 });
}

// Supply endpoint
router.post('/beta/v0/ionic/supply/:chain', async (req, res) => {
try {
Expand Down Expand Up @@ -179,27 +192,42 @@ router.post('/beta/v0/ionic/supply/:chain', async (req, res) => {
router.post('/beta/v0/ionic/withdraw/:chain', async (req, res) => {
try {
const { chain } = req.params;
const { account, amount, asset } = req.body;
const { sender, call_data } = req.body as IonicRequestBody;

const chainConfig = getChainConfig(chain as SupportedChain);
const publicClient = createPublicClient({
chain: chainConfig,
transport: http()
});

const poolAddress = await getAssetPoolAddress(
chain as SupportedChain,
asset
call_data.asset
);

const formattedAmount = formatDecimal(call_data.amount);
const amountInWei = parseUnits(formattedAmount, 18);

const data = encodeFunctionData({
abi: IonicPoolABI,
functionName: 'redeemUnderlying',
args: [parseEther(amount)]
args: [amountInWei] as const
});

const nonce = await publicClient.getTransactionCount({ address: sender as Address });
const feeData = await publicClient.estimateFeesPerGas();

return res.json({
success: true,
transactionRequest: {
to: poolAddress,
data,
from: account,
}
chainId: chainConfig.id,
data,
from: sender,
to: poolAddress,
value: 0,
nonce: Number(nonce),
maxFeePerGas: Number(feeData?.maxFeePerGas || 0),
maxPriorityFeePerGas: Number(feeData?.maxPriorityFeePerGas || 0)
});

} catch (error: any) {
console.error('Withdraw error:', error);
return res.status(500).json({ success: false, error: error.message });
Expand All @@ -210,27 +238,42 @@ router.post('/beta/v0/ionic/withdraw/:chain', async (req, res) => {
router.post('/beta/v0/ionic/borrow/:chain', async (req, res) => {
try {
const { chain } = req.params;
const { account, amount, asset } = req.body;
const { sender, call_data } = req.body as IonicRequestBody;

const chainConfig = getChainConfig(chain as SupportedChain);
const publicClient = createPublicClient({
chain: chainConfig,
transport: http()
});

const poolAddress = await getAssetPoolAddress(
chain as SupportedChain,
asset
call_data.asset
);

const formattedAmount = formatDecimal(call_data.amount);
const amountInWei = parseUnits(formattedAmount, 18);

const data = encodeFunctionData({
abi: IonicPoolABI,
functionName: 'borrow',
args: [parseEther(amount)]
args: [amountInWei] as const
});

const nonce = await publicClient.getTransactionCount({ address: sender as Address });
const feeData = await publicClient.estimateFeesPerGas();

return res.json({
success: true,
transactionRequest: {
to: poolAddress,
data,
from: account,
}
chainId: chainConfig.id,
data,
from: sender,
to: poolAddress,
value: 0,
nonce: Number(nonce),
maxFeePerGas: Number(feeData?.maxFeePerGas || 0),
maxPriorityFeePerGas: Number(feeData?.maxPriorityFeePerGas || 0)
});

} catch (error: any) {
console.error('Borrow error:', error);
return res.status(500).json({ success: false, error: error.message });
Expand All @@ -241,28 +284,45 @@ router.post('/beta/v0/ionic/borrow/:chain', async (req, res) => {
router.post('/beta/v0/ionic/repay/:chain', async (req, res) => {
try {
const { chain } = req.params;
const { account, amount, asset } = req.body;
const { sender, call_data } = req.body;

const chainConfig = getChainConfig(chain as SupportedChain);
const publicClient = createPublicClient({
chain: chainConfig,
transport: http()
});

const poolAddress = await getAssetPoolAddress(
chain as SupportedChain,
asset
call_data.asset
);

const formattedAmount = formatDecimal(call_data.amount);
const amountInWei = parseUnits(formattedAmount, 18);

const data = encodeFunctionData({
abi: IonicPoolABI,
functionName: 'repayBorrow',
args: [parseEther(amount)]
args: [amountInWei] as const
});

const nonce = await publicClient.getTransactionCount({ address: sender as Address });
const feeData = await publicClient.estimateFeesPerGas();

// Set value for native token repayments and ensure it's formatted as a full string
// const valueToSend = isNativeTokenChain(chain) ? amountInWei.toString() : '0';

return res.json({
success: true,
transactionRequest: {
to: poolAddress,
data,
from: account,
value: chain.toLowerCase() === 'mode' ? parseEther(amount) : undefined
}
chainId: chainConfig.id,
data,
from: sender,
to: poolAddress,
value: 0,
nonce: Number(nonce),
maxFeePerGas: Number(feeData?.maxFeePerGas || 0),
maxPriorityFeePerGas: Number(feeData?.maxPriorityFeePerGas || 0)
});

} catch (error: any) {
console.error('Repay error:', error);
return res.status(500).json({ success: false, error: error.message });
Expand Down

0 comments on commit 8d4d230

Please sign in to comment.