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

feat(web): support for ERC20 Tokens in the General Escrow flow #53

Merged
merged 16 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@web3modal/ethereum": "^2.7.1",
"@web3modal/react": "^2.2.2",
"@yornaath/batshit": "^0.9.0",
"alchemy-sdk": "^3.3.1",
"amqplib": "^0.10.3",
"chart.js": "^3.9.1",
"chartjs-adapter-moment": "^1.0.1",
Expand Down
Binary file added web/src/assets/svgs/icons/eth-token-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions web/src/components/PreviewCard/EscrowTimeline/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IEscrowTimeline {
isPreview: boolean;
transactionCreationTimestamp: number;
status: boolean;
token: string;
assetSymbol: string;
buyerAddress: string;
sellerAddress: string;
payments: Payment[];
Expand All @@ -26,7 +26,7 @@ const EscrowTimeline: React.FC<IEscrowTimeline> = ({
isPreview,
transactionCreationTimestamp,
status,
token,
assetSymbol,
buyerAddress,
sellerAddress,
payments,
Expand All @@ -41,7 +41,7 @@ const EscrowTimeline: React.FC<IEscrowTimeline> = ({
isPreview,
transactionCreationTimestamp,
status,
token,
assetSymbol,
buyerAddress,
sellerAddress,
payments,
Expand Down
33 changes: 22 additions & 11 deletions web/src/components/PreviewCard/Terms/Description.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const StyledP = styled.p`
word-break: break-word;
`;

const InlineBlockSpan = styled.span`
display: inline-block;
`;

interface IDescription {
escrowType: string;
deliverableText: string;
Expand All @@ -17,7 +21,7 @@ interface IDescription {
sendingQuantity: string;
sendingToken: string;
sellerAddress: string;
deadlineDate: Date;
deadline: number;
assetSymbol: string;
buyer: string;
}
Expand All @@ -31,19 +35,25 @@ const Description: React.FC<IDescription> = ({
sendingQuantity,
sendingToken,
sellerAddress,
deadlineDate,
deadline,
assetSymbol,
}) => {
const generalEscrowSummary =
`By Paying ${sendingQuantity + " " + assetSymbol}, address ${buyerAddress} should receive` +
` "${deliverableText}" from address ${sellerAddress} before the delivery deadline ${new Date(
deadlineDate
)}.`;
const generalEscrowSummary = (
<>
By Paying {sendingQuantity}{" "}
<InlineBlockSpan>{assetSymbol ? assetSymbol : <StyledSkeleton width={30} />}</InlineBlockSpan>, address{" "}
{buyerAddress} should receive "{deliverableText}" from address {sellerAddress} before the delivery deadline{" "}
{new Date(deadline).toString()}.
</>
);

const cryptoSwapSummary =
`By Paying ${sendingQuantity + " " + sendingToken}, [Blockchain] address ${buyerAddress} should receive` +
` ${receivingQuantity + " " + receivingToken} at the [Blockchain] address ${sellerAddress}` +
` from [Blockchain] address TODO before the delivery deadline ${deadlineDate}.`;
const cryptoSwapSummary = (
<>
By Paying {sendingQuantity} {sendingToken}, [Blockchain] address {buyerAddress} should receive {receivingQuantity}{" "}
{receivingToken} at the [Blockchain] address {sellerAddress} from [Blockchain] address TODO before the delivery
deadline {new Date(deadline).toString()}.
</>
);

return isUndefined(deliverableText) ? (
<StyledSkeleton />
Expand All @@ -58,4 +68,5 @@ const Description: React.FC<IDescription> = ({
</div>
);
};

export default Description;
28 changes: 13 additions & 15 deletions web/src/components/PreviewCard/Terms/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface ITerms {
sendingQuantity: string;
sendingToken: string;
sellerAddress: string;
deadlineDate: Date;
deadline: number;
assetSymbol: string;
extraDescriptionUri: string;
buyer: string;
Expand All @@ -29,31 +29,29 @@ const Terms: React.FC<ITerms> = ({
escrowType,
deliverableText,
receivingQuantity,
receivingToken,
buyerAddress,
sendingQuantity,
sendingToken,
sellerAddress,
deadlineDate,
deadline,
assetSymbol,
extraDescriptionUri,
}) => {
return (
<Container>
<Header />
<Description
escrowType={escrowType}
deliverableText={deliverableText}
receivingQuantity={receivingQuantity}
receivingToken={receivingToken}
buyerAddress={buyerAddress}
sendingQuantity={sendingQuantity}
sendingToken={sendingToken}
sellerAddress={sellerAddress}
deadlineDate={deadlineDate}
assetSymbol={assetSymbol}
{...{
escrowType,
deliverableText,
receivingQuantity,
buyerAddress,
sendingQuantity,
sellerAddress,
deadline,
assetSymbol,
}}
/>
<AttachedFile extraDescriptionUri={extraDescriptionUri} />
<AttachedFile {...{ extraDescriptionUri }} />
</Container>
);
};
Expand Down
19 changes: 5 additions & 14 deletions web/src/components/PreviewCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ interface IPreviewCard {
escrowTitle: string;
deliverableText: string;
receivingQuantity: string;
receivingToken: string;
transactionCreationTimestamp: string;
status: string;
token: string;
buyerAddress: string;
sendingQuantity: string;
sendingToken: string;
sellerAddress: string;
deadlineDate: string;
deadline: number;
assetSymbol: string;
overrideIsList: boolean;
extraDescriptionUri: string;
Expand All @@ -80,15 +77,12 @@ const PreviewCard: React.FC<IPreviewCard> = ({
escrowTitle,
deliverableText,
receivingQuantity,
receivingToken,
transactionCreationTimestamp,
status,
token,
buyerAddress,
sendingQuantity,
sendingToken,
sellerAddress,
deadlineDate,
deadline,
assetSymbol,
overrideIsList,
extraDescriptionUri,
Expand All @@ -108,9 +102,8 @@ const PreviewCard: React.FC<IPreviewCard> = ({
<Divider />
<TransactionInfo
amount={sendingQuantity}
assetSymbol={assetSymbol}
isPreview={true}
{...{ overrideIsList, deadlineDate, sellerAddress, buyerAddress }}
{...{ overrideIsList, deadline, sellerAddress, buyerAddress, assetSymbol }}
/>
<Divider />
</TransactionInfoContainer>
Expand All @@ -119,12 +112,10 @@ const PreviewCard: React.FC<IPreviewCard> = ({
escrowType,
deliverableText,
receivingQuantity,
receivingToken,
buyerAddress,
sendingQuantity,
sendingToken,
sellerAddress,
deadlineDate,
deadline,
assetSymbol,
extraDescriptionUri,
}}
Expand All @@ -134,7 +125,7 @@ const PreviewCard: React.FC<IPreviewCard> = ({
{...{
isPreview,
status,
token,
assetSymbol,
transactionCreationTimestamp,
buyerAddress,
sellerAddress,
Expand Down
19 changes: 11 additions & 8 deletions web/src/components/TransactionCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React from "react";
import styled from "styled-components";
import { formatEther } from "viem";
import { responsiveSize } from "styles/responsiveSize";
import { Card } from "@kleros/ui-components-library";
import { formatEther } from "viem";
import { useIsList } from "context/IsListProvider";
import TransactionInfo from "../TransactionInfo";
import StatusBanner from "./StatusBanner";
import { responsiveSize } from "styles/responsiveSize";
import { mapStatusToEnum } from "utils/mapStatusToEnum";
import { isUndefined } from "utils/index";
import { StyledSkeleton, StyledSkeletonTitle } from "../StyledSkeleton";
import TransactionInfo from "../TransactionInfo";
import StatusBanner from "./StatusBanner";
import { useNavigateAndScrollTop } from "hooks/useNavigateAndScrollTop";
import { useNativeTokenSymbol } from "hooks/useNativeTokenSymbol";
import useFetchIpfsJson from "hooks/useFetchIpfsJson";
import { useNavigateAndScrollTop } from "hooks/useNavigateAndScrollTop";
import { useTokenMetadata } from "hooks/useTokenMetadata";
import { TransactionDetailsFragment } from "src/graphql/graphql";
import { StyledSkeleton, StyledSkeletonTitle } from "../StyledSkeleton";

const StyledCard = styled(Card)`
width: 100%;
Expand Down Expand Up @@ -69,6 +70,8 @@ const TransactionCard: React.FC<ITransactionCard> = ({
const transactionInfo = useFetchIpfsJson(transactionUri);
const { isList } = useIsList();
const nativeTokenSymbol = useNativeTokenSymbol();
const { tokenMetadata } = useTokenMetadata(token);
const erc20TokenSymbol = tokenMetadata?.symbol;
const title = transactionInfo?.title;
const navigateAndScrollTop = useNavigateAndScrollTop();

Expand All @@ -83,7 +86,7 @@ const TransactionCard: React.FC<ITransactionCard> = ({
{!isUndefined(title) ? <StyledTitle>{title}</StyledTitle> : <StyledSkeleton />}
<TransactionInfo
amount={formatEther(amount)}
assetSymbol={!token ? nativeTokenSymbol : ""}
assetSymbol={!token ? nativeTokenSymbol : erc20TokenSymbol}
buyerAddress={buyer}
sellerAddress={seller}
deadlineDate={new Date(deadline * 1000).toLocaleString()}
Expand All @@ -104,7 +107,7 @@ const TransactionCard: React.FC<ITransactionCard> = ({
)}
<TransactionInfo
amount={formatEther(amount)}
assetSymbol={!token ? nativeTokenSymbol : ""}
assetSymbol={!token ? nativeTokenSymbol : erc20TokenSymbol}
buyerAddress={buyer}
sellerAddress={seller}
deadlineDate={new Date(deadline * 1000).toLocaleString()}
Expand Down
18 changes: 12 additions & 6 deletions web/src/components/TransactionInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import styled, { css } from "styled-components";
import Skeleton from "react-loading-skeleton";
import { landscapeStyle } from "styles/landscapeStyle";
import { Statuses } from "consts/statuses";
import { useIsList } from "context/IsListProvider";
Expand Down Expand Up @@ -70,7 +71,7 @@ const RestOfFieldsContainer = styled.div<{ isList?: boolean; isPreview?: boolean

export interface ITransactionInfo {
amount?: string;
deadlineDate: string;
deadline: number;
assetSymbol?: string;
status?: Statuses;
overrideIsList?: boolean;
Expand All @@ -82,7 +83,7 @@ export interface ITransactionInfo {
const TransactionInfo: React.FC<ITransactionInfo> = ({
amount,
assetSymbol,
deadlineDate,
deadline,
sellerAddress,
buyerAddress,
overrideIsList,
Expand All @@ -94,20 +95,24 @@ const TransactionInfo: React.FC<ITransactionInfo> = ({
return (
<Container isList={displayAsList} isPreview={isPreview}>
<RestOfFieldsContainer isPreview={isPreview} isList={displayAsList}>
{amount && assetSymbol ? (
{amount ? (
<Field
icon={PileCoinsIcon}
name="Amount"
value={`${amount} ${assetSymbol}`}
value={
<>
{amount} {!assetSymbol ? <Skeleton width={30} /> : assetSymbol}
</>
}
displayAsList={displayAsList}
isPreview={isPreview}
/>
) : null}
{deadlineDate ? (
{deadline ? (
<Field
icon={CalendarIcon}
name="Delivery Deadline"
value={deadlineDate}
value={new Date(deadline).toLocaleString()}
displayAsList={displayAsList}
isPreview={isPreview}
/>
Expand All @@ -134,4 +139,5 @@ const TransactionInfo: React.FC<ITransactionInfo> = ({
</Container>
);
};

export default TransactionInfo;
20 changes: 14 additions & 6 deletions web/src/context/NewTransactionContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React, { createContext, useState, useContext, useEffect } from "react";

export interface IToken {
symbol: string;
address: string;
logo: string;
}

interface INewTransactionContext {
escrowType: string;
setEscrowType: (type: string) => void;
Expand All @@ -23,8 +29,8 @@ interface INewTransactionContext {
setSellerAddress: (address: string) => void;
sendingQuantity: string;
setSendingQuantity: (quantity: string) => void;
sendingToken: string;
setSendingToken: (token: string) => void;
sendingToken: IToken;
setSendingToken: (token: IToken) => void;
buyerAddress: string;
setBuyerAddress: (address: string) => void;
deadline: string;
Expand Down Expand Up @@ -61,7 +67,7 @@ const NewTransactionContext = createContext<INewTransactionContext>({
setSellerAddress: () => {},
sendingQuantity: "",
setSendingQuantity: () => {},
sendingToken: "",
sendingToken: { address: "native", symbol: "", logo: "" },
setSendingToken: () => {},
buyerAddress: "",
setBuyerAddress: () => {},
Expand Down Expand Up @@ -93,7 +99,9 @@ export const NewTransactionProvider: React.FC<{ children: React.ReactNode }> = (
const [receivingToken, setReceivingToken] = useState<string>(localStorage.getItem("receivingToken") || "");
const [sellerAddress, setSellerAddress] = useState<string>(localStorage.getItem("sellerAddress") || "");
const [sendingQuantity, setSendingQuantity] = useState<string>(localStorage.getItem("sendingQuantity") || "");
const [sendingToken, setSendingToken] = useState<string>(localStorage.getItem("sendingToken") || "");
const [sendingToken, setSendingToken] = useState<IToken>(
JSON.parse(localStorage.getItem("sendingToken")) || { address: "native", symbol: "", logo: "" }
);
const [buyerAddress, setBuyerAddress] = useState<string>(localStorage.getItem("buyerAddress") || "");
const [isRecipientAddressResolved, setIsRecipientAddressResolved] = useState(false);
const [deadline, setDeadline] = useState<string>(localStorage.getItem("deadline") || "");
Expand All @@ -111,7 +119,7 @@ export const NewTransactionProvider: React.FC<{ children: React.ReactNode }> = (
setReceivingToken("");
setSellerAddress("");
setSendingQuantity("");
setSendingToken("");
setSendingToken({ address: "native", symbol: "", logo: "" });
setBuyerAddress("");
setDeadline("");
setNotificationEmail("");
Expand All @@ -128,7 +136,7 @@ export const NewTransactionProvider: React.FC<{ children: React.ReactNode }> = (
localStorage.setItem("receivingToken", receivingToken);
localStorage.setItem("buyerAddress", buyerAddress);
localStorage.setItem("sendingQuantity", sendingQuantity);
localStorage.setItem("sendingToken", sendingToken);
localStorage.setItem("sendingToken", JSON.stringify(sendingToken));
localStorage.setItem("sellerAddress", sellerAddress);
localStorage.setItem("deadline", deadline);
localStorage.setItem("notificationEmail", notificationEmail);
Expand Down
Loading
Loading