-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #332 from Lumerin-protocol/feature/auto-update
Added profit adjust
- Loading branch information
Showing
26 changed files
with
1,004 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}, | ||
|
@@ -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", | ||
|
@@ -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" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.