From 5de89e920f91d2c167b62ee417138c16e21ecee2 Mon Sep 17 00:00:00 2001 From: Pascal Weber Date: Mon, 13 Mar 2023 15:47:03 -0600 Subject: [PATCH 1/2] enhance logic to allow fetching a token's name --- src/core/getTokenInfo.ts | 37 ++++++++++++++++----- src/helpers/__tests__/validPayment.test.tsx | 16 ++++++--- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/core/getTokenInfo.ts b/src/core/getTokenInfo.ts index 5ee958b..3f6f635 100644 --- a/src/core/getTokenInfo.ts +++ b/src/core/getTokenInfo.ts @@ -3,17 +3,35 @@ import { ETH_ADDRESS, isSameAddress } from "../helpers"; import { IToken } from "../typing"; import { getJsonRpcProvider } from "./internal/getJsonRpcProvider"; -const fetchTokenInfo = async (tokenAddress: string) => { +interface ITokenWithName extends IToken { + name?: string; +} + +const fetchTokenInfo = async (tokenAddress: string, withName?: boolean) => { const provider = getJsonRpcProvider(); const token = ERC20__factory.connect(tokenAddress, provider!); try { await token.symbol(); - return Promise.all([token.symbol(), token.decimals()]).then((results) => ({ - address: tokenAddress, - symbol: results[0], - decimals: results[1], - })); + const promises = [token.symbol(), token.decimals()]; + return Promise.all(withName ? [...promises, token.name()] : promises).then((results) => { + const [symbol, decimals] = results; + const result = { + address: tokenAddress, + symbol, + decimals + }; + + if (withName) { + const name = results[3]; + return { + ...result, + name, + } as ITokenWithName; + } + + return result as IToken; + }); } catch (e) { console.error(e); } @@ -27,7 +45,8 @@ const fetchTokenInfo = async (tokenAddress: string) => { */ export const getTokenInfo = async ( tokenAddress = ETH_ADDRESS, -): Promise => { + withName?: boolean, +): Promise => { if (isSameAddress(tokenAddress, ETH_ADDRESS)) { return { address: tokenAddress, @@ -37,7 +56,7 @@ export const getTokenInfo = async ( } const storedValue = window.localStorage.getItem(tokenAddress); - let tokenInfo: IToken | null = null; + let tokenInfo: IToken | ITokenWithName = null; // There was some value stored if (storedValue) { @@ -50,7 +69,7 @@ export const getTokenInfo = async ( } console.info("fetching new information"); - tokenInfo = await fetchTokenInfo(tokenAddress); + tokenInfo = await fetchTokenInfo(tokenAddress, withName); if (tokenInfo) { window.localStorage.setItem(tokenAddress, JSON.stringify(tokenInfo)); diff --git a/src/helpers/__tests__/validPayment.test.tsx b/src/helpers/__tests__/validPayment.test.tsx index 0a72e0b..c9eb9d9 100644 --- a/src/helpers/__tests__/validPayment.test.tsx +++ b/src/helpers/__tests__/validPayment.test.tsx @@ -25,7 +25,9 @@ describe("Valid payments function", () => { ...params, seller: "0x7bD733DBc10A1cD04e1e51cC89450941c928", }), - ).rejects.toThrow("seller is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928."); + ).rejects.toThrow( + "seller is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928.", + ); }); it("Should throw an error given an invalid marketplace address ", async () => { @@ -34,7 +36,9 @@ describe("Valid payments function", () => { ...params, marketplace: "0x7bD733DBc10A1cD04e1e51cC89450941c928", }), - ).rejects.toThrow("marketplace is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928."); + ).rejects.toThrow( + "marketplace is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928.", + ); }); it("Should throw an error given an invalid arbitrator address ", async () => { @@ -43,7 +47,9 @@ describe("Valid payments function", () => { ...params, marketplace: "0x7bD733DBc10A1cD04e1e51cC89450941c928", }), - ).rejects.toThrow("marketplace is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928."); + ).rejects.toThrow( + "marketplace is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928.", + ); }); it("Should throw an error given an invalid token address ", async () => { @@ -52,7 +58,9 @@ describe("Valid payments function", () => { ...params, marketplace: "0x7bD733DBc10A1cD04e1e51cC89450941c928", }), - ).rejects.toThrow("marketplace is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928."); + ).rejects.toThrow( + "marketplace is invalid: 0x7bD733DBc10A1cD04e1e51cC89450941c928.", + ); }); it("Should throw an error given invalid amount", async () => { From ba886044256c5a7cf9a97f30cf2999ead4465910 Mon Sep 17 00:00:00 2001 From: Pascal Weber Date: Tue, 14 Mar 2023 09:33:38 -0600 Subject: [PATCH 2/2] fix payment props typing, format/rome --- src/core/getTokenInfo.ts | 34 ++++++++++++++++++---------------- src/typing/index.ts | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/core/getTokenInfo.ts b/src/core/getTokenInfo.ts index 3f6f635..b84c5a5 100644 --- a/src/core/getTokenInfo.ts +++ b/src/core/getTokenInfo.ts @@ -14,24 +14,26 @@ const fetchTokenInfo = async (tokenAddress: string, withName?: boolean) => { try { await token.symbol(); const promises = [token.symbol(), token.decimals()]; - return Promise.all(withName ? [...promises, token.name()] : promises).then((results) => { - const [symbol, decimals] = results; - const result = { - address: tokenAddress, - symbol, - decimals - }; + return Promise.all(withName ? [...promises, token.name()] : promises).then( + (results) => { + const [symbol, decimals] = results; + const result = { + address: tokenAddress, + symbol, + decimals, + }; - if (withName) { - const name = results[3]; - return { - ...result, - name, - } as ITokenWithName; - } + if (withName) { + const name = results[3]; + return { + ...result, + name, + } as ITokenWithName; + } - return result as IToken; - }); + return result as IToken; + }, + ); } catch (e) { console.error(e); } diff --git a/src/typing/index.ts b/src/typing/index.ts index 5b5adfb..2e3ad09 100644 --- a/src/typing/index.ts +++ b/src/typing/index.ts @@ -837,7 +837,7 @@ export interface IClaimTransactionCallbacks } export interface IPaymentModalProps { - paymentProps: IPaymentProps; + paymentProps: IPaymentPropsData; deferredPromise: Deferred; callbacks?: IPayTransactionCallbacks; }