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

Commit

Permalink
convert current blockchain functions to backend api
Browse files Browse the repository at this point in the history
  • Loading branch information
CelestialCrafter committed Dec 7, 2023
1 parent c65475c commit a68cc93
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
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
10 changes: 3 additions & 7 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 @@ -63,14 +61,12 @@ export default async redis => {
})
.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
};
19 changes: 14 additions & 5 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,19 @@ 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, privateKey }) => ({
transactions: [
await Promise.allSettled([
executeTransaction(privateKey, {
modToken: tokens.wrapped,
baseAmount,
action
})
]),
...(await executeApproval(privateKey, { strengthToUSD, id }))
],
id
})
)
);
File renamed without changes.

0 comments on commit a68cc93

Please sign in to comment.