Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FE Support For Snapshots v2 Beta Server #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"scripts": {
"client": "PORT=3001 FAST_REFRESH=false react-scripts start",
"server": "node -r dotenv/config --trace-warnings --max-old-space-size=13958 server/main.js 2>&1 | tee /tmp/cryptoecon.log",
"server": "node -r dotenv/config --trace-warnings --max-old-space-size=32768 server/main.js 2>&1 | tee /tmp/cryptoecon.log",
"dev": "node -r dotenv/config --max-old-space-size=16384 server/main.js",
"test-process": "node -r dotenv/config --trace-warnings --max-old-space-size=16384 ./server/worker/process.childprocess.test.js",
"refresh-lm-snapshot": "source .env && curl -H 'Content-Type: application/json' -H \"$HEADER_SECRET\" -X POST -d '{\"query\": \"query GetSnapshot { snapshots_new(limit: 1 order_by: {id: desc}) { snapshot_data } }\"}' $SNAPSHOT_URL -o ./snapshots/snapshot_lm_latest.trash.json",
Expand Down
2 changes: 1 addition & 1 deletion js/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const config = {
DEPOSITS_ALLOWED_DURATION_MS,
MULTIPLIER_MATURITY: 864, // 4 months in in 200minute intervals,
NUMBER_OF_INTERVALS_TO_RUN: REWARD_ACCRUAL_DURATION_INTERVAL_COUNT + 864, // duration of bucket drain + duration to latest possible multiplier maturity
REWARD_ACCRUAL_DURATION_INTERVAL_COUNT,
REWARD_ACCRUAL_DURATION_INTERVAL_COUNT
};

module.exports = config;
14 changes: 7 additions & 7 deletions js/server/loaders/loadLiquidityMinersSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ const { getDatabase } = require('./getDatabase');
// }
// };

const getSQLQueryByNetwork = (network) => {
const getSQLQueryByNetwork = network => {
network = network ? network.toLowerCase() : network;
switch (network) {
case TESTNET: {
return getDatabase().transaction(async (tx) => {
return getDatabase().transaction(async tx => {
const snapshots_new = await tx.many(
slonik.sql`select snapshot_data from snapshots_new_dev ORDER BY created_at DESC LIMIT 1`
);
Expand All @@ -64,13 +64,13 @@ const getSQLQueryByNetwork = (network) => {
data: {
snapshots_new,
snapshots_lm_claims,
snapshots_lm_dispensation,
},
snapshots_lm_dispensation
}
};
});
}
default: {
return getDatabase().transaction(async (tx) => {
return getDatabase().transaction(async tx => {
const snapshots_new = tx.many(
slonik.sql`select * from snapshots_lm_rf rf where rf.snapshot_time = (select max(snapshot_time) from snapshots_lm_rf)`
);
Expand All @@ -92,8 +92,8 @@ const getSQLQueryByNetwork = (network) => {
data: {
snapshots_new: snapshotsNewLoaded,
snapshots_lm_claims: await snapshots_lm_claims,
snapshots_lm_dispensation: await snapshots_lm_dispensation,
},
snapshots_lm_dispensation: await snapshots_lm_dispensation
}
};
});
}
Expand Down
20 changes: 11 additions & 9 deletions js/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ const { ProcessingHandler } = require('./worker');
const {
DEVNET,
MAINNET,
TESTNET,
TESTNET
} = require('./constants/snapshot-source-names');
const {
GET_LM_DISPENSATION_JOB,
GET_VS_DISPENSATION_JOB,
GET_VS_DISPENSATION_JOB
} = require('./constants/action-names');
const moment = require('moment');
const { encrypt, decrypt } = require('./util/encrypt');
const {
createGenericDispensationJob,
createGenericDispensationJob
} = require('./util/createGenericDispensationJob');

if (process.env.DATABASE_URL) {
const encrypted = encrypt(process.env.DATABASE_URL);
require('fs').writeFileSync('./DATABASE_URL.enc', encrypted.encryptedData);
} else {
const dburlEnc = require('fs').readFileSync('./DATABASE_URL.enc').toString();
const dburlEnc = require('fs')
.readFileSync('./DATABASE_URL.enc')
.toString();
const data = decrypt(dburlEnc);
process.env.DATABASE_URL = data;
}
Expand Down Expand Up @@ -61,12 +63,12 @@ let createTestnetHandler = () => {
};
const processingHandlers = {
[MAINNET]: ProcessingHandler.init(MAINNET),
get [DEVNET]() {
get [DEVNET] () {
return createTestnetHandler();
},
get [TESTNET]() {
get [TESTNET] () {
return createTestnetHandler();
},
}
};

// const processingHandler = BackgroundProcessor.startAsMainProcess();
Expand Down Expand Up @@ -164,7 +166,7 @@ app.get('/api/lm', async (req, res, next) => {
const timeIndex = getTimeIndex(req.query.timestamp);
responseJSON = await processingHandler.dispatch('GET_LM_USER_DATA', {
address,
timeIndex,
timeIndex
});
break;
}
Expand Down Expand Up @@ -226,7 +228,7 @@ app.get('/api/vs', async (req, res, next) => {
const timeIndex = getTimeIndex(req.query.timestamp);
responseJSON = await processingHandler.dispatch('GET_VS_USER_DATA', {
address,
timeIndex,
timeIndex
});
break;
}
Expand Down
37 changes: 19 additions & 18 deletions js/server/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,42 @@ const {
remapLMAddresses,
getLMTimeseriesFinalIndex,
createClaimEvents,
createDispensationEvents,
createDispensationEvents
} = require('./util/lm-util');
const {
remapVSAddresses,
getVSTimeseriesFinalIndex,
getVSTimeseriesFinalIndex
} = require('./util/vs-util');
const { processVSGlobalState } = require('./process-vs');

const {
EVENT_INTERVAL_MINUTES,
NUMBER_OF_INTERVALS_TO_RUN,
NUMBER_OF_INTERVALS_TO_RUN
} = require('./config');
const { GlobalTimestampState } = require('./types');

const {
VALIDATOR_STAKING,
LIQUIDITY_MINING,
LIQUIDITY_MINING
} = require('./constants/reward-program-types');
const { mockMinerClaims, mockMinerDispensations } = require('./mock');

exports.getProcessedLMData = (snapshotLM) => {
exports.getProcessedLMData = snapshotLM => {
let {
snapshots_new: [{ snapshot_data: minerSnapshotData }],
snapshots_lm_claims: [{ snapshot_data: claimsSnapshotData = {} } = {}],
snapshots_lm_dispensation: [
{ snapshot_data: dispensationsSnapshotData = {} } = {},
] = [],
{ snapshot_data: dispensationsSnapshotData = {} } = {}
] = []
} = snapshotLM.data;
const snapshotTimeseriesFinalIndex =
getLMTimeseriesFinalIndex(minerSnapshotData);
const snapshotTimeseriesFinalIndex = getLMTimeseriesFinalIndex(
minerSnapshotData
);

if (process.env.MOCK_DATA_ENABLED === 'true') {
claimsSnapshotData = mockMinerClaims(snapshotLM).claimsSnapshotData;
dispensationsSnapshotData =
mockMinerDispensations(claimsSnapshotData).dispensationsSnapshotData;
dispensationsSnapshotData = mockMinerDispensations(claimsSnapshotData)
.dispensationsSnapshotData;
}

const claimEventsByUserByTimestamp = createClaimEvents(claimsSnapshotData);
Expand All @@ -57,13 +58,13 @@ exports.getProcessedLMData = (snapshotLM) => {
);
};

exports.getProcessedVSData = (snapshotVS) => {
exports.getProcessedVSData = snapshotVS => {
let {
snapshots_validators: [{ snapshot_data: validatorSnapshotData }],
snapshots_vs_claims: [{ snapshot_data: claimsSnapshotData = {} } = {}],
snapshots_vs_dispensation: [
{ snapshot_data: dispensationsSnapshotData = {} } = {},
] = [],
{ snapshot_data: dispensationsSnapshotData = {} } = {}
] = []
} = snapshotVS.data;

const snapshotTimeseriesFinalIndex = getVSTimeseriesFinalIndex(
Expand All @@ -78,7 +79,7 @@ exports.getProcessedVSData = (snapshotVS) => {
console.time('remapVS');
const { userEventsByTimestamp } = remapVSAddresses(validatorSnapshotData);
console.timeEnd('remapVS');
function getCurrentCommissionRate(validatorStakeAddress, stateIndex) {
function getCurrentCommissionRate (validatorStakeAddress, stateIndex) {
const validatorCommissionData =
validatorSnapshotData[validatorStakeAddress].commission;
const commissionIndex =
Expand All @@ -101,10 +102,10 @@ exports.getProcessedVSData = (snapshotVS) => {
const cacheEnabled = false;
const history = {
[VALIDATOR_STAKING]: {},
[LIQUIDITY_MINING]: {},
[LIQUIDITY_MINING]: {}
};

function processUserEventsByTimestamp(
function processUserEventsByTimestamp (
userEventsByTimestamp,
getCurrentCommissionRate = (address, stateIndex) => 0,
rewardProgramType,
Expand Down Expand Up @@ -134,7 +135,7 @@ function processUserEventsByTimestamp(
lastGlobalState,
timestamp,
userEvents,
(address) => getCurrentCommissionRate(address, i),
address => getCurrentCommissionRate(address, i),
rewardProgramType,
isSimulatedFutureInterval,
claimEventsByUser,
Expand Down
1 change: 1 addition & 0 deletions js/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ class App extends React.Component {
>
<option value={networks.MAINNET}>MAINNET</option>
<option value={networks.TESTNET}>TESTNET</option>
<option value={networks.MAINNET_SNAPSHOTS_V2_BETA}>V2_BETA</option>
</select>
</div>
</div>
Expand Down
23 changes: 16 additions & 7 deletions js/src/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { networks } from './config';
const serverURL = (() => {
let environment = process.env.REACT_APP_DEPLOYMENT_TAG;
// environment = 'devnet';
switch (environment) {
case 'production':
return 'https://api-cryptoeconomics.sifchain.finance/api';
Expand All @@ -23,9 +22,16 @@ function handleFailedRequest () {
window.location.reload();
}, 3000);
}
const getUrl = network => {
let url = serverURL;
if (networks.MAINNET_SNAPSHOTS_V2_BETA === network) {
url = 'http://ec2-3-138-153-106.us-east-2.compute.amazonaws.com/api';
}
return url;
};
export const fetchUsers = (type, network) => {
return window
.fetch(`${serverURL}/${type}?key=users`, {
.fetch(`${getUrl(network)}/${type}?key=users`, {
headers: getSnapshotNetworkHeaders(network)
})
.then(response => response.json())
Expand All @@ -35,7 +41,7 @@ export const fetchUsers = (type, network) => {
export const fetchUserData = (address, type, timestamp, network) => {
return window
.fetch(
`${serverURL}/${type}?key=userData&address=${address}${
`${getUrl(network)}/${type}?key=userData&address=${address}${
timestamp ? `&timestamp=${new Date(timestamp).toISOString()}` : ``
}`,
{
Expand All @@ -48,16 +54,19 @@ export const fetchUserData = (address, type, timestamp, network) => {

export const fetchUserTimeSeriesData = (address, type, network) => {
return window
.fetch(`${serverURL}/${type}?key=userTimeSeriesData&address=${address}`, {
headers: getSnapshotNetworkHeaders(network)
})
.fetch(
`${getUrl(network)}/${type}?key=userTimeSeriesData&address=${address}`,
{
headers: getSnapshotNetworkHeaders(network)
}
)
.then(response => response.json())
.catch(handleFailedRequest);
};

export const fetchStack = (type, network) => {
return window
.fetch(`${serverURL}/${type}?key=stack`, {
.fetch(`${getUrl(network)}/${type}?key=stack`, {
headers: getSnapshotNetworkHeaders(network)
})
.then(response => response.json())
Expand Down
3 changes: 2 additions & 1 deletion js/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const config = {
networks: {
MAINNET: 'mainnet',
DEVNET: 'devnet',
TESTNET: 'testnet'
TESTNET: 'testnet',
MAINNET_SNAPSHOTS_V2_BETA: 'snapshots_v2_mainnet'
},
RECENT_ADDRESS_LIST_STORAGE_KEY: '__recent-address-list'
};
Expand Down