Skip to content

Commit

Permalink
feat: assert wallet is synced
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Feb 21, 2022
1 parent ad98ae8 commit 37e3216
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 18 deletions.
17 changes: 15 additions & 2 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { Organization } from '../models/organizations';

import { assertHomeOrgExists } from '../utils/data-assertions';
import {
assertHomeOrgExists,
assertWalletIsSynced,
assertDataLayerAvailable,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
return res.json(await Organization.getOrgsMap());
};

export const create = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();

const myOrganization = await Organization.getHomeOrg();

if (myOrganization) {
Expand All @@ -32,8 +39,12 @@ export const create = async (req, res) => {

// eslint-disable-next-line
export const importOrg = async (req, res) => {
const { orgUid, ip, port } = req.body;
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();

const { orgUid, ip, port } = req.body;

res.json({
message:
'Importing and subscribing organization this can take a few mins.',
Expand All @@ -51,6 +62,8 @@ export const importOrg = async (req, res) => {

export const subscribeToOrganization = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertWalletIsSynced();
await assertHomeOrgExists();

await Organization.subscribeToOrganization(req.body.orgUid);
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
assertProjectRecordExists,
assertCsvFileInRequest,
assertHomeOrgExists,
assetNoPendingCommits,
assertNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
} from '../utils/data-assertions';
Expand All @@ -34,7 +34,7 @@ export const create = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const newRecord = _.cloneDeep(req.body);
// When creating new projects assign a uuid to is so
Expand Down Expand Up @@ -187,7 +187,7 @@ export const updateFromXLS = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const { files } = req;

Expand Down Expand Up @@ -217,7 +217,7 @@ export const update = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const originalRecord = await assertProjectRecordExists(
req.body.warehouseProjectId,
Expand Down Expand Up @@ -306,7 +306,7 @@ export const destroy = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const originalRecord = await assertProjectRecordExists(
req.body.warehouseProjectId,
Expand Down Expand Up @@ -337,7 +337,7 @@ export const batchUpload = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const csvFile = assertCsvFileInRequest(req);
await createProjectRecordsFromCsv(csvFile);
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { Staging } from '../models';
import {
assertStagingRecordExists,
assertHomeOrgExists,
assetNoPendingCommits,
assertNoPendingCommits,
assertWalletIsSynced,
assertDataLayerAvailable,
} from '../utils/data-assertions';

export const findAll = async (req, res) => {
Expand Down Expand Up @@ -40,7 +42,9 @@ export const findAll = async (req, res) => {
export const commit = async (req, res) => {
try {
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertDataLayerAvailable();
await assertWalletIsSynced();
await assertNoPendingCommits();

await Staging.pushToDataLayer(_.get(req, 'query.table', null));
res.json({ message: 'Staging Table committed to full node' });
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
assertSumOfSplitUnitsIsValid,
assertCsvFileInRequest,
assertHomeOrgExists,
assetNoPendingCommits,
assertNoPendingCommits,
assertRecordExistance,
assertDataLayerAvailable,
} from '../utils/data-assertions';
Expand All @@ -36,7 +36,7 @@ import xlsx from 'node-xlsx';
export const create = async (req, res) => {
try {
await assertDataLayerAvailable();
await assetNoPendingCommits();
await assertNoPendingCommits();
await assertHomeOrgExists();

const newRecord = _.cloneDeep(req.body);
Expand Down Expand Up @@ -223,7 +223,7 @@ export const updateFromXLS = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const { files } = req;

Expand All @@ -250,7 +250,7 @@ export const update = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const originalRecord = await assertUnitRecordExists(
req.body.warehouseUnitId,
Expand Down Expand Up @@ -336,7 +336,7 @@ export const destroy = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const originalRecord = await assertUnitRecordExists(
req.body.warehouseUnitId,
Expand Down Expand Up @@ -366,7 +366,7 @@ export const split = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const originalRecord = await assertUnitRecordExists(
req.body.warehouseUnitId,
Expand Down Expand Up @@ -443,7 +443,7 @@ export const batchUpload = async (req, res) => {
try {
await assertDataLayerAvailable();
await assertHomeOrgExists();
await assetNoPendingCommits();
await assertNoPendingCommits();

const csvFile = assertCsvFileInRequest(req);
await createUnitRecordsFromCsv(csvFile);
Expand Down
23 changes: 23 additions & 0 deletions src/datalayer/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ const getBaseOptions = () => {
return baseOptions;
};

const walletIsSynced = async () => {
const options = {
url: `${rpcUrl}/get_sync_status`,
body: JSON.stringify({
genesis_initialized: true,
success: true,
synced: true,
syncing: false,
}),
};

const response = await request(Object.assign({}, getBaseOptions(), options));

const data = JSON.parse(response);

if (data.success) {
return data.synced;
}

return false;
};

const hasUnconfirmedTransactions = async () => {
const options = {
url: `${rpcUrl}/get_transactions`,
Expand All @@ -44,4 +66,5 @@ const hasUnconfirmedTransactions = async () => {

export default {
hasUnconfirmedTransactions,
walletIsSynced,
};
2 changes: 2 additions & 0 deletions src/tasks/sync-organizations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SimpleIntervalJob, Task } from 'toad-scheduler';
import { Organization } from '../models';
import dotenv from 'dotenv';
dotenv.config();

const task = new Task('sync-organizations', () => {
console.log('Subscribing to default organizations');
Expand Down
12 changes: 11 additions & 1 deletion src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const assertDataLayerAvailable = async () => {
}
};

export const assetNoPendingCommits = async () => {
export const assertNoPendingCommits = async () => {
if (process.env.USE_SIMULATOR === 'true') {
const pendingCommits = await Staging.findAll({
where: { commited: true },
Expand All @@ -35,6 +35,16 @@ export const assetNoPendingCommits = async () => {
}
};

export const assertWalletIsSynced = async () => {
if (process.env.USE_SIMULATOR === 'false') {
if (!(await datalayer.walletIsSynced())) {
throw new Error(
'Your wallet is syncing, please wait for it to sync and try again',
);
}
}
};

export const assertRecordExistance = async (Model, pk) => {
const record = await Model.findByPk(pk);
if (!record) {
Expand Down

0 comments on commit 37e3216

Please sign in to comment.