@@ -4,14 +4,56 @@ import { getSdk } from "../../src/shared/utils/cache/get-sdk";
44import { getChain } from "../../src/shared/utils/chain" ;
55import { thirdwebClient } from "../../src/shared/utils/sdk" ;
66import { getWalletBalance } from "thirdweb/wallets" ;
7+ import { getBalance } from "thirdweb/extensions/erc20" ;
8+ import { getContractEvents } from "thirdweb" ;
9+ import {
10+ getContract as getContractV5 ,
11+ GetContractEventsResult ,
12+ } from "thirdweb" ;
13+ import { getContract as getContractV4 } from "../../src/shared/utils/cache/get-contract" ;
14+
15+ import superjson from "superjson" ;
16+ import { BigNumber } from "ethers" ;
717
818/**
919 * need to pass THIRDWEB_API_SECRET_KEY as env when running test case
20+ * THIRDWEB_API_SECRET_KEY=<secret> npx vitest run tests/unit/migrationV5.test.ts
1021 *
1122 * todo: remove all dependencies including tests after everything is migrated properly.
1223 */
13- describe ( "migration to v5" , ( ) => {
14- it ( "get-balance" , async ( ) => {
24+ describe ( "migration from v4 to v5" , ( ) => {
25+ it ( "get-contract: check difference in contract interface" , async ( ) => {
26+ const chainId = 137 ;
27+ const contractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174" ;
28+ const walletAddress = "0xE52772e599b3fa747Af9595266b527A31611cebd" ;
29+
30+ // v4
31+ const sdk = await getSdk ( { chainId } ) ;
32+ const contractV4 = await sdk . getContract ( contractAddress ) ;
33+ const balV4 = await contractV4 . erc20 . balanceOf ( walletAddress ) ;
34+
35+ /**
36+ * v5
37+ * Doesnt have nested helper functions and is separated into individual "extensions"
38+ */
39+ const contractV5 = getContractV5 ( {
40+ client : thirdwebClient ,
41+ address : contractAddress ,
42+ chain : await getChain ( chainId ) ,
43+ } ) ;
44+ const balV5 = await getBalance ( {
45+ contract : contractV5 ,
46+ address : walletAddress ,
47+ } ) ;
48+
49+ expect ( balV4 . name ) . eq ( balV5 . name ) ;
50+ expect ( balV4 . symbol ) . eq ( balV5 . symbol ) ;
51+ expect ( balV4 . decimals ) . eq ( balV5 . decimals ) ;
52+ expect ( balV4 . displayValue ) . eq ( balV5 . displayValue ) ;
53+ expect ( balV4 . value . toString ( ) ) . eq ( balV5 . value . toString ( ) ) ;
54+ } ) ;
55+
56+ it ( "tests for get-balance(native token)" , async ( ) => {
1557 const chainId = 137 ;
1658 const walletAddress = "0xE52772e599b3fa747Af9595266b527A31611cebd" ;
1759
@@ -26,11 +68,89 @@ describe("migration to v5", () => {
2668 chain : await getChain ( chainId ) ,
2769 } ) ;
2870
29- console . log ( balanceV4 , balanceV5 ) ;
3071 expect ( balanceV4 . name ) . eq ( balanceV5 . name ) ;
3172 expect ( balanceV4 . symbol ) . eq ( balanceV5 . symbol ) ;
3273 expect ( balanceV4 . decimals ) . eq ( balanceV5 . decimals ) ;
3374 expect ( balanceV4 . displayValue ) . eq ( balanceV5 . displayValue ) ;
3475 expect ( balanceV4 . value . toString ( ) ) . eq ( balanceV5 . value . toString ( ) ) ;
3576 } ) ;
77+
78+ it ( "tests for events/get-all" , async ( ) => {
79+ const chainId = 137 ;
80+ const contractAddress = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174" ;
81+ const fromBlock = 65334800 ;
82+ const toBlock = 65334801 ;
83+ const order = "asc" ;
84+
85+ // v4
86+ const contractV4 = await getContractV4 ( { chainId, contractAddress } ) ;
87+ const eventsV4 = await contractV4 . events . getAllEvents ( {
88+ fromBlock,
89+ toBlock,
90+ order,
91+ } ) ;
92+ // console.log(eventsV4);
93+
94+ // v5.
95+ const contractV5 = getContractV5 ( {
96+ client : thirdwebClient ,
97+ address : contractAddress ,
98+ chain : await getChain ( chainId ) ,
99+ } ) ;
100+ const eventsV5 = mapEventsV4ToV5 (
101+ await getContractEvents ( {
102+ contract : contractV5 ,
103+ fromBlock : BigInt ( fromBlock ) ,
104+ toBlock : BigInt ( toBlock ) ,
105+ } ) ,
106+ order ,
107+ ) ;
108+
109+ expect ( eventsV4 . length ) . eq ( eventsV5 . length ) ;
110+ for ( let i = 0 ; i < eventsV4 . length ; i ++ ) {
111+ expect ( eventsV4 [ i ] . transaction . transactionHash ) . eq (
112+ eventsV5 [ i ] . transaction . transactionHash ,
113+ ) ;
114+ }
115+ } ) ;
116+
117+ /**
118+ * Mapping of events v5 response to v4 for backward compatiblity.
119+ * Clients may be using this api and dont want to break things.
120+ */
121+ const mapEventsV4ToV5 = ( eventsV5 , order ) => {
122+ if ( ! eventsV5 ?. length ) return [ ] ;
123+
124+ return eventsV5
125+ . map ( ( event ) => {
126+ const eventName = event . eventName ;
127+ const data = { } ;
128+
129+ // backwards compatibility of BigInt(v5) to BigNumber (v4)
130+ Object . keys ( event . args ) . forEach ( ( key ) => {
131+ let value = event . args [ key ] ;
132+ if ( typeof value == "bigint" ) {
133+ value = BigNumber . from ( value . toString ( ) ) ;
134+ }
135+ data [ key ] = value ;
136+ } ) ;
137+
138+ delete event . eventName ;
139+ delete event . args ;
140+ const transaction = event ;
141+ transaction . blockNumber = parseInt ( transaction . blockNumber ) ;
142+ transaction . event = eventName ;
143+
144+ return {
145+ eventName,
146+ data,
147+ transaction,
148+ } ;
149+ } )
150+ . sort ( ( a , b ) => {
151+ return order === "desc"
152+ ? b . transaction . blockNumber - a . transaction . blockNumber
153+ : a . transaction . blockNumber - b . transaction . blockNumber ;
154+ } ) ;
155+ } ;
36156} ) ;
0 commit comments