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

[UN-1609] enhance logic to allow fetching a token's name #136

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
39 changes: 30 additions & 9 deletions src/core/getTokenInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,37 @@ 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);
}
Expand All @@ -27,7 +47,8 @@ const fetchTokenInfo = async (tokenAddress: string) => {
*/
export const getTokenInfo = async (
tokenAddress = ETH_ADDRESS,
): Promise<IToken> => {
withName?: boolean,
): Promise<IToken | ITokenWithName> => {
if (isSameAddress(tokenAddress, ETH_ADDRESS)) {
return {
address: tokenAddress,
Expand All @@ -37,7 +58,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) {
Expand All @@ -50,7 +71,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));
Expand Down
16 changes: 12 additions & 4 deletions src/helpers/__tests__/validPayment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/typing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ export interface IClaimTransactionCallbacks
}

export interface IPaymentModalProps {
paymentProps: IPaymentProps;
paymentProps: IPaymentPropsData;
deferredPromise: Deferred<any>;
callbacks?: IPayTransactionCallbacks;
}
Expand Down