Skip to content

Commit

Permalink
Rework Linter (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored May 17, 2021
1 parent 917cf5a commit 7a54b08
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 347 deletions.
7 changes: 1 addition & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
plugins: ["import", "prettier"],
extends: [
"plugin:import/recommended",
"plugin:import/typescript",
"plugin:prettier/recommended",
"react-app",
],
extends: ["plugin:import/recommended", "plugin:import/typescript", "plugin:prettier/recommended", "react-app"],
ignorePatterns: ["build/", "node_modules/", "!.prettierrc.js", "lib/"],
rules: {
"import/order": [
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 120,
"trailingComma": "all",
"singleQuote": false
}
3 changes: 1 addition & 2 deletions config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ module.exports = {
config.headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
"Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization",
};

return config;
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.7.2",
"eslint-plugin-import": "^2.23.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.23.2",
"eslint-plugin-react-hooks": "^4.2.0",
Expand Down
33 changes: 12 additions & 21 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ const App: React.FC = () => {
const { tokenList, isLoading } = useTokenList();
const [submitting, setSubmitting] = useState(false);
const [transferContent, setTransferContent] = useState<Payment[]>([]);
const [csvText, setCsvText] = useState<string>(
"token_address,receiver,amount,decimals"
);
const { addMessage, setCodeWarnings, setMessages } =
useContext(MessageContext);
const [csvText, setCsvText] = useState<string>("token_address,receiver,amount,decimals");
const { addMessage, setCodeWarnings, setMessages } = useContext(MessageContext);

const web3Provider = useMemo(
() => new ethers.providers.Web3Provider(new SafeAppProvider(safe, sdk)),
[sdk, safe]
);
const web3Provider = useMemo(() => new ethers.providers.Web3Provider(new SafeAppProvider(safe, sdk)), [sdk, safe]);

const onChangeTextHandler = useCallback(
(csvText: string) => {
Expand All @@ -39,23 +33,20 @@ const App: React.FC = () => {
.then(([transfers, warnings]) => {
console.log("CSV parsed!");
const summary = transfersToSummary(transfers);
checkAllBalances(summary, web3Provider, safe, tokenList).then(
(insufficientBalances) =>
setMessages(
insufficientBalances.map((insufficientBalanceInfo) => ({
message: `Insufficient Balance: ${insufficientBalanceInfo.transferAmount} of ${insufficientBalanceInfo.token}`,
severity: "warning",
}))
)
checkAllBalances(summary, web3Provider, safe, tokenList).then((insufficientBalances) =>
setMessages(
insufficientBalances.map((insufficientBalanceInfo) => ({
message: `Insufficient Balance: ${insufficientBalanceInfo.transferAmount} of ${insufficientBalanceInfo.token}`,
severity: "warning",
})),
),
);
setTransferContent(transfers);
setCodeWarnings(warnings);
})
.catch((reason: any) =>
addMessage({ severity: "error", message: reason.message })
);
.catch((reason: any) => addMessage({ severity: "error", message: reason.message }));
},
[addMessage, safe, setCodeWarnings, setMessages, tokenList, web3Provider]
[addMessage, safe, setCodeWarnings, setMessages, tokenList, web3Provider],
);

const submitTx = useCallback(async () => {
Expand Down
88 changes: 19 additions & 69 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,95 +38,53 @@ describe("Parsing CSVs ", () => {
it("should throw errors for invalid CSVs", async () => {
// thins csv contains more values than headers in row1
const invalidCSV = "head1,header2\nvalue1,value2,value3";
expect(parseCSV(invalidCSV, tokenList)).to.be.rejectedWith(
"column header mismatch expected: 2 columns got: 3"
);
expect(parseCSV(invalidCSV, tokenList)).to.be.rejectedWith("column header mismatch expected: 2 columns got: 3");
});

it("should transform simple, valid CSVs correctly", async () => {
const rowWithoutDecimal = [listedToken.address, validReceiverAddress, "1"];
const rowWithDecimal = [
unlistedTokenAddress,
validReceiverAddress,
"69.420",
"18",
];
const rowWithDecimal = [unlistedTokenAddress, validReceiverAddress, "69.420", "18"];
const rowWithoutTokenAddress = ["", validReceiverAddress, "1"];

const [payment, warnings] = await parseCSV(
csvStringFromRows(
rowWithoutDecimal,
rowWithDecimal,
rowWithoutTokenAddress
),
tokenList
csvStringFromRows(rowWithoutDecimal, rowWithDecimal, rowWithoutTokenAddress),
tokenList,
);
expect(warnings).to.be.empty;
expect(payment).to.have.lengthOf(3);
const [
paymentWithoutDecimal,
paymentWithDecimal,
paymentWithoutTokenAddress,
] = payment;
const [paymentWithoutDecimal, paymentWithDecimal, paymentWithoutTokenAddress] = payment;
expect(paymentWithoutDecimal.decimals).to.be.undefined;
expect(paymentWithoutDecimal.receiver).to.equal(validReceiverAddress);
expect(paymentWithoutDecimal.tokenAddress).to.equal(listedToken.address);
expect(paymentWithoutDecimal.amount.isEqualTo(new BigNumber(1))).to.be.true;

expect(paymentWithDecimal.receiver).to.equal(validReceiverAddress);
expect(paymentWithDecimal.tokenAddress.toLowerCase()).to.equal(
unlistedTokenAddress.toLowerCase()
);
expect(paymentWithDecimal.tokenAddress.toLowerCase()).to.equal(unlistedTokenAddress.toLowerCase());
expect(paymentWithDecimal.decimals).to.equal(18);
expect(paymentWithDecimal.amount.isEqualTo(new BigNumber(69.42))).to.be
.true;
expect(paymentWithDecimal.amount.isEqualTo(new BigNumber(69.42))).to.be.true;

expect(paymentWithoutTokenAddress.decimals).to.be.undefined;
expect(paymentWithoutTokenAddress.receiver).to.equal(validReceiverAddress);
expect(paymentWithoutTokenAddress.tokenAddress).to.equal(null);
expect(paymentWithoutTokenAddress.amount.isEqualTo(new BigNumber(1))).to.be
.true;
expect(paymentWithoutTokenAddress.amount.isEqualTo(new BigNumber(1))).to.be.true;
});

it("should generate validation warnings", async () => {
const rowWithNegativeAmount = [
listedToken.address,
validReceiverAddress,
"-1",
];
const rowWithInvalidDecimal = [
unlistedTokenAddress,
validReceiverAddress,
"1",
"-2",
];
const unlistedTokenWithoutDecimal = [
unlistedTokenAddress,
validReceiverAddress,
"1",
];
const rowWithInvalidTokenAddress = [
"0x420",
validReceiverAddress,
"1",
"18",
];
const rowWithInvalidReceiverAddress = [
unlistedTokenAddress,
"0x420",
"1",
"18",
];
const rowWithNegativeAmount = [listedToken.address, validReceiverAddress, "-1"];
const rowWithInvalidDecimal = [unlistedTokenAddress, validReceiverAddress, "1", "-2"];
const unlistedTokenWithoutDecimal = [unlistedTokenAddress, validReceiverAddress, "1"];
const rowWithInvalidTokenAddress = ["0x420", validReceiverAddress, "1", "18"];
const rowWithInvalidReceiverAddress = [unlistedTokenAddress, "0x420", "1", "18"];

const [payment, warnings] = await parseCSV(
csvStringFromRows(
rowWithNegativeAmount,
rowWithInvalidDecimal,
unlistedTokenWithoutDecimal,
rowWithInvalidTokenAddress,
rowWithInvalidReceiverAddress
rowWithInvalidReceiverAddress,
),
tokenList
tokenList,
);
expect(warnings).to.have.lengthOf(5);
const [
Expand All @@ -138,26 +96,18 @@ describe("Parsing CSVs ", () => {
] = warnings;
expect(payment).to.be.empty;

expect(warningNegativeAmount.message).to.equal(
"Only positive amounts possible: -1"
);
expect(warningNegativeAmount.message).to.equal("Only positive amounts possible: -1");
expect(warningNegativeAmount.lineNo).to.equal(1);
expect(warningNegativeDecimals.message).to.equal("Invalid decimals: -2");
expect(warningNegativeDecimals.lineNo).to.equal(2);

expect(warningUndefinedDecimals.message).to.equal(
"Invalid decimals: undefined"
);
expect(warningUndefinedDecimals.message).to.equal("Invalid decimals: undefined");
expect(warningUndefinedDecimals.lineNo).to.equal(3);

expect(warningInvalidTokenAddress.message).to.equal(
"Invalid Token Address: 0x420"
);
expect(warningInvalidTokenAddress.message).to.equal("Invalid Token Address: 0x420");
expect(warningInvalidTokenAddress.lineNo).to.equal(4);

expect(warningInvalidReceiverAddress.message).to.equal(
"Invalid Receiver Address: 0x420"
);
expect(warningInvalidReceiverAddress.message).to.equal("Invalid Receiver Address: 0x420");
expect(warningInvalidReceiverAddress.lineNo).to.equal(5);
});
});
44 changes: 10 additions & 34 deletions src/__tests__/transfers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,17 @@ describe("Build Transfers:", () => {
},
];

const [listedTransfer, unlistedTransfer, nativeTransfer] = buildTransfers(
largePayments,
tokenList
);
const [listedTransfer, unlistedTransfer, nativeTransfer] = buildTransfers(largePayments, tokenList);
expect(listedTransfer.value).to.be.equal("0");
expect(listedTransfer.to).to.be.equal(listedToken.address);
expect(listedTransfer.data).to.be.equal(
erc20Interface.encodeFunctionData("transfer", [
receiver,
MAX_U256.toFixed(),
])
erc20Interface.encodeFunctionData("transfer", [receiver, MAX_U256.toFixed()]),
);

expect(unlistedTransfer.value).to.be.equal("0");
expect(unlistedTransfer.to).to.be.equal(testData.unlistedToken.address);
expect(unlistedTransfer.data).to.be.equal(
erc20Interface.encodeFunctionData("transfer", [
receiver,
MAX_U256.toFixed(),
])
erc20Interface.encodeFunctionData("transfer", [receiver, MAX_U256.toFixed()]),
);

expect(nativeTransfer.value).to.be.equal(MAX_U256.toFixed());
Expand Down Expand Up @@ -106,17 +97,11 @@ describe("Build Transfers:", () => {
},
];

const [listed, unlisted, native] = buildTransfers(
smallPayments,
tokenList
);
const [listed, unlisted, native] = buildTransfers(smallPayments, tokenList);
expect(listed.value).to.be.equal("0");
expect(listed.to).to.be.equal(listedToken.address);
expect(listed.data).to.be.equal(
erc20Interface.encodeFunctionData("transfer", [
receiver,
toWei(tinyAmount, listedToken.decimals).toFixed(),
])
erc20Interface.encodeFunctionData("transfer", [receiver, toWei(tinyAmount, listedToken.decimals).toFixed()]),
);

expect(unlisted.value).to.be.equal("0");
Expand All @@ -125,7 +110,7 @@ describe("Build Transfers:", () => {
erc20Interface.encodeFunctionData("transfer", [
receiver,
toWei(tinyAmount, testData.unlistedToken.decimals).toFixed(),
])
]),
);

expect(native.value).to.be.equal(toWei(tinyAmount, 18).toString());
Expand Down Expand Up @@ -161,17 +146,11 @@ describe("Build Transfers:", () => {
},
];

const [listed, unlisted, native] = buildTransfers(
mixedPayments,
tokenList
);
const [listed, unlisted, native] = buildTransfers(mixedPayments, tokenList);
expect(listed.value).to.be.equal("0");
expect(listed.to).to.be.equal(listedToken.address);
expect(listed.data).to.be.equal(
erc20Interface.encodeFunctionData("transfer", [
receiver,
toWei(mixedAmount, listedToken.decimals).toFixed(),
])
erc20Interface.encodeFunctionData("transfer", [receiver, toWei(mixedAmount, listedToken.decimals).toFixed()]),
);

expect(unlisted.value).to.be.equal("0");
Expand All @@ -180,7 +159,7 @@ describe("Build Transfers:", () => {
erc20Interface.encodeFunctionData("transfer", [
receiver,
toWei(mixedAmount, testData.unlistedToken.decimals).toFixed(),
])
]),
);

expect(native.value).to.be.equal(toWei(mixedAmount, 18).toFixed());
Expand Down Expand Up @@ -210,10 +189,7 @@ describe("Build Transfers:", () => {
expect(transfer.value).to.be.equal("0");
expect(transfer.to).to.be.equal(crappyToken.address);
expect(transfer.data).to.be.equal(
erc20Interface.encodeFunctionData("transfer", [
receiver,
toWei(amount, crappyToken.decimals).toFixed(),
])
erc20Interface.encodeFunctionData("transfer", [receiver, toWei(amount, crappyToken.decimals).toFixed()]),
);
});
});
Expand Down
18 changes: 5 additions & 13 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ describe("toWei()", () => {
it("mixed", () => {
expect(toWei(1.234, 0).eq(ONE));
expect(toWei(1.234, 3).eq(new BigNumber(1234)));
expect(
toWei(1.00000000000000000001, 18).eq(new BigNumber(1000000000000000000))
);
expect(toWei(1.00000000000000000001, 18).eq(new BigNumber(1000000000000000000)));
});
});

Expand All @@ -43,7 +41,7 @@ describe("fromWei()", () => {
});
});

describe("transerToSummary()", () => {
describe("transferToSummary()", () => {
it("works for integer native currency", () => {
const transfers: Payment[] = [
{
Expand Down Expand Up @@ -107,9 +105,7 @@ describe("transerToSummary()", () => {
},
];
const summary = transfersToSummary(transfers);
expect(
summary.get(testData.unlistedToken.address).amount.toFixed()
).to.equal("0.111");
expect(summary.get(testData.unlistedToken.address).amount.toFixed()).to.equal("0.111");
});

it("works for integer in erc20", () => {
Expand All @@ -131,9 +127,7 @@ describe("transerToSummary()", () => {
},
];
const summary = transfersToSummary(transfers);
expect(
summary.get(testData.unlistedToken.address).amount.toFixed()
).to.equal("6");
expect(summary.get(testData.unlistedToken.address).amount.toFixed()).to.equal("6");
});

it("works for mixed payments", () => {
Expand Down Expand Up @@ -165,9 +159,7 @@ describe("transerToSummary()", () => {
},
];
const summary = transfersToSummary(transfers);
expect(
summary.get(testData.unlistedToken.address).amount.toFixed()
).to.equal("6.4");
expect(summary.get(testData.unlistedToken.address).amount.toFixed()).to.equal("6.4");
expect(summary.get(null).amount.toFixed()).to.equal("3.33");
});
});
Loading

0 comments on commit 7a54b08

Please sign in to comment.