Skip to content

Commit

Permalink
epochs wip
Browse files Browse the repository at this point in the history
  • Loading branch information
noahlitvin authored Aug 8, 2024
2 parents 5817c45 + 36d543a commit b314e11
Show file tree
Hide file tree
Showing 35 changed files with 912 additions and 693 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ import TraderPosition from '~/lib/components/foil/traderPosition';
import TraderPositionsTable from '~/lib/components/foil/traderPositionsTable';
import { MarketProvider } from '~/lib/context/MarketProvider';

const Market = ({ params }: { params: { id: string } }) => {
const Market = ({ params }: { params: { id: string, epoch: string } }) => {
const [chainId, marketAddress] = params.id.split('%3A');
const { epoch } = params;

return (
<MarketProvider chainId={Number(chainId)} address={marketAddress}>
<MarketProvider
chainId={Number(chainId)}
address={marketAddress}
epoch={Number(epoch)}
>
<Flex direction="column" alignItems="left" mb={8} w="full" py={8}>
<PositionsHeader />
<Flex width="100%" gap={12} mb={12}>
Expand Down
49 changes: 4 additions & 45 deletions packages/app/src/lib/components/foil/addLiquidity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const tickToPrice = (tick: number): number => 1.0001 ** tick;

const AddLiquidity = () => {
const {
epoch,
pool,
chain,
collateralAsset,
Expand Down Expand Up @@ -159,6 +160,7 @@ const AddLiquidity = () => {
abi: foilData.abi,
functionName: 'getTokenAmounts',
args: [
epoch.toString(),
parseUnits(depositAmount.toString(), collateralAssetDecimals), // uint256 collateralAmount
pool ? pool.sqrtRatioX96.toString() : '0', // uint160 sqrtPriceX96, // current price of pool
TickMath.getSqrtRatioAtTick(tickLower).toString(), // uint160 sqrtPriceAX96, // lower tick price in sqrtRatio
Expand Down Expand Up @@ -210,56 +212,12 @@ const AddLiquidity = () => {
setDepositAmount(value);
};

useEffect(() => {
console.log('Deposit amount changed:', depositAmount);
// The following lines have been removed as they are redundant:
// const newBaseToken = depositAmount * 0.001;
// const newQuoteToken = depositAmount * 0.001;
// setBaseToken(newBaseToken);
// setQuoteToken(newQuoteToken);
}, [depositAmount]);

useEffect(() => {
if (approveSuccess && transactionStep === 1) {
setTransactionStep(2);
}
}, [approveSuccess, transactionStep]);

const result = useSimulateContract({
address: foilData.address as `0x${string}`,
abi: foilData.abi,
functionName: 'createLiquidityPosition',
args: [
{
amountTokenA: parseUnits(baseToken.toString(), 18),
amountTokenB: parseUnits(quoteToken.toString(), 18),
collateralAmount: parseUnits(
depositAmount.toString(),
collateralAssetDecimals
),
lowerTick: BigInt(tickLower),
upperTick: BigInt(tickUpper),
minAmountTokenA: parseUnits(minAmountTokenA.toString(), 18),
minAmountTokenB: parseUnits(minAmountTokenB.toString(), 18),
},
],
chainId: chain?.id,
});
console.log('result', result, [
{
amountTokenA: parseUnits(baseToken.toString(), 18),
amountTokenB: parseUnits(quoteToken.toString(), 18),
collateralAmount: parseUnits(
depositAmount.toString(),
collateralAssetDecimals
),
lowerTick: BigInt(tickLower),
upperTick: BigInt(tickUpper),
minAmountTokenA: parseUnits(minAmountTokenA.toString(), 18),
minAmountTokenB: parseUnits(minAmountTokenB.toString(), 18),
},
]);

useEffect(() => {
if (transactionStep === 2) {
addLiquidityWrite({
Expand All @@ -268,6 +226,7 @@ const AddLiquidity = () => {
functionName: 'createLiquidityPosition',
args: [
{
epochId: epoch,
amountTokenA: parseUnits(baseToken.toString(), 18),
amountTokenB: parseUnits(quoteToken.toString(), 18),
collateralAmount: parseUnits(
Expand All @@ -285,6 +244,7 @@ const AddLiquidity = () => {
setTransactionStep(3);
}
}, [
epoch,
transactionStep,
addLiquidityWrite,
baseToken,
Expand All @@ -311,7 +271,6 @@ const AddLiquidity = () => {

const handleSlippageChange = (newSlippage: number) => {
setSlippage(newSlippage);
console.log(`Slippage tolerance updated to: ${newSlippage}%`);
};

return (
Expand Down
8 changes: 7 additions & 1 deletion packages/app/src/lib/context/MarketProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ interface MarketContextType {
poolAddress: `0x${string}`;
pool: Pool | null;
collateralAssetDecimals: number;
epoch: number;
}

interface MarketProviderProps {
chainId: number;
address: string;
epoch: number;
children: ReactNode;
}

Expand All @@ -51,6 +53,7 @@ export const MarketContext = createContext<MarketContextType>({
prices: [],
pool: null,
poolAddress: '0x',
epoch: 0,
});

export const useUniswapPool = (chainId: number, poolAddress: `0x${string}`) => {
Expand Down Expand Up @@ -138,6 +141,7 @@ export const MarketProvider: React.FC<MarketProviderProps> = ({
chainId,
address,
children,
epoch,
}) => {
const [state, setState] = useState<MarketContextType>({
chain: undefined,
Expand All @@ -153,6 +157,7 @@ export const MarketProvider: React.FC<MarketProviderProps> = ({
prices: [],
pool: null,
poolAddress: '0x',
epoch: 0,
});

// Set chainId and address from the URL
Expand All @@ -169,8 +174,9 @@ export const MarketProvider: React.FC<MarketProviderProps> = ({
...currentState,
chain: chain[1] as any,
address,
epoch,
}));
}, [chainId, address]);
}, [chainId, address, epoch]);

const contractId = `${chainId}:${address}`;

Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/lib/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const Header = () => {
<Flex marginLeft="auto" gap={9} align="center" fontWeight="600">
{/* <Link href="/subscribe">Subscribe</Link> */}
{/* <Link href="/earn">Earn</Link> */}
<Link href={`/markets/13370:${FoilLocal.address}`}>Local Market</Link>
<Link href={`/markets/11155111:${FoilTestnet.address}`}>
<Link href={`/markets/13370:${FoilLocal.address}/1`}>
Local Market
</Link>
<Link href={`/markets/11155111:${FoilTestnet.address}/1`}>
Testnet Market
</Link>
<Link href="https://docs.foil.xyz">Docs</Link>
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/cannonfile.sepolia.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name="foil"
version="0.2"
version="0.3"

[var.settings]
owner="0xE006B58cA5aB7ba53863012dc3067A14b965C1da"
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/cannonfile.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name="foil"
version="0.2"
version="0.3"

[var.settings]
owner = "0xEB045D78d273107348b0300c01D29B7552D622ab"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"address": "0xfE6Cc6E08D279dc31e78DCEC9fd49d88E5B599Ed",
"address": "0x18E03c74Aa56dbb6c2255F2a7b87331621A1eB03",
"abi": [
{
"type": "function",
Expand Down Expand Up @@ -68,6 +68,45 @@
{
"type": "function",
"name": "getEpoch",
"inputs": [
{
"name": "id",
"type": "uint256",
"internalType": "uint256"
}
],
"outputs": [
{
"name": "startTime",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "endTime",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "pool",
"type": "address",
"internalType": "address"
},
{
"name": "ethToken",
"type": "address",
"internalType": "address"
},
{
"name": "gasToken",
"type": "address",
"internalType": "address"
}
],
"stateMutability": "view"
},
{
"type": "function",
"name": "getLatestEpoch",
"inputs": [],
"outputs": [
{
Expand Down Expand Up @@ -136,7 +175,7 @@
{
"name": "epochParams",
"type": "tuple",
"internalType": "struct Market.EpochParams",
"internalType": "struct IFoilStructs.EpochParams",
"components": [
{
"name": "baseAssetMinPriceTick",
Expand Down Expand Up @@ -256,7 +295,7 @@
{
"name": "epochParams",
"type": "tuple",
"internalType": "struct Market.EpochParams",
"internalType": "struct IFoilStructs.EpochParams",
"components": [
{
"name": "baseAssetMinPriceTick",
Expand Down Expand Up @@ -331,7 +370,7 @@
{
"name": "epochParms",
"type": "tuple",
"internalType": "struct Market.EpochParams",
"internalType": "struct IFoilStructs.EpochParams",
"components": [
{
"name": "baseAssetMinPriceTick",
Expand Down Expand Up @@ -379,6 +418,17 @@
"name": "EpochAlreadyStarted",
"inputs": []
},
{
"type": "error",
"name": "InvalidData",
"inputs": [
{
"name": "message",
"type": "string",
"internalType": "string"
}
]
},
{
"type": "error",
"name": "InvalidMarket",
Expand Down Expand Up @@ -407,12 +457,12 @@
],
"constructorArgs": [],
"linkedLibraries": {},
"deployTxnHash": "0xba4d6ddf7bfd95259df401cf76f672984116b3257c5a999827fe011771060a69",
"deployTxnBlockNumber": "6437196",
"deployTimestamp": "1722799824",
"deployTxnHash": "0x4bad60777f46c1274224ff69cd0e8df2c625d6e80041e8c6b101f764e7fb0216",
"deployTxnBlockNumber": "6455400",
"deployTimestamp": "1723048644",
"sourceName": "src/contracts/modules/EpochConfigurationModule.sol",
"contractName": "EpochConfigurationModule",
"deployedOn": "deploy.EpochConfigurationModule",
"gasUsed": 2257070,
"gasCost": "5075557086"
"gasUsed": 2338721,
"gasCost": "17266776845"
}
37 changes: 31 additions & 6 deletions packages/protocol/deployments/11155111/EpochLiquidityModule.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"address": "0x4b50D165D7D85C480374BaAC8547CF7D1953c205",
"address": "0x91d122d12863D7620EfFa9812Ad79Abf80f37A01",
"abi": [
{
"type": "function",
"name": "collectFees",
"inputs": [
{
"name": "epochId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "tokenId",
"type": "uint256",
Expand Down Expand Up @@ -34,6 +39,11 @@
"type": "tuple",
"internalType": "struct IFoilStructs.LiquidityPositionParams",
"components": [
{
"name": "epochId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "amountTokenA",
"type": "uint256",
Expand Down Expand Up @@ -100,6 +110,11 @@
"type": "function",
"name": "decreaseLiquidityPosition",
"inputs": [
{
"name": "epochId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "accountId",
"type": "uint256",
Expand Down Expand Up @@ -178,6 +193,11 @@
"type": "function",
"name": "getTokenAmounts",
"inputs": [
{
"name": "epochId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "collateralAmount",
"type": "uint256",
Expand Down Expand Up @@ -222,6 +242,11 @@
"type": "function",
"name": "increaseLiquidityPosition",
"inputs": [
{
"name": "epochId",
"type": "uint256",
"internalType": "uint256"
},
{
"name": "accountId",
"type": "uint256",
Expand Down Expand Up @@ -494,12 +519,12 @@
],
"constructorArgs": [],
"linkedLibraries": {},
"deployTxnHash": "0xcf391b1e589d12bdb9bc16f30a8490e15a279507411d74315b6a29b52f9ecaa2",
"deployTxnBlockNumber": "6437191",
"deployTimestamp": "1722799764",
"deployTxnHash": "0x1b8dde6061aa8128612949d63a8caede89ce27e6ec293f2b3b9c38f1a9a8281a",
"deployTxnBlockNumber": "6455394",
"deployTimestamp": "1723048560",
"sourceName": "src/contracts/modules/EpochLiquidityModule.sol",
"contractName": "EpochLiquidityModule",
"deployedOn": "deploy.EpochLiquidityModule",
"gasUsed": 2131396,
"gasCost": "4713106815"
"gasUsed": 2165858,
"gasCost": "16351223971"
}
Loading

0 comments on commit b314e11

Please sign in to comment.