Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipient address to streams when nonexistent #1890

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/resolvers/anchorContractAddressResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { ApolloContext } from '../types/ApolloContext';
import { findUserById } from '../repositories/userRepository';
import { getProvider } from '../provider';
import { logger } from '../utils/logger';
import { ChainType } from '../types/network';
import { addBulkNewProjectAddress } from '../repositories/projectAddressRepository';
import { validateProjectRelatedAddresses } from '../utils/validators/projectValidator';

@Resolver(_of => AnchorContractAddress)
export class AnchorContractAddressResolver {
Expand All @@ -24,6 +27,7 @@ export class AnchorContractAddressResolver {
@Arg('networkId', () => Int) networkId: number,
@Arg('address', () => String) address: string,
@Arg('txHash', () => String) txHash: string,
@Arg('recipientAddress', { nullable: true }) recipientAddress?: string,
): Promise<AnchorContractAddress> {
const userId = ctx?.req?.user?.userId;
const creatorUser = await findUserById(userId);
Expand Down Expand Up @@ -53,11 +57,29 @@ export class AnchorContractAddressResolver {
projectAddress.isRecipient === true,
)
) {
throw new Error(
i18n.__(
translationErrorMessagesKeys.PROJECT_DOESNT_HAVE_RECIPIENT_ADDRESS_ON_THIS_NETWORK,
),
);
if (recipientAddress) {
const recipientAddressInput = {
project,
user: creatorUser,
address: recipientAddress!,
chainType: ChainType.EVM,
networkId: networkId,
isRecipient: true,
};

await validateProjectRelatedAddresses(
[recipientAddressInput],
projectId,
);

await addBulkNewProjectAddress([recipientAddressInput]);
} else {
Comment on lines +60 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add address format validation before processing.

The recipient address should be validated for proper format before attempting to create and validate the input object.

   if (recipientAddress) {
+    if (!ethers.utils.isAddress(recipientAddress)) {
+      throw new Error(
+        i18n.__(translationErrorMessagesKeys.INVALID_ADDRESS_FORMAT)
+      );
+    }
+
     const recipientAddressInput = {
       project,
       user: creatorUser,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (recipientAddress) {
const recipientAddressInput = {
project,
user: creatorUser,
address: recipientAddress!,
chainType: ChainType.EVM,
networkId: networkId,
isRecipient: true,
};
await validateProjectRelatedAddresses(
[recipientAddressInput],
projectId,
);
await addBulkNewProjectAddress([recipientAddressInput]);
} else {
if (recipientAddress) {
if (!ethers.utils.isAddress(recipientAddress)) {
throw new Error(
i18n.__(translationErrorMessagesKeys.INVALID_ADDRESS_FORMAT)
);
}
const recipientAddressInput = {
project,
user: creatorUser,
address: recipientAddress!,
chainType: ChainType.EVM,
networkId: networkId,
isRecipient: true,
};
await validateProjectRelatedAddresses(
[recipientAddressInput],
projectId,
);
await addBulkNewProjectAddress([recipientAddressInput]);
} else {

throw new Error(
i18n.__(
translationErrorMessagesKeys.PROJECT_DOESNT_HAVE_RECIPIENT_ADDRESS_ON_THIS_NETWORK,
),
);
}
}

const web3Provider = getProvider(networkId);
Expand Down
Loading