-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from dezswap/feat/pool-simulation
feat: pool simulation
- Loading branch information
Showing
8 changed files
with
1,211 additions
and
788 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { useLCDClient } from "@xpla/wallet-provider"; | ||
import { Pool } from "types/pair"; | ||
import { queryMessages } from "utils/dezswap"; | ||
|
||
const usePool = (contractAddress?: string) => { | ||
const lcd = useLCDClient(); | ||
const [pool, setPool] = useState<Pool>(); | ||
|
||
useEffect(() => { | ||
const fetchPool = async () => { | ||
if (!contractAddress) { | ||
setPool(undefined); | ||
return; | ||
} | ||
const res = await lcd?.wasm.contractQuery<Pool>( | ||
contractAddress, | ||
queryMessages.getPool(), | ||
); | ||
setPool(res); | ||
}; | ||
|
||
fetchPool(); | ||
}, [contractAddress, lcd]); | ||
|
||
return useMemo(() => pool, [pool]); | ||
}; | ||
|
||
export default usePool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,92 @@ | ||
const useSimulate = () => { | ||
/* TODO: implement */ | ||
import useAssets from "hooks/useAssets"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import usePool from "hooks/usePool"; | ||
import { Numeric } from "@xpla/xpla.js"; | ||
|
||
export interface ProvideSimulationResult { | ||
estimatedAmount: string; | ||
poolAssets: { address: string; amount: string }[]; | ||
percentageOfShare: string; | ||
share: string; | ||
} | ||
|
||
const useSimulate = ( | ||
pairAddress: string, | ||
assetAddress: string, | ||
assetAmount: string, | ||
) => { | ||
const pool = usePool(pairAddress); | ||
const { getAsset } = useAssets(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isFailed, setIsFailed] = useState(false); | ||
const [result, setResult] = useState<ProvideSimulationResult>(); | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
setIsFailed(false); | ||
const fetchProvideInfos = async () => { | ||
try { | ||
if (pool) { | ||
const poolAssets = pool.assets.map((asset) => ({ | ||
address: | ||
"token" in asset.info | ||
? asset.info.token.contract_addr | ||
: asset.info.native_token.denom, | ||
amount: asset.amount, | ||
})); | ||
|
||
if (poolAssets.length > 1) { | ||
const [poolAsset1, poolAsset2] = poolAssets.sort((a) => | ||
a?.address === assetAddress ? -1 : 1, | ||
); | ||
|
||
const depositAmount = Numeric.parse(assetAmount); | ||
if (Numeric.parse(pool.total_share).gt(0) && depositAmount.gt(0)) { | ||
const share = depositAmount | ||
.mul(pool.total_share) | ||
.div(poolAsset1.amount) | ||
.ceil(); | ||
const estimated = share | ||
.mul(poolAsset2.amount) | ||
.div(pool.total_share) | ||
.ceil(); | ||
|
||
setResult({ | ||
estimatedAmount: estimated.toString(), | ||
poolAssets, | ||
percentageOfShare: share | ||
.mul(100) | ||
.div(share.plus(pool.total_share)) | ||
.toString(), | ||
share: share.toString(), | ||
}); | ||
return; | ||
} | ||
} | ||
} | ||
setIsFailed(true); | ||
setResult(undefined); | ||
} catch (error) { | ||
console.error(error); | ||
setIsFailed(true); | ||
setResult(undefined); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
const timerId = setTimeout(() => { | ||
fetchProvideInfos(); | ||
}, 1000); | ||
|
||
return () => { | ||
clearTimeout(timerId); | ||
}; | ||
}, [assetAddress, getAsset, assetAmount]); | ||
|
||
return useMemo( | ||
() => ({ ...result, isLoading, isFailed }), | ||
[isFailed, isLoading, result], | ||
); | ||
}; | ||
|
||
export default useSimulate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,94 @@ | ||
const useSimulate = () => { | ||
/* TODO: implement */ | ||
import { Numeric } from "@xpla/xpla.js"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import usePool from "hooks/usePool"; | ||
import { useNetwork } from "hooks/useNetwork"; | ||
import useAssets from "hooks/useAssets"; | ||
|
||
export interface WithdrawSimulationResult { | ||
estimatedAmount1: string; | ||
estimatedAmount2: string; | ||
poolAssets: { address: string; amount: string }[]; | ||
percentageOfShare: string; | ||
} | ||
|
||
const useSimulate = ( | ||
pairAddress: string, | ||
liquidityTokenAddress: string, | ||
amount: string, | ||
) => { | ||
const { name: networkName } = useNetwork(); | ||
const pool = usePool(pairAddress); | ||
const { getAsset } = useAssets(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isFailed, setIsFailed] = useState(false); | ||
const [result, setResult] = useState<WithdrawSimulationResult>(); | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
setIsFailed(false); | ||
let isAborted = false; | ||
|
||
const fetchPool = async () => { | ||
try { | ||
if (isAborted) { | ||
return; | ||
} | ||
|
||
if (pool) { | ||
const poolAssets = pool.assets.map((asset) => ({ | ||
address: | ||
"token" in asset.info | ||
? asset.info.token.contract_addr | ||
: asset.info.native_token.denom, | ||
amount: asset.amount, | ||
})); | ||
|
||
if ( | ||
poolAssets && | ||
poolAssets.length > 1 && | ||
Numeric.parse(pool.total_share).gt(0) && | ||
Numeric.parse(amount).gt(0) | ||
) { | ||
const amountInNumeric = Numeric.parse(amount); | ||
setResult({ | ||
estimatedAmount1: amountInNumeric | ||
.mul(poolAssets[0].amount) | ||
.div(pool.total_share) | ||
.floor() | ||
.toString(), | ||
estimatedAmount2: amountInNumeric | ||
.mul(poolAssets[1].amount) | ||
.div(pool.total_share) | ||
.floor() | ||
.toString(), | ||
poolAssets, | ||
percentageOfShare: amountInNumeric.mul(100).toString(), | ||
}); | ||
return; | ||
} | ||
} | ||
setIsFailed(true); | ||
setResult(undefined); | ||
} catch (error) { | ||
console.log(error); | ||
setIsFailed(true); | ||
setResult(undefined); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
fetchPool(); | ||
|
||
return () => { | ||
isAborted = true; | ||
}; | ||
}, [pairAddress, liquidityTokenAddress, amount, getAsset, networkName, pool]); | ||
|
||
return useMemo( | ||
() => ({ ...result, isLoading, isFailed }), | ||
[isFailed, isLoading, pool, result], | ||
); | ||
}; | ||
|
||
export default useSimulate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.