Skip to content

Commit

Permalink
fix/check buyer and seller (#107)
Browse files Browse the repository at this point in the history
* fix: modal

* fix: validation core function

* chore: remove if and comments

* feat: remove console.log

* chore: remove wrong import

* chore: remove console.log

* refactor: validation

* refactor: move to validation paramters

* feat: improve code

* feat: improve code
  • Loading branch information
EmanuelCampos committed Feb 3, 2023
1 parent bcdb22f commit 94c3385
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/core/pay.ts
Expand Up @@ -220,6 +220,7 @@ export const pay = async (
challengePeriodExtension: paymentProps.challengePeriodExtension,
tokenAddress,
amount,
buyer: walletUser,
});

const _arbitrator = addrs.common.arbitrator || ADDRESS_ZERO;
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/__tests__/validPayment.test.tsx
@@ -1,8 +1,9 @@
import { IPaymentProps } from "../../typing";
import { IValidateProps } from "../../typing";
import { validateParameters } from "../validateParameters";

const params: IPaymentProps = {
const params: IValidateProps = {
seller: "0x7bD733DBc10A1cD04e1e51cC89450941c928ee62",
buyer: "0x484Ee4Eb8CB165F4FBFd897f84283142C8f1fD3a",
arbitrator: "0x7bD733DBc10A1cD04e1e51cC89450941c928ee62",
marketplace: "0x7bD733DBc10A1cD04e1e51cC89450941c928ee62",
amount: 1,
Expand Down
9 changes: 7 additions & 2 deletions src/helpers/validateParameters.ts
@@ -1,4 +1,4 @@
import { IPaymentProps } from "../typing";
import { IValidateProps } from "../typing";
import { ETH_ADDRESS } from "./constants";
import {
validateAddress,
Expand All @@ -13,7 +13,7 @@ export interface AddressesToCheck {
tokenAddress?: string;
}

export const validateParameters = async (data: IPaymentProps) => {
export const validateParameters = async (data: IValidateProps) => {
const {
seller,
arbitrator,
Expand All @@ -24,6 +24,7 @@ export const validateParameters = async (data: IPaymentProps) => {
arbitratorFee,
marketplaceFee,
tokenAddress = ETH_ADDRESS,
buyer,
} = data;

const addrs: AddressToReturn = await validateEns({
Expand All @@ -38,6 +39,10 @@ export const validateParameters = async (data: IPaymentProps) => {
throw new Error(e.message);
}

if (buyer.toLowerCase() === seller.toLowerCase()) {
throw new Error("Buyer cannot be the same as the seller");
}

if (amount <= 0) {
throw new Error("Invalid amount");
}
Expand Down
4 changes: 4 additions & 0 deletions src/typing/index.ts
Expand Up @@ -45,6 +45,10 @@ export interface IPaymentProps {
challengePeriodExtension?: number;
}

export interface IValidateProps extends IPaymentProps {
buyer: string;
}

export interface IPaymentPropsData extends IPaymentProps {
ensAddresses?: IEnsAddresses;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ui/render/pay.ts
Expand Up @@ -9,6 +9,7 @@ import {
import { toast } from "ui/internal/notification/toast";
import { renderModal } from "ui/internal/config/render";
import { PayModal } from "ui/internal/modals";
import { getWalletAccount } from "wallet";

/**
* Opens a payment modal, which summarizes the escrow parameters for the user (buyer) and displays a button to Pay.
Expand Down Expand Up @@ -122,7 +123,8 @@ export const pay = async (
const data: IPaymentPropsData = paymentProps;

try {
const addrs = await validateParameters(data);
const walletUser = await getWalletAccount();
const addrs = await validateParameters({ ...data, buyer: walletUser });

Object.entries(addrs.common).forEach(([key, value]) => {
paymentProps[key] = value;
Expand Down

0 comments on commit 94c3385

Please sign in to comment.