|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { getSdk } from "../../src/shared/utils/cache/get-sdk"; |
| 4 | +import { getChain } from "../../src/shared/utils/chain"; |
| 5 | +import { thirdwebClient } from "../../src/shared/utils/sdk"; |
| 6 | +import { getWalletBalance } from "thirdweb/wallets"; |
| 7 | +import { getBalance } from "thirdweb/extensions/erc20"; |
| 8 | +import { getContractEvents } from "thirdweb"; |
| 9 | +import { getContract as getContractV5 } from "thirdweb"; |
| 10 | +import { getContract as getContractV4 } from "../../src/shared/utils/cache/get-contract"; |
| 11 | +import { maybeBigInt } from "../../src/shared/utils/primitive-types"; |
| 12 | +import { toContractEventV4Schema } from "../../src/server/schemas/event"; |
| 13 | + |
| 14 | +/** |
| 15 | + * todo: remove all dependencies including tests after everything is migrated properly. |
| 16 | + */ |
| 17 | +describe("migration from v4 to v5", () => { |
| 18 | + it("get-contract: check difference in contract interface", async () => { |
| 19 | + const chainId = 137; |
| 20 | + const contractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"; |
| 21 | + const walletAddress = "0xE52772e599b3fa747Af9595266b527A31611cebd"; |
| 22 | + |
| 23 | + // v4 |
| 24 | + const sdk = await getSdk({ chainId }); |
| 25 | + const contractV4 = await sdk.getContract(contractAddress); |
| 26 | + const balV4 = await contractV4.erc20.balanceOf(walletAddress); |
| 27 | + |
| 28 | + /** |
| 29 | + * v5 |
| 30 | + * Doesnt have nested helper functions and is separated into individual "extensions" |
| 31 | + */ |
| 32 | + const contractV5 = getContractV5({ |
| 33 | + client: thirdwebClient, |
| 34 | + address: contractAddress, |
| 35 | + chain: await getChain(chainId), |
| 36 | + }); |
| 37 | + const balV5 = await getBalance({ |
| 38 | + contract: contractV5, |
| 39 | + address: walletAddress, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(balV4.name).eq(balV5.name); |
| 43 | + expect(balV4.symbol).eq(balV5.symbol); |
| 44 | + expect(balV4.decimals).eq(balV5.decimals); |
| 45 | + expect(balV4.displayValue).eq(balV5.displayValue); |
| 46 | + expect(balV4.value.toString()).eq(balV5.value.toString()); |
| 47 | + }); |
| 48 | + |
| 49 | + it("tests for get-balance(native token)", async () => { |
| 50 | + const chainId = 137; |
| 51 | + const walletAddress = "0xE52772e599b3fa747Af9595266b527A31611cebd"; |
| 52 | + |
| 53 | + // v4 |
| 54 | + const sdk = await getSdk({ chainId }); |
| 55 | + const balanceV4 = await sdk.getBalance(walletAddress); |
| 56 | + |
| 57 | + // v5. |
| 58 | + const balanceV5 = await getWalletBalance({ |
| 59 | + client: thirdwebClient, |
| 60 | + address: walletAddress, |
| 61 | + chain: await getChain(chainId), |
| 62 | + }); |
| 63 | + |
| 64 | + expect(balanceV4.name).eq(balanceV5.name); |
| 65 | + expect(balanceV4.symbol).eq(balanceV5.symbol); |
| 66 | + expect(balanceV4.decimals).eq(balanceV5.decimals); |
| 67 | + expect(balanceV4.displayValue).eq(balanceV5.displayValue); |
| 68 | + expect(balanceV4.value.toString()).eq(balanceV5.value.toString()); |
| 69 | + }); |
| 70 | + |
| 71 | + it("tests for events/get-all", async () => { |
| 72 | + const chainId = 137; |
| 73 | + const contractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"; |
| 74 | + const fromBlock = 65334800; |
| 75 | + const toBlock = 65334801; |
| 76 | + const order = Math.random() > 0.5 ? "asc" : "desc"; |
| 77 | + |
| 78 | + // v4 |
| 79 | + const contractV4 = await getContractV4({ chainId, contractAddress }); |
| 80 | + const eventsV4 = await contractV4.events.getAllEvents({ |
| 81 | + fromBlock, |
| 82 | + toBlock, |
| 83 | + order, |
| 84 | + }); |
| 85 | + |
| 86 | + // v5. |
| 87 | + const contractV5 = getContractV5({ |
| 88 | + client: thirdwebClient, |
| 89 | + address: contractAddress, |
| 90 | + chain: await getChain(chainId), |
| 91 | + }); |
| 92 | + const eventsV5Raw = await getContractEvents({ |
| 93 | + contract: contractV5, |
| 94 | + fromBlock: maybeBigInt(fromBlock?.toString()), |
| 95 | + toBlock: maybeBigInt(toBlock?.toString()), |
| 96 | + }); |
| 97 | + |
| 98 | + const eventsV5 = eventsV5Raw.map(toContractEventV4Schema).sort((a, b) => { |
| 99 | + return order === "desc" |
| 100 | + ? b.transaction.blockNumber - a.transaction.blockNumber |
| 101 | + : a.transaction.blockNumber - b.transaction.blockNumber; |
| 102 | + }); |
| 103 | + |
| 104 | + // check two array ordering is the same |
| 105 | + expect(eventsV4.length).eq(eventsV5.length); |
| 106 | + for (let i = 0; i < eventsV4.length; i++) { |
| 107 | + expect(eventsV4[i].transaction.transactionHash).eq( |
| 108 | + eventsV5[i].transaction.transactionHash, |
| 109 | + ); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments