Skip to content

Commit

Permalink
fix: protocol to procol swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
marthendalnunes committed Dec 13, 2024
1 parent 37488e1 commit d00bbb8
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 74 deletions.
89 changes: 60 additions & 29 deletions apps/api-delegations/src/processing/limit-order/get-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
getWithdrawPoolTogetherV5HookData,
} from './protocols/pool-together-v5.js';
import { getDepositUnderlyingAssetHookData } from './protocols/underlying-asset.js';
import {
getDepositCompoundV3HookData,
getWithdrawCompoundV3HookData,
} from './protocols/compound-v3.js';

type HookActions = {
target: Address;
Expand Down Expand Up @@ -53,44 +57,71 @@ export function getHookActions({
throw new Error('Token not found');
}

const tokenOutProtocol = tokenOutData.extensions?.protocol;

// Withdraw actions
if (tokenOutData.extensions?.protocol === 'aave-v3') {
withdrawActions = getWithdrawAaveV3HookData({
amountIn,
tokenIn,
});
} else if (tokenOutData.extensions?.protocol === 'pool-together-v5') {
withdrawActions = getWithdrawPoolTogetherV5HookData({
amountOut,
tokenOut,
});
switch (tokenOutProtocol) {
case 'aave-v3':
withdrawActions = getWithdrawAaveV3HookData({
// Always use the underlying asset as the tokenIn for compound
tokenIn: underlyingAssetData.address as Address,
amountOut,
});
break;
case 'pool-together-v5':
withdrawActions = getWithdrawPoolTogetherV5HookData({
amountOut,
tokenOut,
});
break;
case 'compound-v3':
withdrawActions = getWithdrawCompoundV3HookData({
// Always use the underlying asset as the tokenIn for compound
tokenIn: underlyingAssetData.address as Address,

tokenOut,
});
break;
}

// If there are withdraw calls, the tokenOut should be replaced with the underlying asset
const updatedTokenOut = withdrawActions.length
? (underlyingAssetData.address as Address)
: tokenOut;

const tokenInProtocol = tokenInData.extensions?.protocol;
// Deposit actions
if (tokenInData.extensions?.protocol === 'aave-v3') {
depositActions = getDepositAaveV3HookData({
amountOut,
delegator,
tokenOut: updatedTokenOut,
});
} else if (tokenInData.extensions?.protocol === 'pool-together-v5') {
depositActions = getDepositPoolTogetherV5HookData({
amountOut,
delegator,
tokenIn,
tokenOut: updatedTokenOut,
});
} else if (!tokenInData.extensions?.protocol) {
depositActions = getDepositUnderlyingAssetHookData({
amountOut,
delegator,
tokenIn,
});
switch (tokenInProtocol) {
case 'aave-v3':
depositActions = getDepositAaveV3HookData({
amountOut,
delegator,
tokenOut: updatedTokenOut,
});
break;
case 'pool-together-v5':
depositActions = getDepositPoolTogetherV5HookData({
amountOut,
delegator,
tokenIn,
tokenOut: updatedTokenOut,
});
break;
case 'compound-v3':
depositActions = getDepositCompoundV3HookData({
amountOut,
delegator,
tokenIn,
tokenOut: updatedTokenOut,
});
break;
case undefined:
depositActions = getDepositUnderlyingAssetHookData({
amountIn,
delegator,
tokenIn,
});
break;
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export function getDepositAaveV3HookData({

type GetWithdrawAaveV3HookDataReturnType = GetDepositAaveV3HookDataReturnType;
export function getWithdrawAaveV3HookData({
amountIn,
amountOut,
tokenIn,
}: {
amountIn: bigint;
amountOut: bigint;
tokenIn: Address;
}): GetWithdrawAaveV3HookDataReturnType {
return [
Expand All @@ -58,7 +58,7 @@ export function getWithdrawAaveV3HookData({
callData: encodeFunctionData({
abi: aaveV3PoolAbi,
functionName: 'withdraw',
args: [tokenIn, amountIn, universalDeployments.Multicall],
args: [tokenIn, amountOut, universalDeployments.Multicall],
}),
},
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { encodeFunctionData, erc20Abi, type Address, type Hex } from 'viem';
import { multicallAbi } from 'universal-data';
import {
encodeFunctionData,
erc20Abi,
type Address,
type Hex,
maxUint256,
} from 'viem';
import { compoundV3Abi } from 'universal-data';

type GetDepositCompoundV3HookDataReturnType = {
target: Address;
value: bigint;
callData: Hex;
}[];

export function getDepositCompoundV3HookData({
amountOut,
delegator,
Expand All @@ -12,33 +23,50 @@ export function getDepositCompoundV3HookData({
delegator: Address;
tokenIn: Address;
tokenOut: Address;
}): Hex {
return encodeFunctionData({
abi: multicallAbi,
functionName: 'multicall',
args: [
[
// Approves the token to the Compound Pool
{
target: tokenOut,
value: 0n,
callData: encodeFunctionData({
abi: erc20Abi,
functionName: 'approve',
args: [tokenIn, amountOut],
}),
},
// Deposits the token to the Compound Pool on behalf of the delegator
{
target: tokenIn,
value: 0n,
callData: encodeFunctionData({
abi: compoundV3Abi,
functionName: 'supplyTo',
args: [delegator, tokenOut, amountOut],
}),
},
],
],
});
}): GetDepositCompoundV3HookDataReturnType {
return [
// Approves the token to the Compound Pool
{
target: tokenOut,
value: 0n,
callData: encodeFunctionData({
abi: erc20Abi,
functionName: 'approve',
args: [tokenIn, amountOut],
}),
},
// Deposits the token to the Compound Pool on behalf of the delegator
{
target: tokenIn,
value: 0n,
callData: encodeFunctionData({
abi: compoundV3Abi,
functionName: 'supplyTo',
args: [delegator, tokenOut, amountOut],
}),
},
];
}

type GetWithdrawCompoundV3HookDataReturnType =
GetDepositCompoundV3HookDataReturnType;
export function getWithdrawCompoundV3HookData({
tokenOut,
tokenIn,
}: {
tokenIn: Address;
tokenOut: Address;
}): GetWithdrawCompoundV3HookDataReturnType {
return [
// Approves the token to the Compound Pool
{
target: tokenOut,
value: 0n,
callData: encodeFunctionData({
abi: compoundV3Abi,
functionName: 'withdraw',
args: [tokenIn, maxUint256],
}),
},
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ type GetDepositUnderlyingAssetHookDataReturnType = {
}[];

export function getDepositUnderlyingAssetHookData({
amountOut,
amountIn,
delegator,
tokenIn,
}: {
amountOut: bigint;
amountIn: bigint;
delegator: Address;
tokenIn: Address;
}): GetDepositUnderlyingAssetHookDataReturnType {
Expand All @@ -23,7 +23,7 @@ export function getDepositUnderlyingAssetHookData({
callData: encodeFunctionData({
abi: erc20Abi,
functionName: 'transfer',
args: [delegator, amountOut],
args: [delegator, amountIn],
}),
},
];
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/src/components/forms/form-erc20-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function FormErc20Swap({ defaultValues }: FormErc20SwapProps) {
if (formValues.tokenOut && formValues.tokenIn && formValues.amountOut) {
form.setValue('amountIn', formValues.amountOut);
} else {
form.setValue('amountIn', undefined);
form.setValue('amountIn', '');
}
}, [form, formValues.amountOut, formValues.tokenOut, formValues.tokenIn]);

Expand Down
11 changes: 11 additions & 0 deletions packages/universal-data/src/token-list/stablecoin-token-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,16 @@ export const stablecoinTokenList: TokenList = {
},
},
},
{
chainId: 8453,
name: 'Compound USDC',
symbol: 'cUSDCv3',
logoURI: 'https://ethereum-optimism.github.io/data/USDC/logo.png',
address: '0xb125e6687d4313864e53df431d5425969c15eb2f',
decimals: 6,
extensions: {
protocol: 'compound-v3',
},
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
import { useState } from 'react';
import { ROOT_AUTHORITY, SALT, universalDeployments } from 'universal-data';
import type { Delegation, DelegationWithMetadata } from 'universal-types';
import {
type Address,
parseUnits,
// ,
// parseUnits
} from 'viem';
import { type Address, parseUnits } from 'viem';
import { useSignTypedData } from 'wagmi';
import { useInsertDelegation } from '../api/actions/insert-delegation.js';
import { eip712DelegationTypes } from '../delegation/eip712-delegation-type.js';
import { getDelegationHash } from '../delegation/get-delegation-hash.js';
import { encodeEnforcerERC20TransferAmount } from '../enforcers/enforcer-erc20-transfer-amount.js';
import { encodeERC20BalanceGteWrapEnforcerTerms } from '../enforcers/erc20-balance-gte-wrap-enforcer.js';
// import { encodeERC20BalanceGteWrapEnforcerTerms } from '../enforcers/erc20-balance-gte-wrap-enforcer.js';

type SignDelegationParams = {
chainId: number;
Expand Down

0 comments on commit d00bbb8

Please sign in to comment.