Skip to content

Commit

Permalink
Merge pull request #719 from Chia-Network/feature/cancelEndpoint
Browse files Browse the repository at this point in the history
feat: cancel offer endpoint
  • Loading branch information
MichaelTaylor3D authored Sep 2, 2022
2 parents 04c88c4 + da4a7cc commit 4445144
Show file tree
Hide file tree
Showing 9 changed files with 588 additions and 497 deletions.
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * as LabelController from './label.controller';
export * as AuditController from './audit.controller';
export * as GovernanceController from './governance.controller';
export * as FileStoreController from './fileStore.controller';
export * as OfferController from './offer.controller';
54 changes: 54 additions & 0 deletions src/controllers/offer.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Meta, Staging } from '../models';

import {
assertHomeOrgExists,
assertNoPendingCommits,
assertWalletIsSynced,
assertIfReadOnlyMode,
assertStagingTableNotEmpty,
} from '../utils/data-assertions';

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

export const generateOfferFile = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertStagingTableNotEmpty();
await assertHomeOrgExists();
await assertWalletIsSynced();
await assertNoPendingCommits();

const offerFile = await Staging.generateOfferFile();
res.json(offerFile);
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Error generating offer file.',
error: error.message,
});
}
};

export const cancelActiveOffer = async (req, res) => {
try {
const activeOffer = await Meta.findOne({
where: { metaKey: 'activeOfferTradeId' },
});

if (!activeOffer) {
throw new Error(`There is no active offer to cancel`);
}

await datalayer.cancelActiveOffer(activeOffer.metaValue);
await Meta.destroy({ where: { metaKey: 'activeOfferTradeId' } });

res.json({
message: 'Active offer has been canceled.',
});
} catch (error) {
res.status(400).json({
message: 'Can not cancel active offer',
error: error.message,
});
}
};
19 changes: 0 additions & 19 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ export const findAll = async (req, res) => {
}
};

export const generateOfferFile = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertStagingTableNotEmpty();
await assertHomeOrgExists();
await assertWalletIsSynced();
await assertNoPendingCommits();

const offerFile = await Staging.generateOfferFile();
res.json(offerFile);
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Error generating offer file.',
error: error.message,
});
}
};

export const commit = async (req, res) => {
try {
await assertIfReadOnlyMode();
Expand Down
31 changes: 30 additions & 1 deletion src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ const addMirror = async (storeId, url, forceAddMirror = false) => {
body: JSON.stringify({
id: storeId,
urls: [url],
amount: _.get(CONFIG, 'MIRROR_FEE', 1000000000 /* 1 billion mojos */),
amount: _.get(CONFIG, 'DEFAULT_FEE', 1000000000 /* 1 billion mojos */),
}),
};

Expand Down Expand Up @@ -514,6 +514,34 @@ const makeOffer = async (offer) => {
}
};

const cancelOffer = async (tradeId) => {
const options = {
url: `${CONFIG.DATALAYER_URL}/cancel_offer `,
body: JSON.stringify({
trade_id: tradeId,
secure: true,
fee: _.get(CONFIG, 'DEFAULT_FEE', 1000000000 /* 1 billion mojos */),
}),
};

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;
}
};

export {
addMirror,
makeOffer,
Expand All @@ -530,4 +558,5 @@ export {
pushChangeListToDataLayer,
createDataLayerStore,
getSubscriptions,
cancelOffer,
};
Loading

0 comments on commit 4445144

Please sign in to comment.