import { ChainId, CHAINS_TO_RPC, Interval, OverrideRPC } from "../constants"; import { expect } from "chai"; import "mocha"; import { EOA } from "./EOA"; import { WalletConfig } from "./Wallet"; import BigNumber from "bignumber.js"; import { Token, TokenAmount } from "../Tokens"; import Web3 from "web3"; import TestWalletInfo from "../../tests/data/WalletInfoData"; import RequestData from "../../tests/data/PaymentRequestData"; import PaymentRequest from "../PaymentRequest"; import { ERC20_INTERFACE } from "../constants/contracts"; import { Connection } from "@celo/connect"; const TIMEOUT = 10 * 1000; const mockGetMnemonic = (): Promise => new Promise((resolve) => resolve(TestWalletInfo.wallets[0].mnemonic)); const mockGetPrivateKey = (): Promise => new Promise((resolve) => resolve(TestWalletInfo.wallets[0].pk)); describe("EOA class", () => { const expectedEOA = TestWalletInfo.wallets[0].eoa; const configTypes: WalletConfig = { mnemonic: TestWalletInfo.wallets[0].mnemonic, privateKey: TestWalletInfo.wallets[0].pk, getMnemonic: mockGetMnemonic, getPrivateKey: mockGetPrivateKey, }; const alfajoresRPC = CHAINS_TO_RPC[ChainId.Alfajores]; afterEach(() => { OverrideRPC(ChainId.Alfajores, alfajoresRPC); }); // it("test", async () => { // const eoa = new EOA(); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // }); // Object.keys(configTypes).forEach((creationType) => // it(`Successfully creates an EOA for method ${creationType}`, async () => { // const eoa = new EOA(); // await eoa._loadWallet( // { // // @ts-ignore // [creationType]: configTypes[creationType], // }, // true // ); // expect(eoa.address).equal(expectedEOA); // }) // ); // it("Syncs portfolio", async () => { // const eoa = new EOA( // process.env.API_KEY, // ChainId.Celo, // TestWalletInfo.wallets[0].eoa // ); // const success = await eoa.syncBackendPortfolioValue(); // expect(success).true; // }).timeout(10 * 1000); // it("Aborts when getMnemonic returns undefined", async () => { // const eoa = new EOA(undefined, ChainId.Celo, TestWalletInfo.wallets[0].eoa); // eoa._loadWallet( // { // getMnemonic: () => new Promise((resolve) => resolve(undefined)), // }, // true // ); // expect(eoa.address).equal(TestWalletInfo.wallets[0].eoa); // }); // it("Allows for a custom gas token to be set on construction", async () => { // const feeCurrency = "0xrandom_address"; // const eoa = new EOA( // undefined, // ChainId.Celo, // TestWalletInfo.wallets[0].eoa, // { // defaultGasToken: feeCurrency, // } // ); // expect(eoa.feeCurrency).equal(feeCurrency); // }); // it("Can send tokens", async () => { // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet(configTypes, true); // await eoa.transferToken( // "0xa4449bB3AC733c04B7Aa6E7878eE70B259a8ea23", // new BigNumber("100"), // eoa.address ?? "" // ); // }).timeout(TIMEOUT); // it("Can send tokens from token object", async () => { // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ getMnemonic: configTypes.getMnemonic }, true); // const token = new Token( // ChainId.Alfajores, // "0xa4449bB3AC733c04B7Aa6E7878eE70B259a8ea23" // ); // await token.send(new BigNumber("1000"), eoa, eoa.address ?? ""); // }).timeout(TIMEOUT); // it("Can use alternate gas tokens", async () => { // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // const token = new Token( // ChainId.Alfajores, // "0xa4449bB3AC733c04B7Aa6E7878eE70B259a8ea23" // ); // eoa.setFeeCurrency("0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1"); // await token.send(new BigNumber("1000"), eoa, eoa.address ?? ""); // }).timeout(TIMEOUT); // it("Syncs wallet with backend", async () => { // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa.syncBackendPortfolioValue(); // }).timeout(TIMEOUT); // it("Fetches historical portfolio value", async () => { // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // const resp = await eoa.fetchBackendPortfolioValue( // Interval.Hour, // "week", // "brl" // ); // expect(resp); // }).timeout(TIMEOUT); // it("Declares a custom RPC endpoint", async () => { // const my_rpc = { // http: "http", // wss: "wss", // }; // OverrideRPC(ChainId.Alfajores, my_rpc); // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // expect(eoa.rpc?.http).equal(my_rpc.http); // expect(eoa?.rpc?.wss).equal(my_rpc.wss); // }); // it("Declaring a custom RPC endpoint should only affect the intended chain", async () => { // const my_rpc = { // http: "http", // wss: "wss", // }; // const expected = { // http: "https://necessary-muddy-research.celo-mainnet.discover.quiknode.pro/3577f3bb994031ee8c5dfa75313b24bfa4cffd1e/", // wss: "wss://necessary-muddy-research.celo-mainnet.discover.quiknode.pro/3577f3bb994031ee8c5dfa75313b24bfa4cffd1e/", // }; // OverrideRPC(ChainId.Alfajores, my_rpc); // const eoa = new EOA(undefined, ChainId.Celo, TestWalletInfo.wallets[0].eoa); // expect(eoa.rpc?.http).equal(expected.http); // expect(eoa?.rpc?.wss).equal(expected.wss); // }); // it("Can sign a message", async () => { // const message = "Hello!"; // const expectedAddress = TestWalletInfo.wallets[0].eoa; // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // const signatureObj = eoa.signMessage(message); // const foundAddress = eoa.web3.eth.accounts.recover(signatureObj as any); // expect(foundAddress).equal(expectedAddress); // }); // it("Can sign a message from mnemonic", async () => { // const message = "proving_identity_1660056511488"; // const expectedAddress = TestWalletInfo.wallets[0].eoa; // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ mnemonic: configTypes.mnemonic }, true); // const signatureObj = eoa.signMessage(message); // const foundAddress = eoa.web3.eth.accounts.recover(signatureObj as any); // expect(foundAddress).equal(expectedAddress); // }); // it("Can sign a transaction", async () => { // const eoa = new EOA(undefined, ChainId.Celo, TestWalletInfo.wallets[0].eoa); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // const signatureObj = eoa.web3 // ? await eoa.web3.eth.accounts.signTransaction( // { // to: "0x73a210637f6f6b7005512677ba6b3c96bb4aa44b", // data: "0x095ea7b3000000000000000000000000f35ed7156babf2541e032b3bb8625210316e2832000000000000000000000000000000000000000000000002b5e3af16b1880000", // from: TestWalletInfo.wallets[0].eoa, // gas: "8000000", // }, // configTypes.privateKey ?? "0x" // ) // : {}; // const foundAddress = eoa.web3.eth.accounts.recover(signatureObj as any); // expect(foundAddress).equal(TestWalletInfo.wallets[0].eoa); // }); // it("Can sign a message, should catch incorrect signature", async () => { // const message = "Hello!"; // const expectedAddressNotToBe = "0x16E319d8dAFeF25AAcec0dF0f1E349819D36993c"; // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // const signatureObj = eoa.signMessage(message); // const foundAddress = eoa.web3.eth.accounts.recover(signatureObj as any); // expect(foundAddress).not.equal(expectedAddressNotToBe); // }); // it("Can sign typed data", async () => { // const data = [ // { type: "uint256", value: "1" }, // { type: "uint256", value: "1655135997" }, // { type: "address", value: "0x00400FcbF0816bebB94654259de7273f4A05c762" }, // { type: "address", value: "0x00Be915B9dCf56a3CBE739D9B9c202ca692409EC" }, // ]; // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // const signatureObj = eoa.signTypedData(data); // const foundAddress = eoa.web3.eth.accounts.recover(signatureObj as any); // expect(foundAddress.toLowerCase()).to.equal(eoa.address?.toLowerCase()); // }); // it("Can fetch payment requests", async () => { // const [request] = RequestData.requests; // const eoa = new EOA(undefined, ChainId.Alfajores, request.payer); // const { requests, pageInfo } = await eoa.fetchPaymentRequests( // () => new Token(request.chain, request.token, 18), // { // type: "pending", // page: 0, // count: 10, // } // ); // const containsRequest = requests.find((el) => el.info.id === request.id); // expect(containsRequest).to.exist; // expect(containsRequest?.info.payer).to.eq(request.payer); // expect(pageInfo.currentPage).to.eq(1); // expect(pageInfo.perPage).to.eq(10); // }); // it("Can fulfill a paymentrequest", async () => { // const { id: rid, ...baseReq } = RequestData.requests[1]; // const testToken = new Token(baseReq.chain, baseReq.token, 18); // const request = PaymentRequest.deserialize( // { rid, ...baseReq }, // (addr: string) => new Token(baseReq.chain, addr, 18) // ); // const eoa = new EOA( // undefined, // ChainId.Alfajores, // TestWalletInfo.wallets[0].eoa // ); // await eoa._loadWallet({ privateKey: configTypes.privateKey }, true); // const amountToFulfill = new TokenAmount(testToken, new BigNumber("100")); // const { request: newRequest, receipt } = await eoa // .fulfillPaymentRequest(request, amountToFulfill) // .on("transfer", (d) => { // expect(d).to.exist; // }) // .catch((e) => console.error(e)) // .then( // (r) => // r as { // request: PaymentRequest>; // receipt: { // amountCredited: TokenAmount; // isFullyRepaid: boolean; // hash: string; // rid: number; // time: number; // }; // } // ); // const amountCredited = request.info.amount.raw // .minus(newRequest.info.amount.raw) // .toFixed(0); // const expectedAmountCredited = "100"; // expect(amountCredited).to.eq(expectedAmountCredited); // expect(receipt.amountCredited.raw.toFixed(0)).to.eq(expectedAmountCredited); // }).timeout(TIMEOUT); });