Automation
In-Built
targetImpactIntervalPurchase
implements a strategy similar to TWAP for swapping large amount of crypto over an interval of time at a targeted price impact in uniswap
The below will return a TIIPExecutor Object with which you can trigger to start the strategy and listen to events
let TIIPExecutor = unifi.uniswapTools.targetImpactIntervalPurchase({
walletAddress:wallet.address,
sellTokenAddress:'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
buyTokenAddress:'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
maximumPriceImpact:0.05,
priceImpactTolerance:0.01,
desiredChunkSize:"20",
targetQuantity:"2000",
interval:60000,
lowerBoundChunkSize:"10",
chainId:1,
});
The following command with start the strategy
TIIPExecutor.runTIPExecutor()
The following is essential to listen to events which will contain the required transaction to be sent from your wallet at every interval which you have specified
TIIPExecutor.TIPExecutorEvents.on('executionOutput',(response)=>{
console.log('response',response);
//wallet.sendTransaction(response.unsignedTxnObject);
})
Full Implementation
const Unifi = require('unifi-sdk');
let apiKey = 'your-api-key';
let unifi = Unifi(apiKey);
const fs = require('fs');
const ethers = require('ethers');
let provider = new ethers.providers.JsonRpcProvider('your-provider-url');
let wallet = new ethers.Wallet('your-private-key',provider);
let TIPExecutor = unifi.uniswapTools.targetImpactIntervalPurchase({
walletAddress:wallet.address,
sellTokenAddress:'0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
buyTokenAddress:'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
maximumPriceImpact:0.05,
priceImpactTolerance:0.01,
desiredChunkSize:"20",
targetQuantity:"2000",
interval:60000,
lowerBoundChunkSize:"10",
chainId:1,
});
const time = new Date().getTime();
TIPExecutor.runTIPExecutor()
TIPExecutor.TIPExecutorEvents.on('executionOutput',async(response)=>{
console.log('response',response);
let sentTxn = await wallet.sendTransaction(response.unsignedTxnObject);
let receipt = await sentTxn.wait();
//writing to a file to log events for reference
fs.appendFile('executionHistory_'+time+'.txt',JSON.stringify(response)+'\n', (err)=>{
if(err) throw err;
})
})
You can check out the code for the strategy at
unifi-sdk/tools/uniswap/TIPExecutor.js