Skip to content

Commit

Permalink
feat: add endpoit to create an on chain blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroferreira1 committed Jan 30, 2025
1 parent 1d1c899 commit 99ee2ee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/controllers/wallet/nano-contracts.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,46 @@ async function getOracleSignedResult(req, res) {
}
}

/**
* Create an on chain blueprint transaction
*/
async function createOnChainBlueprint(req, res) {
const validationResult = parametersValidation(req);

Check warning on line 206 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L206

Added line #L206 was not covered by tests
if (!validationResult.success) {
res.status(400).json(validationResult);
return;

Check warning on line 209 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L208-L209

Added lines #L208 - L209 were not covered by tests
}

const unlock = lockSendTx(req.walletId);

Check warning on line 212 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L212

Added line #L212 was not covered by tests
if (unlock === null) {
// TODO: return status code 423
// we should do this refactor in the future for all APIs
res.send({ success: false, error: cantSendTxErrorMessage });
return;

Check warning on line 217 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L216-L217

Added lines #L216 - L217 were not covered by tests
}

const { wallet } = req;
const { code, address } = req.body;

Check warning on line 221 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L220-L221

Added lines #L220 - L221 were not covered by tests

try {

Check warning on line 223 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L223

Added line #L223 was not covered by tests
/** @type {import('@hathor/wallet-lib').SendTransaction} */
const sendTransaction = await wallet.createOnChainBlueprintTransaction(code, address);
const tx = await runSendTransaction(sendTransaction, unlock);
res.send({ success: true, ...mapTxReturn(tx) });

Check warning on line 227 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L225-L227

Added lines #L225 - L227 were not covered by tests
} catch (err) {
// The unlock method should be always called. `runSendTransaction` method
// already calls unlock, so we can manually call it only in the catch block.
unlock();
res.send({ success: false, error: err.message });

Check warning on line 232 in src/controllers/wallet/nano-contracts.controller.js

View check run for this annotation

Codecov / codecov/patch

src/controllers/wallet/nano-contracts.controller.js#L231-L232

Added lines #L231 - L232 were not covered by tests
}
}

module.exports = {
getState,
getHistory,
createNanoContract,
executeNanoContractMethod,
getOracleData,
getOracleSignedResult,
createOnChainBlueprint,
};
12 changes: 12 additions & 0 deletions src/routes/wallet/nano-contracts.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
getState,
getHistory,
createNanoContract,
createOnChainBlueprint,
executeNanoContractMethod,
getOracleData,
getOracleSignedResult
Expand Down Expand Up @@ -99,4 +100,15 @@ nanoContractRouter.post(
executeNanoContractMethod,
);

/**
* POST request to create an on chain blueprint tx
* For the docs, see api-docs.js
*/
nanoContractRouter.post(
'/create-on-chain-blueprint',
body('code').isString(),
body('address').isString(),
createOnChainBlueprint,
);

module.exports = nanoContractRouter;

0 comments on commit 99ee2ee

Please sign in to comment.