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

[todo] Add more robust retry logic to Point of Sale app #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 19 additions & 4 deletions point-of-sale/src/components/contexts/PaymentProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ValidateTransactionSignatureError,
} from '@solana/pay';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { ConfirmedSignatureInfo, Keypair, PublicKey, TransactionSignature } from '@solana/web3.js';
import { ConfirmedSignatureInfo, Connection, Keypair, PublicKey, TransactionSignature } from '@solana/web3.js';
import BigNumber from 'bignumber.js';
import React, { FC, ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import { useConfig } from '../../hooks/useConfig';
Expand All @@ -18,9 +18,10 @@ import { Confirmations } from '../../types';

export interface PaymentProviderProps {
children: ReactNode;
connections: Connection[];
}

export const PaymentProvider: FC<PaymentProviderProps> = ({ children }) => {
export const PaymentProvider: FC<PaymentProviderProps> = ({ children, connections }) => {
const { connection } = useConnection();
const { recipient, splToken, label, requiredConfirmations, connectWallet } = useConfig();
const { publicKey, sendTransaction } = useWallet();
Expand All @@ -32,6 +33,7 @@ export const PaymentProvider: FC<PaymentProviderProps> = ({ children }) => {
const [signature, setSignature] = useState<TransactionSignature>();
const [status, setStatus] = useState(PaymentStatus.New);
const [confirmations, setConfirmations] = useState<Confirmations>(0);
const [goodconnection, setGoodConnection] = useState<Connection>();
const navigate = useNavigateWithQuery();
const progress = useMemo(() => confirmations / requiredConfirmations, [confirmations, requiredConfirmations]);

Expand Down Expand Up @@ -110,8 +112,18 @@ export const PaymentProvider: FC<PaymentProviderProps> = ({ children }) => {
const interval = setInterval(async () => {
let signature: ConfirmedSignatureInfo;
try {
signature = await findTransactionSignature(connection, reference, undefined, 'confirmed');
const result = await Promise.any(
connections.map(async (connection) => {
return [
connection,
await findTransactionSignature(connection, reference, undefined, 'confirmed'),
];
})
);

signature = result[1] as ConfirmedSignatureInfo;
const goodConnection = result[0] as Connection;
setGoodConnection(goodConnection);
if (!changed) {
clearInterval(interval);
setSignature(signature.signature);
Expand Down Expand Up @@ -140,8 +152,11 @@ export const PaymentProvider: FC<PaymentProviderProps> = ({ children }) => {

const run = async () => {
try {
if (!goodconnection) {
return;
}
await validateTransactionSignature(
connection,
goodconnection,
signature,
recipient,
amount,
Expand Down
6 changes: 4 additions & 2 deletions point-of-sale/src/components/routes/RootRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PhantomWalletAdapter } from '@solana/wallet-adapter-phantom';
import { ConnectionProvider, WalletProvider } from '@solana/wallet-adapter-react';
import { WalletModalProvider } from '@solana/wallet-adapter-react-ui';
import { TorusWalletAdapter } from '@solana/wallet-adapter-torus';
import { PublicKey } from '@solana/web3.js';
import { PublicKey, Connection } from '@solana/web3.js';
import React, { FC, useMemo } from 'react';
import { Outlet, useSearchParams } from 'react-router-dom';
import { DEVNET_ENDPOINT } from '../../utils/constants';
Expand Down Expand Up @@ -41,6 +41,8 @@ export const RootRoute: FC = () => {
return { recipient, label };
}, [params]);

const connections = useMemo(() => [new Connection(DEVNET_ENDPOINT)], []);

return (
<ThemeProvider>
<FullscreenProvider>
Expand All @@ -58,7 +60,7 @@ export const RootRoute: FC = () => {
connectWallet={connectWallet}
>
<TransactionsProvider>
<PaymentProvider>
<PaymentProvider connections={connections}>
<Outlet />
</PaymentProvider>
</TransactionsProvider>
Expand Down