Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
fix small bug in approvals
Browse files Browse the repository at this point in the history
small variable renaming changes and stuff
  • Loading branch information
CelestialCrafter committed Nov 14, 2023
1 parent efc45a9 commit 0008bba
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
19 changes: 10 additions & 9 deletions src/blockchain/approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const executeOOMNotification = async (id, address) => {
};

const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defaultBaseToken }) => {
const multiplier = 10;
const wallet = new Wallet(privateKey, provider);
const address = await wallet.getAddress();
console.log(`Running approvals for ${address}`);

const approvalAmount = fromReadableAmount(strengthToUSD * 10, tokens.usdc.decimals);
const approvalAmount = fromReadableAmount(strengthToUSD * multiplier, tokens.usdc.decimals);
const minimumEth = fromReadableAmount(0.005, tokens.wrapped.decimals);

const values = await getWorth(null, baseToken, approvalAmount, true);
Expand All @@ -60,14 +60,12 @@ const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defa
const renew = await Promise.all(
values.map(async data => ({
...data,
// 1.1 to leave a bit of extra room for error
renew:
(await data.contract.allowance(address, addresses.router)) <
BigInt(strengthToUSD) + minimumEth
(await data.contract.allowance(address, addresses.router)) < data.value / BigInt(multiplier)
}))
);

const transactions = await Promise.all(
const transactionDatas = await Promise.all(
renew
.map(data =>
data.renew ? data.contract.approve.populateTransaction(addresses.router, data.value) : null
Expand All @@ -87,13 +85,16 @@ const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defa
);

return Promise.allSettled(
transactions.map(transaction => repopulateAndSend(wallet, transaction))
transactionDatas.map(transaction => repopulateAndSend(wallet, transaction))
);
};

export default tradeData =>
Promise.allSettled(
export default async tradeData => {
const promises = await Promise.allSettled(
tradeData.map(({ id, strengthToUSD, privateKey }) =>
executeApproval(privateKey, { strengthToUSD, id })
)
);

return promises.filter(p => !(p.status === 'fulfilled' && p.value.length < 1));
};
23 changes: 13 additions & 10 deletions src/blockchain/check.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import { inspect } from 'util';

import { ALGORITHM_SERVER_URI, JWT_SECRET } from '$env/static/private';
import { toReadableAmount, defaultBaseToken } from '$lib/blockchain';
Expand All @@ -10,6 +11,7 @@ import executeTransactions from './trading';
import addWorths from './worth';

export default async redis => {
console.log('Running algorithm check');
try {
const token = jwt.sign({ event: 'auth' }, JWT_SECRET, { algorithm: 'HS256' });
await fetch(`${ALGORITHM_SERVER_URI}/internal_checker`, {
Expand Down Expand Up @@ -57,29 +59,30 @@ export default async redis => {
})
.filter(b => b);

const filteredTradeData = tradeData.filter(
data => data.strength > 0 && data.signal !== 'no_action'
);
const approvalResults = await executeApprovals(tradeData);
const transactionResults = await executeTransactions(filteredTradeData);
console.log('approvals', approvalResults[0].value);
console.log('transactions', transactionResults);
const transactionResults = await executeTransactions(
tradeData.filter(data => data.strength > 0 && data.signal !== 'no_action')
);
const worthsResults = await addWorths(tradeData);

const inspectOptions = { depth: 10, colors: true };
console.log('approvals', inspect(approvalResults, inspectOptions));
console.log('transactions', inspect(transactionResults, inspectOptions));
console.log('worths', inspect(worthsResults, inspectOptions));

// Update worths
const worthsResults = await addWorths(tradeData);
console.log('worths', worthsResults);
Bot.bulkWrite(
worthsResults
.filter(r => r.status === 'fulfilled')
.map(r => r.value)
.map(({ id, value }) => ({
.map(({ id, worth }) => ({
updateOne: {
filter: { _id: new mongoose.Types.ObjectId(id) },
update: {
$push: {
worth: {
timestamp: Date.now(),
value: toReadableAmount(value, defaultBaseToken.decimals)
value: toReadableAmount(worth, defaultBaseToken.decimals)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/blockchain/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const executeTransaction = async (
}
);

if (!route) throw new Error('No Route');

return routeTransaction(wallet, route);
};

Expand Down
11 changes: 7 additions & 4 deletions src/blockchain/worth.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ export const getWorth = async (

export default tradeData =>
Promise.allSettled(
tradeData.map(async ({ id, privateKey }) => ({
value: (await getWorth(await new Wallet(privateKey, provider).getAddress())).toString(),
id
}))
tradeData.map(async ({ id, privateKey }) => {
const address = await new Wallet(privateKey, provider).getAddress();
return {
worth: (await getWorth(address)).toString(),
id
};
})
);

0 comments on commit 0008bba

Please sign in to comment.