import BigNumber from "bignumber.js"; import TokenInfoJSON from "../../tests/TokenTestInfo.json"; import { ChainId } from "../constants"; import { fetchPrices } from "./fetchers"; import { Token } from "./Token"; import { TokenAmount } from "./TokenAmount"; import { expect } from "chai"; import "mocha"; describe("Token generation from address", () => { const generated: Token[] = TokenInfoJSON.tokens.map( (el) => new Token(el.chainId, el.address) ); const expected: Token[] = TokenInfoJSON.tokens.map( (el) => new Token(el.chainId, el.address, el.decimals, el.symbol, el.name) ); before(async () => { await Promise.all(generated.map((token) => token.loadDetails())); }); expected.forEach((expToken, i) => it(`Correctly creates token object for address ${expToken.address}`, () => { const generatedToken = generated[i]; expect(generatedToken.address).equal(expToken.address); expect(generatedToken.chainId).equal(expToken.chainId); expect(generatedToken.decimals).equal(expToken.decimals); expect(generatedToken.name).equal(expToken.name); expect(generatedToken.symbol).equal(expToken.symbol); }) ); }); describe("Token equality", () => { const addressA = "0x00400FcbF0816bebB94654259de7273f4A05c762"; const addressB = "0x00Be915B9dCf56a3CBE739D9B9c202ca692409EC"; it("Returns true if tokens are same object", () => { const t1 = new Token(ChainId.Celo, addressA); expect(t1.equals(t1)).to.be.true; }); it("Returns true if tokens are equal", () => { const t1 = new Token(ChainId.Celo, addressA); const t2 = new Token(ChainId.Celo, addressA); expect(t1.equals(t2)).to.be.true; }); it("Returns false if tokens are not equal", () => { const t1 = new Token(ChainId.Celo, addressA); const t2 = new Token(ChainId.Celo, addressB); expect(t1.equals(t2)).to.be.false; }); }); // 0x73a210637f6F6B7005512677Ba6B3C96bb4AA44B describe("Token sorting order", () => { const addressA = "0x00400FcbF0816bebB94654259de7273f4A05c762"; const addressB = "0x73a210637f6F6B7005512677Ba6B3C96bb4AA44B"; // it("Errors if ChainIds are different", () => { // const t1 = new Token(ChainId.Celo, addressA); // const t2 = new Token(ChainId.Alfajores, addressB); // expect(t1.sortsBefore(t2)).toThrow(); // }); // it("Throws error if address is the same", () => { // const t1 = new Token(ChainId.Celo, addressA); // const t2 = new Token(ChainId.Celo, addressA); // expect(t1.sortsBefore(t2)).toThrowError(); // }); it("Returns true if t1 has a lower address than t2", () => { const t1 = new Token(ChainId.Celo, addressA); const t2 = new Token(ChainId.Celo, addressB); expect(t1.sortsBefore(t2)).to.be.true; }); it("Returns false if t1 has a higher address than t2", () => { const t1 = new Token(ChainId.Celo, addressB); const t2 = new Token(ChainId.Celo, addressA); expect(t1.sortsBefore(t2)); }); }); describe("Price fetcher", () => { it("Fetches prices", async () => { const res = await fetchPrices( process.env.API_KEY ?? "", ChainId.Alfajores, "brl" ); expect(Object.keys(res).length).to.be.greaterThan(0); }).timeout(30 * 1000); }); describe("Token format", () => { const token = new Token( ChainId.Alfajores, "0x00400FcbF0816bebB94654259de7273f4A05c762", 18 ); const tokenAmountA = new TokenAmount(token, new BigNumber("1.11e+18")); const tokenAmountB = new TokenAmount(token, new BigNumber("1e+18")); it("formats fixed(2) correctly", () => { const expected = "1.11"; const recieved = tokenAmountA.toFixed(2); expect(recieved).equal(expected); }); it("formats fixed(3) correctly", () => { const expected = "1.110"; const recieved = tokenAmountA.toFixed(3); expect(recieved).equal(expected); }); it("pads right correctly", () => { const expected = "1.00"; const recieved = tokenAmountB.toFixed(2); expect(recieved).equal(expected); }); }); describe("Token format", () => { const token = new Token( ChainId.Alfajores, "0x00400FcbF0816bebB94654259de7273f4A05c762", 18 ); const tokenAmountA = new TokenAmount(token, new BigNumber("1.11e+18")); const tokenAmountB = new TokenAmount(token, new BigNumber("1e+18")); it("formats fixed(2) correctly", () => { const expected = "1.11"; const recieved = tokenAmountA.toFixed(2); expect(recieved).equal(expected); }); it("formats fixed(3) correctly", () => { const expected = "1.110"; const recieved = tokenAmountA.toFixed(3); expect(recieved).equal(expected); }); it("pads right correctly", () => { const expected = "1.00"; const recieved = tokenAmountB.toFixed(2); expect(recieved).equal(expected); }); });