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

Add support for multiple backends #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hooks.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createClient } from 'redis';
import { DB_URI, REDIS_URI } from '$env/static/private';
import { evaluateModelsWhenConnectionReady } from '$lib/models.server';

import executeAlgorithmCheck from './blockchain/check';
import executeAlgorithmCheck from './trading/check';

mongoose.connect(DB_URI);
const redis = createClient({ url: REDIS_URI });
Expand Down
12 changes: 4 additions & 8 deletions src/blockchain/check.js → src/trading/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { ALGORITHM_SERVER_URI, JWT_SECRET } from '$env/static/private';
import { toReadableAmount, defaultBaseToken } from '$lib/blockchain';
import { Bot } from '$lib/models.server';

import executeApprovals from './approval';
import executeTransactions from './trading';
import addWorths from './worth';
import uniswap from './uniswap';

export default async redis => {
console.log('Running algorithm check');
Expand Down Expand Up @@ -55,22 +53,20 @@ export default async redis => {

return {
id: bot._id,
privateKey: bot.privateKey(),
auth: { privateKey: bot.privateKey() },
amount: (10 || bot.strengthToUSD) * strength,
strengthToUSD: bot.strengthToUSD,
signal
};
})
.filter(b => b);

const approvalResults = await executeApprovals(tradeData);
const transactionResults = await executeTransactions(
const transactionResults = await uniswap.trade(
tradeData.filter(data => data.strength > 0 && data.signal !== 'no_action')
);
const worthsResults = await addWorths(tradeData);
const worthsResults = await uniswap.worth(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));

Expand Down
39 changes: 15 additions & 24 deletions src/blockchain/approval.js → src/trading/uniswap/approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const executeOOMNotification = async (id, address) => {
);
};

const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defaultBaseToken }) => {
export const executeApproval = async (
privateKey,
{ id, strengthToUSD, baseToken = defaultBaseToken }
) => {
const multiplier = 10;
const wallet = new Wallet(privateKey, provider);
const address = await wallet.getAddress();
Expand All @@ -65,36 +68,24 @@ const executeApproval = async (privateKey, { id, strengthToUSD, baseToken = defa
}))
);

const transactionDatas = await Promise.all(
const transactions = 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
? console.log(
`Renewing ${await data.contract.getAddress()} for ${toReadableAmount(
data.value,
await data.contract.decimals()
)}`
)
: null
);

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

export default async tradeData => {
const promises = await Promise.allSettled(
tradeData.map(({ id, strengthToUSD, privateKey }) =>
executeApproval(privateKey, { strengthToUSD, id })
)
renew.forEach(
async data =>
data.renew &&
console.log(
`Renewing ${await data.contract.getAddress()} for ${toReadableAmount(
data.value,
await data.contract.decimals()
)}`
)
);

return promises.filter(p => !(p.status === 'fulfilled' && p.value.length < 1));
return transactions.map(transaction => repopulateAndSend(wallet, transaction));
};
7 changes: 7 additions & 0 deletions src/trading/uniswap/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import worth from './worth';
import trade from './trade';

export default {
worth,
trade
};
21 changes: 14 additions & 7 deletions src/blockchain/trading.js → src/trading/uniswap/trade.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AlphaRouter, SwapType } from '@uniswap/smart-order-router';
import { tokens, fromReadableAmount, addresses, defaultBaseToken } from '$lib/blockchain';
import { providerUrl, repopulateAndSend } from '$lib/blockchain.server';
import { PUBLIC_CHAINID } from '$env/static/public';
import { executeApproval } from './approval';

const provider = new JsonRpcProvider(providerUrl);

Expand Down Expand Up @@ -57,11 +58,17 @@ const executeTransaction = async (

export default tradeData =>
Promise.allSettled(
tradeData.map(({ signal: action, amount: baseAmount, privateKey }) =>
executeTransaction(privateKey, {
modToken: tokens.wrapped,
baseAmount,
action
})
)
tradeData.map(async ({ id, signal: action, amount: baseAmount, strengthToUSD, auth }) => ({
transactions: [
await Promise.allSettled([
executeTransaction(auth.privateKey, {
modToken: tokens.wrapped,
baseAmount,
action
})
]),
...(await executeApproval(auth.privateKey, { strengthToUSD, id }))
],
id
}))
);
File renamed without changes.
Loading