Skip to content

Commit

Permalink
feat: import, commit, and cancel offer as a taker
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Sep 5, 2022
1 parent 9026f47 commit f1f5295
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/controllers/offer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import {
assertWalletIsSynced,
assertIfReadOnlyMode,
assertStagingTableNotEmpty,
assertStagingTableIsEmpty,
assertNoActiveOfferFile,
assertActiveOfferFile,
} from '../utils/data-assertions';

// import { deserializeMaker, deserializeTaker } from '../utils/datalayer-utils';

import * as datalayer from '../datalayer/persistance';

export const generateOfferFile = async (req, res) => {
Expand Down Expand Up @@ -52,3 +57,78 @@ export const cancelActiveOffer = async (req, res) => {
});
}
};

export const importOfferFile = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertStagingTableIsEmpty();
await assertHomeOrgExists();
await assertWalletIsSynced();
await assertNoPendingCommits();
await assertNoActiveOfferFile();

const offerFile = req.body;

await datalayer.verifyOffer(offerFile);

await Meta.upsert({
metaKey: 'activeOffer',
metaValue: JSON.stringify(offerFile),
});

// const makerChanges = deserializeMaker(offerFile.maker);
// const takerChanges = deserializeTaker(offerFile.taker);

res.json({
message: 'Offer has been imported for review.',
});
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Can not import offer file.',
error: error.message,
});
}
};

export const commitImportedOfferFile = async (req, res) => {
try {
await assertActiveOfferFile();

const offerFile = Meta.findOne({ where: { metaKey: 'activeOffer' } });
const response = await datalayer.takeOffer(offerFile);

res.json({
message: 'Offer Accepted.',
tradeId: response.trade_id,
});

await Meta.destroy({
where: {
meteKey: 'activeOffer',
},
});
} catch (error) {
res.status(400).json({
message: 'Can not commit offer.',
error: error.message,
});
}
};

export const cancelImportedOfferFile = async (req, res) => {
try {
await assertActiveOfferFile();

await Meta.destroy({
where: {
meteKey: 'activeOffer',
},
});
} catch (error) {
res.status(400).json({
message: 'Can not cancel offer.',
error: error.message,
});
}
};
50 changes: 50 additions & 0 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,54 @@ const makeOffer = async (offer) => {
}
};

const takeOffer = async (offer) => {
const options = {
url: `${CONFIG.DATALAYER_URL}/take_offer `,
body: JSON.stringify(offer),
};

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

const data = JSON.parse(response);

if (data.success) {
return data;
}

throw new Error(data.error);
} catch (error) {
console.log(error);
throw error;
}
};

const verifyOffer = async (offer) => {
const options = {
url: `${CONFIG.DATALAYER_URL}/verify_offer `,
body: JSON.stringify(offer),
};

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

const data = JSON.parse(response);

if (data.success) {
return true;
}

throw new Error(data.error);
} catch (error) {
console.log(error);
throw error;
}
};

const cancelOffer = async (tradeId) => {
const options = {
url: `${CONFIG.DATALAYER_URL}/cancel_offer `,
Expand Down Expand Up @@ -559,4 +607,6 @@ export {
createDataLayerStore,
getSubscriptions,
cancelOffer,
verifyOffer,
takeOffer,
};
1 change: 1 addition & 0 deletions src/models/staging/staging.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class Staging extends Model {

const offerInfo = generateOffer(maker, taker);
const offerResponse = await makeOffer(offerInfo);

if (!offerResponse.success) {
throw new Error(offerResponse.error);
}
Expand Down
12 changes: 12 additions & 0 deletions src/routes/v1/resources/offer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ OfferRouter.delete('/', (req, res) => {
return OfferController.cancelActiveOffer(req, res);
});

OfferRouter.post('/accept/import', (req, res) => {
return OfferController.importOfferFile(req, res);
});

OfferRouter.post('/accept/commit', (req, res) => {
return OfferController.commitImportedOfferFile(req, res);
});

OfferRouter.delete('/accept/cancel', (req, res) => {
return OfferController.cancelImportedOfferFile(req, res);
});

export { OfferRouter };
20 changes: 20 additions & 0 deletions src/utils/data-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,23 @@ export const assertStagingTableIsEmpty = async (customMessage) => {
throw new Error(customMessage || `Staging table is not empty.`);
}
};

export const assertActiveOfferFile = async () => {
const activeOfferFile = await Meta.findOne({
where: { metaKey: 'activeOffer' },
});

if (!activeOfferFile) {
throw new Error(`There is no active offer pending`);
}
};

export const assertNoActiveOfferFile = async () => {
const activeOfferFile = await Meta.findOne({
where: { metaKey: 'activeOffer' },
});

if (activeOfferFile) {
throw new Error(`There is an active offer pending`);
}
};
22 changes: 22 additions & 0 deletions src/utils/datalayer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,25 @@ export const generateOffer = (maker, taker) => {
fee: 0,
};
};

export const deserializeTaker = (taker) => {
const changes = taker[0].inclusions.map((inclusion) => {
const tableKey = decodeHex(inclusion.key);
const table = tableKey.split('|')[0];
const value = JSON.parse(decodeHex(inclusion.value));
return { table, value };
});

return changes;
};

export const deserializeMaker = (maker) => {
const changes = maker[0].proofs.map((inclusion) => {
const tableKey = decodeHex(inclusion.key);
const table = tableKey.split('|')[0];
const value = JSON.parse(decodeHex(inclusion.value));
return { table, value };
});

return changes;
};
Empty file.

0 comments on commit f1f5295

Please sign in to comment.