Skip to content

Commit

Permalink
edit local contracts with remote npm baluni-contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
scobru committed Jun 9, 2024
1 parent b03ae35 commit e5ce2b1
Show file tree
Hide file tree
Showing 14 changed files with 3,542 additions and 422 deletions.
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "baluni",
"version": "2.5.8",
"version": "2.6.0",
"description": "Uniswap v3 rebalance and forecast CLI tool",
"files": [
"dist"
Expand Down Expand Up @@ -40,26 +40,27 @@
"dependencies": {
"@tensorflow/tfjs": "^4.15.0",
"@types/node": "^20.10.5",
"@uniswap/sdk-core": "^4.0.10",
"@uniswap/permit2-sdk": "^1.2.0",
"@uniswap/router-sdk": "1.7.1",
"@uniswap/sdk": "^3.0.3",
"@uniswap/sdk-core": "^4.0.10",
"@uniswap/smart-order-router": "3.19.3",
"asciichart": "^1.5.25",
"axios": "^1.6.5",
"baluni-contracts": "^2.6.5",
"bignumber.js": "^9.1.2",
"chakl": "^0.0.1-security",
"cli-chart": "^0.3.1",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"ethereum-abi-types-generator": "^1.3.4",
"ethers": "^5.7.2",
"express": "^4.18.3",
"jsbi": "3.2.5",
"prettier": "^2.8.4",
"trading-indicator": "^2.0.4",
"@uniswap/permit2-sdk": "^1.2.0",
"@uniswap/router-sdk": "1.7.1",
"bignumber.js": "^9.1.2",
"cors": "^2.8.5",
"ethereum-abi-types-generator": "^1.3.4"
"trading-indicator": "^2.0.4"
},
"repository": {
"type": "git",
Expand Down
6 changes: 3 additions & 3 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RouterABI from './abis/infra/Router.json'
import AgentABI from './abis/infra/Agent.json'
import RegistryABI from './abis/infra/Registry.json'
import RouterABI from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'
import AgentABI from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Agent.sol/BaluniV1Agent.json'
import RegistryABI from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'
import OffChainOracleAbi from './abis/1inch/OffChainOracle.json'
export { RouterABI, AgentABI, OffChainOracleAbi, RegistryABI }
export { buildSwapOdos } from './odos'
Expand Down
21 changes: 12 additions & 9 deletions src/api/odos/actions/buildSwapOdos.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Contract, Wallet, ethers } from 'ethers'
import erc20Abi from '../../abis/common/ERC20.json'
import routerAbi from '../../abis/infra/Router.json'
import factoryAbi from '../../abis/infra/Factory.json'
import registryAbi from '../../abis/infra/Registry.json'
import { PROTOCOLS, INFRA } from '../../constants'
import erc20Abi from 'baluni-contracts/abis/common/ERC20.json'
import routerAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'
import factoryAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1AgentFactory.sol/BaluniV1AgentFactory.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'
import env from 'dotenv'
import { ODOS_ASSEMBLE, ODOS_QUOTE } from '../../constants'
import { ODOS_ASSEMBLE, ODOS_QUOTE, PROTOCOLS, INFRA } from '../../constants'
env.config()

export async function buildSwapOdos(
Expand All @@ -21,17 +20,21 @@ export async function buildSwapOdos(
) {
console.log('Building Batch Swap tx')

const registry = new Contract(INFRA[chainId].REGISTRY, registryAbi, wallet)
const registry = new Contract(
INFRA[chainId].REGISTRY,
registryAbi.abi,
wallet
)

const odosRouter = String(PROTOCOLS[chainId]['odos'].ROUTER)
const infraRouter = await registry.getBaluniRouter()
const agentFactory = await registry.getBaluniAgentFactory()

const InfraRouterContract = new Contract(infraRouter, routerAbi, wallet)
const InfraRouterContract = new Contract(infraRouter, routerAbi.abi, wallet)
let agentAddress = await InfraRouterContract?.getAgentAddress(sender)

if (agentAddress === ethers.constants.AddressZero) {
const factoryCtx = new Contract(agentFactory, factoryAbi, wallet)
const factoryCtx = new Contract(agentFactory, factoryAbi.abi, wallet)
const tx = await factoryCtx.getOrCreateAgent(sender)
tx.wait()
agentAddress = await InfraRouterContract?.getAgentAddress(sender)
Expand Down
20 changes: 12 additions & 8 deletions src/api/uniswap/actions/buildSwap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BigNumberish, BigNumber, Contract, Wallet, ethers } from 'ethers'
import erc20Abi from '../../abis/common/ERC20.json'
import swapRouterAbi from '../../abis/uniswap/SwapRouter.json'
import routerAbi from '../../abis/infra/Router.json'
import erc20Abi from 'baluni-contracts/abis/common/ERC20.json'
import routerAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'
import factoryAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1AgentFactory.sol/BaluniV1AgentFactory.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'
import quoterAbi from '../../abis/uniswap/Quoter.json'
import factoryAbi from '../../abis/uniswap/Factory.json'
import registryAbi from '../../abis/infra/Registry.json'
import swapRouterAbi from '../../abis/uniswap/SwapRouter.json'

import { PROTOCOLS, INFRA } from '../../constants'
import env from 'dotenv'
Expand Down Expand Up @@ -33,10 +33,14 @@ export async function buildSwap(
const uniRouter = String(PROTOCOLS[chainId][protocol].ROUTER)
const swapRouterContract = new Contract(uniRouter, swapRouterAbi, wallet)

const registry = new Contract(INFRA[chainId].REGISTRY, registryAbi, wallet)
const registry = new Contract(
INFRA[chainId].REGISTRY,
registryAbi.abi,
wallet
)
const infraRouter = await registry.getBaluniRouter()

const InfraRouterContract = new Contract(infraRouter, routerAbi, wallet)
const InfraRouterContract = new Contract(infraRouter, routerAbi.abi, wallet)
const agentAddress = await InfraRouterContract.getAgentAddress(address)
const tokenAAddress = reverse == true ? token1 : token0
const tokenBAddress = reverse == true ? token0 : token1
Expand Down Expand Up @@ -84,7 +88,7 @@ export async function buildSwap(
}

const quoterContract = new Contract(quoter, quoterAbi, wallet)
const factoryContract = new Contract(factory, factoryAbi, wallet)
const factoryContract = new Contract(factory, factoryAbi.abi, wallet)

// const quote = await quotePair(
// tokenAAddress,
Expand Down
14 changes: 7 additions & 7 deletions src/api/uniswap/actions/buildSwapUniswap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BigNumber, Contract, Wallet, ethers } from 'ethers'
import erc20Abi from '../../abis/common/ERC20.json'
import { PROTOCOLS, INFRA } from '../../constants'
import env from 'dotenv'
import { getAdjAmount, route } from '../../utils/uniswap/bestQuote'
import { TradeType } from '@uniswap/sdk'
import routerAbi from '../../abis/infra/Router.json'
import factoryAbi from '../../abis/infra/Factory.json'
import registryAbi from '../../abis/infra/Registry.json'
import erc20Abi from 'baluni-contracts/abis/common/ERC20.json'
import routerAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'
import factoryAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1AgentFactory.sol/BaluniV1AgentFactory.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'

env.config()

Expand Down Expand Up @@ -34,14 +34,14 @@ export async function buildSwapUniswap(

const registry = new Contract(
INFRA[swaps[0].chainId].REGISTRY,
registryAbi,
registryAbi.abi,
wallet
)

const infraRouter = await registry.getBaluniRouter()
const agentFactory = await registry.getBaluniAgentFactory()

const InfraRouterContract = new Contract(infraRouter, routerAbi, wallet)
const InfraRouterContract = new Contract(infraRouter, routerAbi.abi, wallet)
const uniRouter = String(protocol.ROUTER)

const Approvals = []
Expand All @@ -59,7 +59,7 @@ export async function buildSwapUniswap(
)

if (agentAddress === ethers.constants.AddressZero) {
const factoryCtx = new Contract(agentFactory, factoryAbi, wallet)
const factoryCtx = new Contract(agentFactory, factoryAbi.abi, wallet)
const tx = await factoryCtx.getOrCreateAgent(wallet.address)
tx.wait()
agentAddress = await InfraRouterContract?.getAgentAddress(swaps[0].address)
Expand Down
16 changes: 10 additions & 6 deletions src/api/yearn/actions/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import YEARN_VAULT_ABI from '../../abis/yearn/YearnVault.json'
import ERC20ABI from '../../abis/common/ERC20.json'
import ERC20ABI from 'baluni-contracts/abis/common/ERC20.json'
import { BigNumber, Contract, ethers } from 'ethers'
import { INFRA, NETWORKS } from '../../constants'
import routerAbi from '../../abis/infra/Router.json'
import registryAbi from '../../abis/infra/Registry.json'
import routerAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'

export async function depositToYearn(
wallet: ethers.Wallet,
Expand All @@ -18,12 +18,16 @@ export async function depositToYearn(
const vault = new ethers.Contract(pool, YEARN_VAULT_ABI, wallet)

const tokenBalance = await token.balanceOf(receiver)
const registry = new Contract(INFRA[chainId].REGISTRY, registryAbi, wallet)
const registry = new Contract(
INFRA[chainId].REGISTRY,
registryAbi.abi,
wallet
)
const infraRouter = await registry.getBaluniRouter()

const InfraRouterContract = new ethers.Contract(
infraRouter,
routerAbi,
routerAbi.abi,
wallet
)
const agentAddress = await InfraRouterContract?.getAgentAddress(receiver)
Expand Down Expand Up @@ -159,7 +163,7 @@ export async function depositToYearnBatched(
const infraRouter = String(INFRA[chainId].ROUTER)
const InfraRouterContract = new ethers.Contract(
infraRouter,
routerAbi,
routerAbi.abi,
wallet
)
const agentAddress = await InfraRouterContract?.getAgentAddress(receiver)
Expand Down
14 changes: 9 additions & 5 deletions src/api/yearn/actions/redeem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import YEARN_VAULT_ABI from '../../abis/yearn/YearnVault.json'
import { BigNumber, Contract, ContractInterface, ethers } from 'ethers'
import { INFRA, NETWORKS } from '../../constants'
import routerAbi from '../../abis/infra/Router.json'
import registryAbi from '../../abis/infra/Registry.json'
import routerAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'

export async function redeemFromYearn(
wallet: ethers.Wallet,
Expand All @@ -24,11 +24,15 @@ export async function redeemFromYearn(
throw new Error('::API:: Insufficient balance')
}

const registry = new Contract(INFRA[chainId].REGISTRY, registryAbi, wallet)
const registry = new Contract(
INFRA[chainId].REGISTRY,
registryAbi.abi,
wallet
)
const infraRouter = await registry.getBaluniRouter()
const InfraRouterContract = new ethers.Contract(
infraRouter,
routerAbi,
routerAbi.abi,
wallet
)
const agentAddress = await InfraRouterContract?.getAgentAddress(receiver)
Expand Down Expand Up @@ -186,7 +190,7 @@ export async function redeemFromYearnBatched(
const infraRouter = String(INFRA[chainId].ROUTER)
const InfraRouterContract = new ethers.Contract(
infraRouter,
routerAbi,
routerAbi.abi,
wallet
)

Expand Down
8 changes: 4 additions & 4 deletions src/core/common/uniswap/batchSwap.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import infraRouterAbi from '../../../api/abis/infra/Router.json'
import { Contract, ethers } from 'ethers'
import { DexWallet } from '../../utils/web3/dexWallet'
import { waitForTx } from '../../utils/web3/networkUtils'
import { INFRA } from '../../../api/constants'
import { NETWORKS, BASEURL } from '../../../api/constants'
import { buildSwapUniswap } from '../../../api/uniswap/actions/buildSwapUniswap'
import registryAbi from '../../../api/abis/infra/Registry.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'
import infraRouterAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'

// TESTING
// import { buildSwapUniswap } from '../../../../baluni-api/dist/uniswap/actions/buildSwapUniswap'
Expand Down Expand Up @@ -36,12 +36,12 @@ export async function batchSwap(

const registry = new Contract(
INFRA[swaps[0].chainId].REGISTRY,
registryAbi,
registryAbi.abi,
wallet
)

const routerAddress = await registry.getBaluniRouter()
const router = new ethers.Contract(routerAddress, infraRouterAbi, wallet)
const router = new ethers.Contract(routerAddress, infraRouterAbi.abi, wallet)

const allApprovals: unknown[] = []
const allApprovalsAgent: unknown[] = []
Expand Down
33 changes: 6 additions & 27 deletions src/core/common/uniswap/swap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import infraRouterAbi from '../../../api/abis/infra/Router.json'
import { ethers, Contract } from 'ethers'
import { DexWallet } from '../../utils/web3/dexWallet'
import { waitForTx } from '../../utils/web3/networkUtils'
import { NETWORKS, INFRA, BASEURL } from '../../../api/constants'
import { buildSwap } from '../../../api/uniswap/actions/buildSwap'
import registryAbi from '../../../api/abis/infra/Registry.json'
import registryAbi from 'baluni-contracts/artifacts/contracts/registry/BaluniV1Registry.sol/BaluniV1Registry.json'
import infraRouterAbi from 'baluni-contracts/artifacts/contracts/orchestators/BaluniV1Router.sol/BaluniV1Router.json'

export async function swap(
dexWallet: DexWallet,
Expand All @@ -19,31 +19,17 @@ export async function swap(
const provider = new ethers.providers.JsonRpcProvider(NETWORKS[chainId])
const registry = new Contract(
INFRA[chainId].REGISTRY,
registryAbi,
registryAbi.abi,
dexWallet.wallet
)

const routerAddress = await registry.getBaluniRouter()
const wallet = dexWallet.wallet
const router = new ethers.Contract(
routerAddress,
infraRouterAbi,
infraRouterAbi.abi,
dexWallet.wallet
)
const gasLimit = 8000000
const gas = await provider?.getGasPrice()

// METHOD 1
//-------------------------------------------------------------------------------------
// const url = `${BASEURL}/swap/${dexWallet.walletAddress}/${token0}/${token1}/${reverse}/${protocol}/${chainId}/${amount}`;
// const url = `http://localhost:3001/swap/${dexWallet.walletAddress}/${token0}/${token1}/${reverse}/${protocol}/${chainId}/${amount}`;
// const response = await fetch(url, {
// method: "POST",
// });
// if (!response.ok) {
// throw new Error(`HTTP error! status: ${response.status}`);
// }
// const data = await response.json().then(data => data);

// METHOD 2
//-------------------------------------------------------------------------------------
Expand Down Expand Up @@ -107,11 +93,7 @@ export async function swap(
console.log('Sending calldatasArray')
const simulationResult: unknown = await router?.callStatic?.execute(
calldatasArray,
TokensReturn,
{
gasLimit: gasLimit,
gasPrice: gas,
}
TokensReturn
)
console.log('Simulation successful:', await simulationResult)

Expand All @@ -120,10 +102,7 @@ export async function swap(
return
}

const tx = await router.execute(calldatasArray, TokensReturn, {
gasLimit: gasLimit,
gasPrice: gas,
})
const tx = await router.execute(calldatasArray, TokensReturn)
const txReceipt = await waitForTx(
provider,
await tx?.hash,
Expand Down
11 changes: 10 additions & 1 deletion src/core/strategies/loops/dca/price-based/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const offChainOracleAddress = '0x0AdDd25a91563696D8567Df78D5A01C9a991F9B8' // Po
let transactionHistory: { buyPrice: number; amount: BigNumber }[] = []
let startPrice: number | null = null
let lastPriceStep: number | null = null
const priceThresholdPercentage = 0.05 // Define the percentage threshold (e.g., 5%)

// 🔍 Function to get the initial price
const getInitialPrice = async () => {
Expand Down Expand Up @@ -225,6 +226,14 @@ const executeDCA = async () => {
toTokenMetadata,
CurrentETHPrice
)

// Check if the current price exceeds the threshold percentage compared to the start price
if (CurrentETHPrice > startPrice! * (1 + priceThresholdPercentage)) {
startPrice = CurrentETHPrice
console.log(
`🔄 Start price updated to ${startPrice} due to significant increase`
)
}
}

// 🧮 Function to calculate profit
Expand Down Expand Up @@ -322,7 +331,7 @@ const mainLoop = async () => {
while (shouldContinue) {
await executeDCA()
console.log('⏳ Waiting for the next interval...')
await sleep(3600000) // ⏲️ Wait for 1 hour (3600000 milliseconds)
await sleep(60000) // ⏲️ Wait for 1 hour (3600000 milliseconds)
}
}

Expand Down
Loading

0 comments on commit e5ce2b1

Please sign in to comment.