Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions app/api/positions/historical/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ async function getPositionAtBlock(
blockNumber: number,
chainId: number,
) {
console.log(`Get user position ${marketId.slice(0, 6)} at blockNumber ${blockNumber}`);
const isNow = blockNumber === 0;

if (!isNow) {
console.log(`Get user position ${marketId.slice(0, 6)} at blockNumber ${blockNumber}`);
} else {
console.log(`Get user position ${marketId.slice(0, 6)} at current block`);
}

const client = chainId === SupportedNetworks.Mainnet ? mainnetClient : baseClient;
if (!client) throw new Error(`Unsupported chain ID: ${chainId}`);
Expand All @@ -64,7 +70,7 @@ async function getPositionAtBlock(
abi: morphoABI,
functionName: 'position',
args: [marketId as `0x${string}`, userAddress as Address],
blockNumber: BigInt(blockNumber),
blockNumber: isNow ? undefined : BigInt(blockNumber),
})) as readonly bigint[];

// Convert array to position object
Expand Down Expand Up @@ -111,23 +117,6 @@ async function getPositionAtBlock(
market.totalBorrowShares,
);

// console.log(`Successfully retrieved position data:`, {
// marketId,
// userAddress,
// blockNumber,
// supplyShares: position.supplyShares.toString(),
// supplyAssets: supplyAssets.toString(),
// borrowShares: position.borrowShares.toString(),
// borrowAssets: borrowAssets.toString(),
// collateral: position.collateral.toString(),
// market: {
// totalSupplyAssets: market.totalSupplyAssets.toString(),
// totalSupplyShares: market.totalSupplyShares.toString(),
// totalBorrowAssets: market.totalBorrowAssets.toString(),
// totalBorrowShares: market.totalBorrowShares.toString(),
// },
// });

return {
supplyShares: position.supplyShares.toString(),
supplyAssets: supplyAssets.toString(),
Expand Down Expand Up @@ -161,7 +150,7 @@ export async function GET(request: NextRequest) {
// chainId,
// });

if (!blockNumber || !marketId || !userAddress) {
if (!marketId || !userAddress || (!blockNumber && blockNumber !== 0)) {
console.error('Missing required parameters:', {
blockNumber: !!blockNumber,
marketId: !!marketId,
Expand Down
194 changes: 194 additions & 0 deletions src/abis/morpho-snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
export const abi = [
{
inputs: [{ internalType: 'address', name: 'morphoAddress', type: 'address' }],
stateMutability: 'nonpayable',
type: 'constructor',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
{
components: [
{ internalType: 'uint128', name: 'totalSupplyAssets', type: 'uint128' },
{ internalType: 'uint128', name: 'totalSupplyShares', type: 'uint128' },
{ internalType: 'uint128', name: 'totalBorrowAssets', type: 'uint128' },
{ internalType: 'uint128', name: 'totalBorrowShares', type: 'uint128' },
{ internalType: 'uint128', name: 'lastUpdate', type: 'uint128' },
{ internalType: 'uint128', name: 'fee', type: 'uint128' },
],
internalType: 'struct Market',
name: 'market',
type: 'tuple',
},
],
name: 'borrowAPY',
outputs: [{ internalType: 'uint256', name: 'borrowApy', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
{ internalType: 'address', name: 'user', type: 'address' },
],
name: 'borrowAssetsUser',
outputs: [{ internalType: 'uint256', name: 'totalBorrowAssets', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ internalType: 'Id', name: 'marketId', type: 'bytes32' },
{ internalType: 'address', name: 'user', type: 'address' },
],
name: 'collateralAssetsUser',
outputs: [{ internalType: 'uint256', name: 'totalCollateralAssets', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
],
name: 'marketTotalBorrow',
outputs: [{ internalType: 'uint256', name: 'totalBorrowAssets', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
],
name: 'marketTotalSupply',
outputs: [{ internalType: 'uint256', name: 'totalSupplyAssets', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'morpho',
outputs: [{ internalType: 'contract IMorpho', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
{
components: [
{ internalType: 'uint128', name: 'totalSupplyAssets', type: 'uint128' },
{ internalType: 'uint128', name: 'totalSupplyShares', type: 'uint128' },
{ internalType: 'uint128', name: 'totalBorrowAssets', type: 'uint128' },
{ internalType: 'uint128', name: 'totalBorrowShares', type: 'uint128' },
{ internalType: 'uint128', name: 'lastUpdate', type: 'uint128' },
{ internalType: 'uint128', name: 'fee', type: 'uint128' },
],
internalType: 'struct Market',
name: 'market',
type: 'tuple',
},
],
name: 'supplyAPY',
outputs: [{ internalType: 'uint256', name: 'supplyApy', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
{ internalType: 'address', name: 'user', type: 'address' },
],
name: 'supplyAssetsUser',
outputs: [{ internalType: 'uint256', name: 'totalSupplyAssets', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
components: [
{ internalType: 'address', name: 'loanToken', type: 'address' },
{ internalType: 'address', name: 'collateralToken', type: 'address' },
{ internalType: 'address', name: 'oracle', type: 'address' },
{ internalType: 'address', name: 'irm', type: 'address' },
{ internalType: 'uint256', name: 'lltv', type: 'uint256' },
],
internalType: 'struct MarketParams',
name: 'marketParams',
type: 'tuple',
},
{ internalType: 'Id', name: 'id', type: 'bytes32' },
{ internalType: 'address', name: 'user', type: 'address' },
],
name: 'userHealthFactor',
outputs: [{ internalType: 'uint256', name: 'healthFactor', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
];
10 changes: 10 additions & 0 deletions src/components/supplyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useLocalStorage } from '@/hooks/useLocalStorage';
import { usePermit2 } from '@/hooks/usePermit2';
import { useStyledToast } from '@/hooks/useStyledToast';
import { useTransactionWithToast } from '@/hooks/useTransactionWithToast';
import { useUserMarketsCache } from '@/hooks/useUserMarketsCache';
import { formatBalance, formatReadable } from '@/utils/balance';
import { getExplorerURL } from '@/utils/external';
import { getBundlerV2, getIRMTitle, MONARCH_TX_IDENTIFIER } from '@/utils/morpho';
Expand All @@ -35,6 +36,8 @@ export function SupplyModal({ market, onClose }: SupplyModalProps): JSX.Element
const [currentStep, setCurrentStep] = useState<'approve' | 'signing' | 'supplying'>('approve');
const [usePermit2Setting] = useLocalStorage('usePermit2', true);

const { batchAddUserMarkets } = useUserMarketsCache();

const { address: account, isConnected, chainId } = useAccount();

const loanToken = findToken(market.loanAsset.address, market.morphoBlue.chain.id);
Expand Down Expand Up @@ -173,6 +176,13 @@ export function SupplyModal({ market, onClose }: SupplyModalProps): JSX.Element
value: useEth ? supplyAmount : 0n,
});

batchAddUserMarkets([
{
marketUniqueKey: market.uniqueKey,
chainId: market.morphoBlue.chain.id,
},
]);

// come back to main supply page
setShowProcessModal(false);
} catch (error: unknown) {
Expand Down
7 changes: 1 addition & 6 deletions src/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,8 @@ export const userPositionsQuery = `
state {
supplyShares
supplyAssets
supplyAssetsUsd
borrowShares
borrowAssets
borrowAssetsUsd
collateral
collateralUsd
}
Expand All @@ -182,16 +180,13 @@ export const userPositionForMarketQuery = `
state {
supplyShares
supplyAssets
supplyAssetsUsd
borrowShares
borrowAssets
borrowAssetsUsd
collateral
collateralUsd
}
market {
...MarketFields
}
...MarketFields
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/useBorrowTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useLocalStorage } from './useLocalStorage';
import { usePermit2 } from './usePermit2';
import { useStyledToast } from './useStyledToast';
import { useTransactionWithToast } from './useTransactionWithToast';
import { useUserMarketsCache } from './useUserMarketsCache';

type UseBorrowTransactionProps = {
market: Market;
Expand All @@ -27,6 +28,8 @@ export function useBorrowTransaction({
const [usePermit2Setting] = useLocalStorage('usePermit2', true);
const [useEth, setUseEth] = useState<boolean>(false);

const { batchAddUserMarkets } = useUserMarketsCache();

const { address: account, chainId } = useAccount();
const toast = useStyledToast();

Expand Down Expand Up @@ -167,6 +170,13 @@ export function useBorrowTransaction({
value: useEth ? collateralAmount : 0n,
});

batchAddUserMarkets([
{
marketUniqueKey: market.uniqueKey,
chainId: market.morphoBlue.chain.id,
},
]);

// come back to main borrow page
setShowProcessModal(false);
} catch (error: unknown) {
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/useMultiMarketSupply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SupportedNetworks } from '@/utils/networks';
import { Market } from '@/utils/types';
import { useERC20Approval } from './useERC20Approval';
import { useStyledToast } from './useStyledToast';

import { useUserMarketsCache } from './useUserMarketsCache';
export type MarketSupply = {
market: Market;
amount: bigint;
Expand All @@ -33,6 +33,8 @@ export function useMultiMarketSupply(
const tokenSymbol = loanAsset?.symbol;
const totalAmount = supplies.reduce((sum, supply) => sum + supply.amount, 0n);

const { batchAddUserMarkets } = useUserMarketsCache();

const {
authorizePermit2,
permit2Authorized,
Expand Down Expand Up @@ -160,6 +162,13 @@ export function useMultiMarketSupply(
value: useEth ? totalAmount : 0n,
});

batchAddUserMarkets(
supplies.map((supply) => ({
marketUniqueKey: supply.market.uniqueKey,
chainId: supply.market.morphoBlue.chain.id,
})),
);

return true;
} catch (error: unknown) {
console.error('Error in executeSupplyTransaction:', error);
Expand Down
Loading