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

Commit

Permalink
update logginfg for algorithm check
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestialCrafter committed Dec 13, 2023
1 parent 40e427c commit 4ae825b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 101 deletions.
75 changes: 35 additions & 40 deletions src/blockchain/approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,41 @@ const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defa
return [];
}

const renew = await Promise.all(
values.map(async data => ({
...data,
renew:
(await data.contract.allowance(address, addresses.router)) < data.value / BigInt(multiplier)
}))
);

const transactionDatas = await Promise.all(
renew
.map(data =>
data.renew ? data.contract.approve.populateTransaction(addresses.router, data.value) : null
)
.filter(p => p)
);

renew.forEach(
async data =>
data.renew &&
log.debug(
{
address: await data.contract.getAddress(),
value: toReadableAmount(data.value, await data.contract.decimals())
},
'renewing'
)
);

return Promise.allSettled(
transactionDatas.map(transaction => repopulateAndSend(wallet, transaction))
);
return (
await Promise.allSettled(
values.map(async data => {
if (
!(await data.contract.allowance(address, addresses.router)) <
data.value / BigInt(multiplier)
)
return;

const tx = data.contract.approve.populateTransaction(addresses.router, data.value);
const transaction = await repopulateAndSend(wallet, tx);

log.debug(
{
address: await data.contract.getAddress(),
value: toReadableAmount(data.value, await data.contract.decimals()),
transaction,
id
},
'approving'
);

return transaction;
})
)
)
.map(p =>
p.status === 'fulfilled' ? p.value : log.warn({ error: p.reason, id }, 'approval error')
)
.filter(p => p);
};

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

return promises.filter(p => !(p.status === 'fulfilled' && p.value.length < 1));
};
64 changes: 11 additions & 53 deletions src/blockchain/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import addWorths from './worth';

export default async redis => {
log.info('algorithm check');

try {
const token = jwt.sign({ server: true }, JWT_SECRET, { algorithm: 'HS256' });
const response = await (
Expand Down Expand Up @@ -63,67 +64,24 @@ export default async redis => {
})
.filter(b => b);

const approvalResults = await executeApprovals(tradeData);
const transactionResults = await executeTransactions(
tradeData.filter(data => data.strength > 0 && data.signal !== 'no_action')
const tradeDataWithSignal = tradeData.filter(
data => data.strength > 0 && data.signal !== 'no_action'
);
await executeApprovals(tradeDataWithSignal);
await executeTransactions(tradeDataWithSignal);
const worthsResults = await addWorths(tradeData);

transactionResults.forEach(result =>
result.status === 'fulfilled'
? log.debug(
{
transaction: result.value.transaction,
id: result.value.id
},
'transaction'
)
: log.warn(
{
error: result.reason
},
'transaction error'
)
);

worthsResults.forEach(result =>
result.status === 'fulfilled'
? log.debug(
{
...result.value
},
'worth'
)
: log.warn(
{
error: result.reason
},
'worth error'
)
);

approvalResults.forEach(result =>
result.status === 'fulfilled'
? log.debug(
{
...result.value
},
'approval'
)
: log.warn(
{
error: result.reason
},
'approval error'
)
worthsResults.forEach(p =>
p.status === 'fulfilled'
? log.debug({ value: p.value.worth, id: p.value.id }, 'worth')
: log.warn({ error: p.reason }, 'worth error')
);

// Update worths
Bot.bulkWrite(
worthsResults
.filter(r => r.status === 'fulfilled')
.map(r => r.value)
.map(({ id, worth }) => ({
.filter(p => p.status === 'fulfilled')
.map(({ value: { id, worth } }) => ({
updateOne: {
filter: { _id: new mongoose.Types.ObjectId(id) },
update: {
Expand Down
18 changes: 10 additions & 8 deletions src/blockchain/trading.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const routeTransaction = async (wallet, route) => {

const executeTransaction = async (
privateKey,
{ baseAmount, baseToken = defaultBaseToken, modToken, action }
{ baseAmount, baseToken = defaultBaseToken, modToken, action, id }
) => {
const wallet = new Wallet(privateKey, provider);
log.debug({ address: await wallet.getAddress() }, 'running trades');
Expand All @@ -53,17 +53,19 @@ const executeTransaction = async (

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

return routeTransaction(wallet, route);
return routeTransaction(wallet, route)
.then(transaction => log.debug({ transaction, id }, 'trading'))
.catch(error => log.warn({ error, id }, 'trading error'));
};

export default tradeData =>
Promise.allSettled(
tradeData.map(({ id, signal: action, amount: baseAmount, privateKey }) => ({
transaction: executeTransaction(privateKey, {
tradeData.map(({ id, signal: action, amount: baseAmount, privateKey }) =>
executeTransaction(privateKey, {
modToken: tokens.wrapped,
baseAmount,
action
}),
id
}))
action,
id
})
)
);

0 comments on commit 4ae825b

Please sign in to comment.