Skip to content

Instantly share code, notes, and snippets.

@n2nco
Created May 6, 2023 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save n2nco/eabb6765ffbe895cfa8e2449fce40e2d to your computer and use it in GitHub Desktop.
Save n2nco/eabb6765ffbe895cfa8e2449fce40e2d to your computer and use it in GitHub Desktop.
Instadapp - DSA-Uniswap-Test
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const app = express();
app.use(cors());
app.use(express.json());
console.log( 'running server')
app.get('/api', async (req, res) => {
function runMiddleware(req, res, fn) {
return new Promise((resolve, reject) => {
fn(req, res, (result) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}
let network = 'polygon'
let chainId = 137
const Web3 = require('web3')
const DSA = require('dsa-connect');
const web3 = new Web3(new Web3.providers.HttpProvider('https://polygon-rpc.com'))
const dsa = new DSA({
web3: web3,
mode: "node",
privateKey: "PKEY_GOES_HERE"
},
137
);
console.log('running instadapp')
const nonce = await web3.eth.getTransactionCount('ACCOUNT_ADDR');
await dsa.build({
version: 2,
gasPrice: '237732701310', // in gwei - estimated gas price - required in nodejs
authority: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', //this seems to be required when using polygon - it indicates MATIC,
nonce: nonce
})
// const dsaAccounts = await dsa.getAccounts('0x5A68C82e4355968DB53177033ad3eDCfD62aCCcA')
const dsa_account = await dsa.internal.getAddress()
console.log('DSA accounts', dsaAccounts)
console.log('DSA account', dsa_account)
let spells = await dsa.Spell(); //
let matic_token = "0x0000000000000000000000000000000000001010" //0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0
let uni_token = "0xb33EaAd8d922B1083446DC23f610c2567fB5180f"
// let amount = web3.utils.toWei("1", "ether") //wtf?
const amountWithFee = web3.utils.toWei('1.09', 'ether')// is this just
const params = {
buyToken: uni_token,
sellToken: matic_token,
sellAmount: amountWithFee,
dsaAddress: dsa_account,
maxSlippage: 0.2,
fee: 3000,
// slippage: 1
}
// Using api to getch details like calldata, unitAmt.
//https://docs.instadapp.io/faq/connectors/calldata-param
let response
try {
response = await axios.get(
`https://api.instadapp.io/defi/${network}/uniswap/v3/swap/router`, { params: params }
);
response = response.data
// console.log(res)
} catch (e) {
console.log('error calling uniswap api: ', e)
}
let _buyAddr = response.buyToken.address
let _sellAddr = response.sellToken.address
let _fee = 9000 // 0.9%
let _unitAmt = response.unitAmt
let _buyAmt = response.buyTokenAmount
let _getId = 0
let _setId = 0
spells.add({
connector: "UNISWAP-V3-SWAP-A",
method: "buy",
args: [_buyAddr, _sellAddr, _fee, _unitAmt, _buyAmt, _getId, _setId]
});
let dsaId = dsaAccounts[0].id
await dsa.setInstance(dsaId)
// const calldata = await dsa.encodeCastABI(spells)
// const gas = await spells.estimateCastGas({ from: dsa_account })
try {
const txHash = await spells.cast({ from: dsa_account, gasPrice: response.gasPriceWei })
console.log(calldata)
debugger
} catch (e) {
console.log('error callin}g instadapp: ', e)
// const options = {
// method: 'POST',
// headers: { accept: 'application/json', 'content-type': 'application/json' },
// body: JSON.stringify({
// id: 1,
// jsonrpc: '2.0',
// method: 'alchemy_simulateAssetChanges',
// params: [demo_transaction ]
// })
// }
// await runMiddleware(req, res, cors); //not
try {
// let response = await fetch('https://eth-goerli.g.alchemy.com/v2/[ALCHEMY_KEY_HERE]', options)
// response = await response.json()
// console.log(response)
res.status(200).json({ spells: spells });
}
catch (e) {
console.log('error calling alchemy sim: ', e)
}
// }
res.json({ message: 'Hello from the API!' });
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment