Skip to content

Commit

Permalink
Merge pull request #332 from Lumerin-protocol/feature/auto-update
Browse files Browse the repository at this point in the history
Added profit adjust
  • Loading branch information
alex-sandrk authored Jan 15, 2024
2 parents 2c8028d + 2645790 commit 9220209
Show file tree
Hide file tree
Showing 26 changed files with 1,004 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .devSepoliaArbitrum.env
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ CLONE_FACTORY_ADDRESS=0x15437978300786aDe37f61e02Be1C061e51353D3

TITAN_LIGHTNING_POOL=pplp.titan.io:4141
DEFAULT_SELLER_CURRENCY=BTC

# 15 minutes
AUTO_ADJUST_PRICE_INTERVAL=900000
# 24 hours
AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT=86400000
4 changes: 4 additions & 0 deletions .github/workflows/auto-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
runs-on: ${{ matrix.os }}
environment: ${{ matrix.environment }}
env: # keep alphabetical order
AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT: ${{ vars.AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT }}
AUTO_ADJUST_PRICE_INTERVAL: ${{ vars.AUTO_ADJUST_PRICE_INTERVAL }}
CHAIN_ID: ${{ vars.CHAIN_ID }}
COIN_DEFAULT_GAS_LIMIT: ${{ vars.COIN_DEFAULT_GAS_LIMIT }}
DEFAULT_GAS_PRICE: ${{ vars.DEFAULT_GAS_PRICE }}
Expand Down Expand Up @@ -63,6 +65,8 @@ jobs:
- name: Set env from github
# keep alphabetical order
run: |
echo "AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT=$AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT" >> .env
echo "AUTO_ADJUST_PRICE_INTERVAL=$AUTO_ADJUST_PRICE_INTERVAL" >> .env
echo "CHAIN_ID=$CHAIN_ID" >> .env
echo "COIN_DEFAULT_GAS_LIMIT=$COIN_DEFAULT_GAS_LIMIT" >> .env
echo "DEFAULT_GAS_PRICE=$DEFAULT_GAS_PRICE" >> .env
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/auto-release-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
runs-on: ${{ matrix.os }}
environment: ${{ matrix.environment }}
env: # keep alphabetical order
AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT: ${{ vars.AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT }}
AUTO_ADJUST_PRICE_INTERVAL: ${{ vars.AUTO_ADJUST_PRICE_INTERVAL }}
CHAIN_ID: ${{ vars.CHAIN_ID }}
COIN_DEFAULT_GAS_LIMIT: ${{ vars.COIN_DEFAULT_GAS_LIMIT }}
DEFAULT_GAS_PRICE: ${{ vars.DEFAULT_GAS_PRICE }}
Expand Down Expand Up @@ -69,6 +71,8 @@ jobs:
- name: Set env from github
# keep alphabetical order
run: |
echo "AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT"=$AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT >> .env
echo "AUTO_ADJUST_PRICE_INTERVAL"=$AUTO_ADJUST_PRICE_INTERVAL >> .env
echo "CHAIN_ID=$CHAIN_ID" >> .env
echo "COIN_DEFAULT_GAS_LIMIT=$COIN_DEFAULT_GAS_LIMIT" >> .env
echo "DEFAULT_GAS_PRICE=$DEFAULT_GAS_PRICE" >> .env
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/auto-release-stg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
runs-on: ${{ matrix.os }}
environment: ${{ matrix.environment }}
env: # keep alphabetical order
AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT: ${{ vars.AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT }}
AUTO_ADJUST_PRICE_INTERVAL: ${{ vars.AUTO_ADJUST_PRICE_INTERVAL }}
CHAIN_ID: ${{ vars.CHAIN_ID }}
COIN_DEFAULT_GAS_LIMIT: ${{ vars.COIN_DEFAULT_GAS_LIMIT }}
DEFAULT_GAS_PRICE: ${{ vars.DEFAULT_GAS_PRICE }}
Expand Down Expand Up @@ -69,6 +71,8 @@ jobs:
- name: Set env from github
# keep alphabetical order
run: |
echo "AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT=$AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT" >> .env
echo "AUTO_ADJUST_PRICE_INTERVAL=$AUTO_ADJUST_PRICE_INTERVAL" >> .env
echo "CHAIN_ID=$CHAIN_ID" >> .env
echo "COIN_DEFAULT_GAS_LIMIT=$COIN_DEFAULT_GAS_LIMIT" >> .env
echo "DEFAULT_GAS_PRICE=$DEFAULT_GAS_PRICE" >> .env
Expand Down
Binary file modified executables/proxy-router
Binary file not shown.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lumerin-wallet-desktop",
"version": "1.2.47",
"version": "1.2.48",
"engines": {
"node": ">=14"
},
Expand Down Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"@electron/remote": "2.0.9",
"@lumerin/wallet-core": "git+ssh://[email protected]:Lumerin-protocol/WalletCore.git#1.0.84",
"@lumerin/wallet-core": "git+ssh://[email protected]:Lumerin-protocol/WalletCore.git#1.0.85",
"@reach/menu-button": "0.17.0",
"@tabler/icons": "1.119.0",
"axios": "0.27.2",
Expand Down Expand Up @@ -94,14 +94,14 @@
"react-select": "5.7.2",
"react-tabs": "4.3.0",
"react-timer-hook": "3.0.5",
"react-tabs": "4.3.0",
"react-virtualized": "9.20.1",
"redux": "4.2.0",
"redux-actions": "2.3.0",
"reselect": "3.0.1",
"smart-round": "1.0.0",
"styled-components": "4.1.2",
"universal-analytics": "0.4.20",
"use-interval": "1.4.0",
"web3-utils": "1.3.6",
"zxcvbn3": "0.1.1"
},
Expand Down
2 changes: 2 additions & 0 deletions public/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ module.exports = {
statePersistanceDebounce: 2000,
trackingId: process.env.TRACKING_ID,
web3Timeout: 120000,
autoAdjustPriceInterval: +process.env.AUTO_ADJUST_PRICE_INTERVAL || 15 * 60 * 1000,
autoAdjustContractPriceTimeout: +process.env.AUTO_ADJUST_CONTRACT_PRICE_TIMEOUT || 24 * 60 * 60 * 1000,
recaptchaSiteKey: process.env.RECAPTCHA_SITE_KEY,
};
22 changes: 21 additions & 1 deletion public/main/client/handlers/no-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ const setDefaultCurrency = async (curr) => setDefaultCurrencySetting(curr);
const getCustomEnvs = async () => getKey('customEnvs');
const setCustomEnvs = async (value) => setKey('customEnvs', value);

const getProfitSettings = async () => getKey('profitSettings') || {
deviation: 2,
target: 10,
adaptExisting: false,
};
const setProfitSettings = async (value) => setKey('profitSettings', value);

const getAutoAdjustPriceData = async () => getKey('autoAdjustPriceData');
const setAutoAdjustPriceData = async (value) => {
const oldData = await getAutoAdjustPriceData();
setKey('autoAdjustPriceData', {
...oldData,
...value,
});
}

/**
*
* @param {string} contractId
Expand Down Expand Up @@ -94,5 +110,9 @@ module.exports = {
getCustomEnvs,
setCustomEnvs,
restartWallet,
getContractHashrate
getContractHashrate,
getProfitSettings,
setProfitSettings,
getAutoAdjustPriceData,
setAutoAdjustPriceData,
};
2 changes: 2 additions & 0 deletions public/main/client/handlers/single-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const createContract = async function (data, { api }) {
price: data.price,
speed: data.speed,
duration: data.duration,
profit: data.profit,
sellerAddress: data.sellerAddress,
password: data.password,
privateKey,
Expand Down Expand Up @@ -79,6 +80,7 @@ const editContract = async function (data, { api }) {
price: data.price,
speed: data.speed,
duration: data.duration,
profit: data.profit,
password: data.password,
walletId: data.walletId,
privateKey,
Expand Down
6 changes: 5 additions & 1 deletion public/main/client/subscriptions/no-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ const listeners = {
"set-default-currency-settings": handlers.setDefaultCurrency,
"get-custom-env-values": handlers.getCustomEnvs,
"set-custom-env-values": handlers.setCustomEnvs,
"get-contract-hashrate": handlers.getContractHashrate
"get-profit-settings": handlers.getProfitSettings,
"set-profit-settings": handlers.setProfitSettings,
"get-contract-hashrate": handlers.getContractHashrate,
"get-auto-adjust-price": handlers.getAutoAdjustPriceData,
"set-auto-adjust-price": handlers.setAutoAdjustPriceData,
};

// Subscribe to messages where no core has to react
Expand Down
4 changes: 4 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ const createClient = function(createStore) {
claimFaucet: utils.forwardToMainProcess('claim-faucet', 750000),
getCustomEnvValues: utils.forwardToMainProcess('get-custom-env-values'),
setCustomEnvValues: utils.forwardToMainProcess('set-custom-env-values'),
getProfitSettings: utils.forwardToMainProcess('get-profit-settings'),
setProfitSettings: utils.forwardToMainProcess('set-profit-settings'),
getAutoAdjustPriceData: utils.forwardToMainProcess('get-auto-adjust-price'),
setAutoAdjustPriceData: utils.forwardToMainProcess('set-auto-adjust-price'),
getContractHashrate: utils.forwardToMainProcess('get-contract-hashrate')
};

Expand Down
152 changes: 152 additions & 0 deletions src/components/AutoPriceAdjuster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import React, { useContext } from 'react';

import withContractsState from '../store/hocs/withContractsState';
import { ToastsContext } from './toasts';
import { lmrDecimals } from '../utils/coinValue';
import { formatBtcPerTh, calculateSuggestedPrice } from './contracts/utils';

import useInterval from 'use-interval';

function AutoPriceAdjuster({
contracts,
address,
client,
networkDifficulty,
lmrCoinPrice,
btcCoinPrice,
autoAdjustPriceInterval,
autoAdjustContractPriceTimeout
}) {
const context = useContext(ToastsContext);

const adjustContractPrices = async (
allContracts,
lmrRate,
btcRate,
sellerAddress,
networkDifficulty
) => {
const profitSettings = await client.getProfitSettings();
const autoAdjustSettings = await client.getAutoAdjustPriceData();

const contractsWithEnabledAutoAdjust = allContracts.filter(
c =>
c.seller === sellerAddress &&
!c.isDead &&
autoAdjustSettings[c.id?.toLowerCase()]?.enabled
);

const reward = formatBtcPerTh(networkDifficulty);
const deviation = +profitSettings.deviation;

const result = contractsWithEnabledAutoAdjust.reduce((curr, contract) => {
const contractProfitTarget =
+contract.futureTerms?.profitTarget || +contract.profitTarget;
const profitTarget =
contractProfitTarget !== 0
? contractProfitTarget
: profitSettings?.adaptExisting
? +profitSettings?.target
: 0;

if (profitTarget === 0) {
return curr;
}

const speed = (contract.futureTerns?.speed || contract.speed) / 10 ** 12;
const length = (contract.futureTerns?.length || contract.length) / 3600;
const price =
(contract.futureTerms?.price || contract.price) / lmrDecimals;

const profitTargetPercent = profitTarget / 100;
const deviationPercent = deviation / 100;

const left = 1 + deviationPercent - profitTargetPercent;
const right = 1 + deviationPercent + profitTargetPercent;

const estimatedLeft = calculateSuggestedPrice(
length,
speed,
btcRate,
lmrRate,
reward,
left
);
const estimatedRight = calculateSuggestedPrice(
length,
speed,
btcRate,
lmrRate,
reward,
right
);
const targetEstimate = calculateSuggestedPrice(
length,
speed,
btcRate,
lmrRate,
reward,
1 + profitTargetPercent
);

const isPriceWithinRange =
estimatedLeft <= price && estimatedRight >= price;

if (!isPriceWithinRange) {
curr.push({
...contract,
newPrice: targetEstimate
});
}
return curr;
}, []);

for (const contract of result) {
const autoAdjustContractSettings =
autoAdjustSettings[contract.id.toLowerCase()];
const lastUpdatedAt = autoAdjustContractSettings?.lastUpdatedAt;

if (
!lastUpdatedAt ||
lastUpdatedAt + autoAdjustContractPriceTimeout < Date.now()
) {
await client
.editContract({
id: contract.id,
price: (contract.newPrice * lmrDecimals).toString(),
speed: contract.speed,
duration: contract.length,
sellerAddress: contract.seller,
profit: contract.profitTarget
})
.then(() => {
client.setAutoAdjustPriceData({
[contract.id.toLowerCase()]: {
...autoAdjustContractSettings,
lastUpdatedAt: Date.now()
}
});
});
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
};

useInterval(() => {
if (contracts.length) {
adjustContractPrices(
contracts,
lmrCoinPrice,
btcCoinPrice,
address,
networkDifficulty
).catch(e => {
context.toast('error', `Failed to auto adjust prices: ${e.message}`);
});
}
}, autoAdjustPriceInterval);

return <></>;
}

export default withContractsState(AutoPriceAdjuster);
2 changes: 2 additions & 0 deletions src/components/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import SellerHub from './contracts/SellerHub';
import Marketplace from './contracts/Marketplace';
import Devices from './devices/Devices';
import BuyerHub from './contracts/BuyerHub';
import AutoPriceAdjuster from './AutoPriceAdjuster';

const bgImage = 'images/MainBackground.png';

Expand Down Expand Up @@ -77,6 +78,7 @@ export const Layout = () => (
<Route path="/devices" component={Devices} />
</Switch>
</Main>
<AutoPriceAdjuster />
<OfflineWarning />
</Container>
);
Expand Down
Loading

0 comments on commit 9220209

Please sign in to comment.