Skip to content

Commit

Permalink
refactor: 💡 more detailed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sansan committed Aug 5, 2024
1 parent 2ad1d18 commit 56b663b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/api/procedures/__tests__/moveFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('moveFunds procedure', () => {

const expectedError = new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'One or more of the specified Confidential Assets do not exist',
message: 'Confidential Assets that do not exist were provided',
});

return expect(prepareMoveFunds.call(proc, args)).rejects.toThrowError(expectedError);
Expand All @@ -236,7 +236,7 @@ describe('moveFunds procedure', () => {

const expectedError = new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'The asset is frozen',
message: 'Assets are frozen for trading',
});

return expect(prepareMoveFunds.call(proc, args)).rejects.toThrowError(expectedError);
Expand Down
73 changes: 47 additions & 26 deletions src/api/procedures/moveFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function checkArgs(

const sendingAccount = asConfidentialAccount(from, context);
const receivingAccount = asConfidentialAccount(to, context);
const assets = proofs.map(({ asset }) => asConfidentialAsset(asset, context));

const [sendingAccountExists, receivingAccountExists] = await Promise.all([
sendingAccount.exists(),
Expand All @@ -35,13 +36,15 @@ export async function checkArgs(
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'The sending Confidential Account does not exist',
data: { receivingAccount: sendingAccount.publicKey },
});
}

if (!receivingAccountExists) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'The receiving Confidential Account does not exist',
data: { receivingAccount: receivingAccount.publicKey },
});
}

Expand Down Expand Up @@ -72,63 +75,81 @@ export async function checkArgs(
});
}

const checkAssetExists = proofs.map(({ asset }) => {
const confidentialAsset = asConfidentialAsset(asset, context);
const checkAssetExists = assets.map(confidentialAsset => confidentialAsset.exists());

return confidentialAsset.exists();
});
const assetExistsResult = await Promise.all(checkAssetExists);
const assetsThatDoNotExist: string[] = [];

const assetExists = await Promise.all(checkAssetExists);
assetExistsResult.forEach((exists, index) => {
if (!exists) {
assetsThatDoNotExist.push(assets[index].id);
}
});

if (!assetExists.every(v => v)) {
if (assetsThatDoNotExist.length) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'One or more of the specified Confidential Assets do not exist',
message: 'Confidential Assets that do not exist were provided',
data: { assets: assetsThatDoNotExist },
});
}

const checkIsAssetFrozenPromises = proofs.map(({ asset }) => {
const confidentialAsset = asConfidentialAsset(asset, context);

return confidentialAsset.isFrozen();
});
const checkIsAssetFrozenPromises = assets.map(confidentialAsset => confidentialAsset.isFrozen());

const isFrozenResult = await Promise.all(checkIsAssetFrozenPromises);
const frozenAssets: string[] = [];

isFrozenResult.forEach((isFrozen, index) => {
if (isFrozen) {
frozenAssets.push(assets[index].id);
}
});

if (isFrozenResult.some(v => v)) {
if (frozenAssets.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'The asset is frozen',
message: 'Assets are frozen for trading',
data: { assets: frozenAssets },
});
}

const checkIsSenderAccountFrozenPromises = proofs.map(({ asset }) => {
const confidentialAsset = asConfidentialAsset(asset, context);
const checkIsSenderAccountFrozenPromises = assets.map(confidentialAsset =>
confidentialAsset.isAccountFrozen(from)
);
const isSenderFrozen = await Promise.all(checkIsSenderAccountFrozenPromises);
const frozenAssetsForSender: string[] = [];

return confidentialAsset.isAccountFrozen(from);
isSenderFrozen.forEach((isFrozen, index) => {
if (isFrozen) {
frozenAssetsForSender.push(assets[index].id);
}
});

const isSenderFrozen = await Promise.all(checkIsSenderAccountFrozenPromises);

if (isSenderFrozen.some(v => v)) {
if (frozenAssetsForSender.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'The sender account is frozen for trading specified asset',
data: { assets: frozenAssetsForSender },
});
}

const checkIsReceiverAccountFrozenPromises = proofs.map(({ asset }) => {
const confidentialAsset = asConfidentialAsset(asset, context);
const checkIsReceiverAccountFrozenPromises = assets.map(confidentialAsset =>
confidentialAsset.isAccountFrozen(to)
);
const isReceiverFrozen = await Promise.all(checkIsReceiverAccountFrozenPromises);
const frozenAssetsForReceiver: string[] = [];

return confidentialAsset.isAccountFrozen(to);
isReceiverFrozen.forEach((isFrozen, index) => {
if (isFrozen) {
frozenAssetsForReceiver.push(assets[index].id);
}
});

const isReceiverFrozen = await Promise.all(checkIsReceiverAccountFrozenPromises);

if (isReceiverFrozen.some(v => v)) {
if (frozenAssetsForReceiver.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'The receiver account is frozen for trading specified asset',
data: { assets: frozenAssetsForReceiver },
});
}

Expand Down

0 comments on commit 56b663b

Please sign in to comment.