Skip to content

Commit

Permalink
new dca strategy price-based
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Jun 10, 2024
1 parent fee068e commit 8abb5b5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 124 deletions.
92 changes: 29 additions & 63 deletions src/api/uniswap/actions/buildSwapUniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function buildSwapUniswap(
if (debug) console.log('::API::UNISWAP::BALANCE', Number(tokenABalance))

const allowanceAgent = await tokenAContract?.allowance(
swap.address,
walletAddress,
agentAddress
)

Expand Down Expand Up @@ -153,37 +153,9 @@ export async function buildSwapUniswap(
if (debug) console.log('::API::UNISWAP::FOUND_ALLOWANCE_SENDER_AGENT')
}

// Transfer tokens from Sender to Agent
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

const dataTransferFromSenderToAgent =
tokenAContract.interface.encodeFunctionData('transferFrom', [
swap.address,
agentAddress,
adjAmount,
])

const transferFromSenderToAgent = {
to: tokenAAddress,
value: 0,
data: dataTransferFromSenderToAgent,
}

if (transferFromSenderToAgent)
if (debug) console.log('::API::UNISWAP::BUILD_TRANSFER_FROM_SENDER_AGENT')

Calldatas.push(transferFromSenderToAgent)

// Encode Swap tx to Uni Router
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// const quote = await quotePair(
// tokenAAddress,
// tokenBAddress,
// Number(swap.chainId)
// );
// const slippageTolerance = swap.slippage;

const currency = {
address: tokenBAddress,
Expand All @@ -199,8 +171,6 @@ export async function buildSwapUniswap(
name: await tokenAContract.name(),
}

console.log(currency, currencyAmount)

const bestRoute = await route({
chainId: Number(137),
recipient: agentAddress,
Expand All @@ -211,8 +181,6 @@ export async function buildSwapUniswap(
slippage: swaps[0].slippage,
})

console.log(bestRoute)

const swapMultiAgentToRouter = {
to: bestRoute.methodParameters.to,
value: bestRoute.methodParameters.value,
Expand All @@ -221,7 +189,7 @@ export async function buildSwapUniswap(

const allowanceAgentToUniversalRouter = await tokenAContract?.allowance(
agentAddress,
bestRoute.methodParameters.to
uniRouter
)

if (adjAmount.gt(allowanceAgentToUniversalRouter)) {
Expand All @@ -230,7 +198,7 @@ export async function buildSwapUniswap(

const calldataApproveAgentToRouter =
tokenAContract.interface.encodeFunctionData('approve', [
bestRoute.methodParameters.to,
uniRouter,
ethers.constants.MaxUint256,
])

Expand All @@ -240,49 +208,47 @@ export async function buildSwapUniswap(
data: calldataApproveAgentToRouter,
}

ApprovalsAgent.push(approvalAgentToRouter)
Calldatas.push(approvalAgentToRouter)
} else {
if (debug)
console.log('::API::UNISWAP::FOUND_ALLOWANCE_AGENT_UNIVERSAL_ROUTER')
}

const allowanceAgentToUniversalRouter2 = await tokenAContract?.allowance(
uniRouter,
bestRoute.methodParameters.to
)

if (adjAmount.gt(allowanceAgentToUniversalRouter2)) {
if (debug)
console.log('::API::UNISWAP::MISSING_ALLOWANCE_AGENT_UNI_ROUTER')
for (let i = 0; i < bestRoute.route.length; i++) {
bestRoute.route[i].tokenPath.forEach(token =>
tokensSet.add(token.address)
)
}

const calldataApproveAgentToRouter2 =
tokenAContract.interface.encodeFunctionData('approve', [
uniRouter,
ethers.constants.MaxUint256,
])
// Transfer tokens from Sender to Agent
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

const approvalAgentToRouter2 = {
to: tokenAAddress,
value: 0,
data: calldataApproveAgentToRouter2,
}
const dataTransferFromSenderToAgent =
tokenAContract.interface.encodeFunctionData('transferFrom', [
walletAddress,
agentAddress,
adjAmount.add(BigNumber.from(100)),
])

ApprovalsAgent.push(approvalAgentToRouter2)
} else {
if (debug) console.log('::API::UNISWAP::FOUND_ALLOWANCE_AGENT_UNI_ROUTER')
const transferFromSenderToAgent = {
to: tokenAAddress,
value: 0,
data: dataTransferFromSenderToAgent,
}

for (let i = 0; i < bestRoute.route.length; i++) {
bestRoute.route[i].tokenPath.forEach(token =>
tokensSet.add(token.address)
)
if (transferFromSenderToAgent) {
if (debug) console.log('::API::UNISWAP::BUILD_TRANSFER_FROM_SENDER_AGENT')

Calldatas.push(transferFromSenderToAgent)
}

if (swapMultiAgentToRouter)
if (swapMultiAgentToRouter) {
if (debug)
console.log('::API::UNISWAP::BUILD_AGENT_EXACT_INPUT_TO_UNIROUTER')

Calldatas.push(swapMultiAgentToRouter)
Calldatas.push(swapMultiAgentToRouter)
}
}

TokensReturn = Array.from(tokensSet)
Expand Down
16 changes: 8 additions & 8 deletions src/api/utils/uniswap/bestQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function isSwapPossible(

async function getPoolData(poolContract, minRequiredLiquidity) {
const slot0 = await poolContract.slot0()
const currentSqrtPriceX96 = slot0.sqrtPriceX96
//const currentSqrtPriceX96 = slot0.sqrtPriceX96
const currentTick = slot0.tick
const tickSpacing = await poolContract.tickSpacing()
const desiredTickLower = currentTick - (currentTick % tickSpacing)
Expand Down Expand Up @@ -264,7 +264,7 @@ export async function getBestQuote(
const tokenAContract = new Contract(tokenAAddress, erc20Abi, wallet)
const tokenADecimals = await tokenAContract.decimals()
const tokenBAddress = reverse ? token0 : token1
const adjAmount = getAdjAmount(amount, tokenADecimals)
const adjAmount = getAdjAmount(Number(amount).toPrecision(18), tokenADecimals)

const bestQuote = await getBestQuoteForSwapPath(
factoryContract,
Expand Down Expand Up @@ -301,7 +301,7 @@ interface TradeRequest {
export async function route(tradeRequest: TradeRequest) {
console.log('::API::UNISWAP::ROUTE')

const router = new AlphaRouter({
const alphaRouter = new AlphaRouter({
chainId: tradeRequest.chainId,
provider: new ethers.providers.JsonRpcProvider(
NETWORKS[tradeRequest.chainId]
Expand All @@ -312,12 +312,12 @@ export async function route(tradeRequest: TradeRequest) {
tradeRequest.currencyAmount,
tradeRequest.chainId
)

const currency = parseToken(tradeRequest.currency, tradeRequest.chainId)

const formatedSlippage = tradeRequest.slippage / 100 // 5000/100 = 50
const SLIPPAGE = new Percent(formatedSlippage, 10_000) // Correct 15%
const SLIPPAGE = new Percent(tradeRequest.slippage, 10_000)

const routing = router.route(
const routing = alphaRouter.route(
CurrencyAmount.fromRawAmount(currencyAmount, Number(tradeRequest.amount)),
currency,
TradeType.EXACT_INPUT,
Expand All @@ -329,8 +329,8 @@ export async function route(tradeRequest: TradeRequest) {
//deadlineOrPreviousBlockhash: Math.floor(Date.now() / 1000) + 360,
},
{
distributionPercent: 2,
maxSplits: 3,
distributionPercent: 5,
maxSplits: 2,
protocols: [Protocol.V3, Protocol.V2, Protocol.MIXED],
}
)
Expand Down
2 changes: 1 addition & 1 deletion src/core/strategies/loops/dca/price-based/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"SELECTED_CHAINID": 137,
"FROMADDRESS": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
"TOADDRESS": "0xb33eaad8d922b1083446dc23f610c2567fb5180f",
"SLIPPAGE": 1000
"SLIPPAGE": 500
}
Loading

0 comments on commit 8abb5b5

Please sign in to comment.