@net-protocol/score 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { readContract } from 'viem/actions';
2
- import { getPublicClient } from '@net-protocol/core';
3
- import { decodeAbiParameters, keccak256, encodePacked, getAddress, encodeAbiParameters } from 'viem';
2
+ import { getPublicClient, getBaseDataSuffix } from '@net-protocol/core';
3
+ import { decodeAbiParameters, keccak256, encodePacked, getAddress, encodeAbiParameters, isAddressEqual } from 'viem';
4
4
  import { getStorageKeyBytes } from '@net-protocol/storage';
5
5
 
6
6
  // src/client/ScoreClient.ts
@@ -1932,6 +1932,467 @@ var multi_version_uniswap_pool_info_retriever_default = [
1932
1932
  }
1933
1933
  ];
1934
1934
 
1935
+ // src/abis/user-upvote.json
1936
+ var user_upvote_default = [
1937
+ {
1938
+ type: "constructor",
1939
+ inputs: [
1940
+ { name: "_alphaToken", type: "address", internalType: "address" },
1941
+ { name: "_erc20TokenGatedChat", type: "address", internalType: "address" },
1942
+ { name: "_net", type: "address", internalType: "address" },
1943
+ { name: "_swapRouter", type: "address", internalType: "address" },
1944
+ { name: "_weth", type: "address", internalType: "address" },
1945
+ { name: "_upvotePrice", type: "uint256", internalType: "uint256" },
1946
+ { name: "_feeBps", type: "uint256", internalType: "uint256" }
1947
+ ],
1948
+ stateMutability: "nonpayable"
1949
+ },
1950
+ {
1951
+ type: "function",
1952
+ name: "ALPHA_TOKEN",
1953
+ inputs: [],
1954
+ outputs: [{ name: "", type: "address", internalType: "address" }],
1955
+ stateMutability: "view"
1956
+ },
1957
+ {
1958
+ type: "function",
1959
+ name: "ERC20_TOKEN_GATED_CHAT",
1960
+ inputs: [],
1961
+ outputs: [{ name: "", type: "address", internalType: "address" }],
1962
+ stateMutability: "view"
1963
+ },
1964
+ {
1965
+ type: "function",
1966
+ name: "ETH_USDC_POOL",
1967
+ inputs: [],
1968
+ outputs: [{ name: "", type: "address", internalType: "address" }],
1969
+ stateMutability: "view"
1970
+ },
1971
+ {
1972
+ type: "function",
1973
+ name: "MAX_TOKENS_PER_USER",
1974
+ inputs: [],
1975
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
1976
+ stateMutability: "view"
1977
+ },
1978
+ {
1979
+ type: "function",
1980
+ name: "NET",
1981
+ inputs: [],
1982
+ outputs: [{ name: "", type: "address", internalType: "address" }],
1983
+ stateMutability: "view"
1984
+ },
1985
+ {
1986
+ type: "function",
1987
+ name: "SWAP_ROUTER",
1988
+ inputs: [],
1989
+ outputs: [{ name: "", type: "address", internalType: "address" }],
1990
+ stateMutability: "view"
1991
+ },
1992
+ {
1993
+ type: "function",
1994
+ name: "UNISWAP_V3_FACTORY",
1995
+ inputs: [],
1996
+ outputs: [{ name: "", type: "address", internalType: "address" }],
1997
+ stateMutability: "view"
1998
+ },
1999
+ {
2000
+ type: "function",
2001
+ name: "WETH",
2002
+ inputs: [],
2003
+ outputs: [{ name: "", type: "address", internalType: "address" }],
2004
+ stateMutability: "view"
2005
+ },
2006
+ {
2007
+ type: "function",
2008
+ name: "addTokenToList",
2009
+ inputs: [
2010
+ { name: "token", type: "address", internalType: "address" },
2011
+ { name: "feeTier", type: "uint24", internalType: "uint24" }
2012
+ ],
2013
+ outputs: [],
2014
+ stateMutability: "nonpayable"
2015
+ },
2016
+ {
2017
+ type: "function",
2018
+ name: "feeBps",
2019
+ inputs: [],
2020
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2021
+ stateMutability: "view"
2022
+ },
2023
+ {
2024
+ type: "function",
2025
+ name: "getTokenFeeTier",
2026
+ inputs: [
2027
+ { name: "user", type: "address", internalType: "address" },
2028
+ { name: "token", type: "address", internalType: "address" }
2029
+ ],
2030
+ outputs: [{ name: "", type: "uint24", internalType: "uint24" }],
2031
+ stateMutability: "view"
2032
+ },
2033
+ {
2034
+ type: "function",
2035
+ name: "getTotalUpvotesPerToken",
2036
+ inputs: [
2037
+ { name: "token", type: "address", internalType: "address" }
2038
+ ],
2039
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2040
+ stateMutability: "view"
2041
+ },
2042
+ {
2043
+ type: "function",
2044
+ name: "getUserTokenCount",
2045
+ inputs: [
2046
+ { name: "user", type: "address", internalType: "address" }
2047
+ ],
2048
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2049
+ stateMutability: "view"
2050
+ },
2051
+ {
2052
+ type: "function",
2053
+ name: "getUserTokensInRange",
2054
+ inputs: [
2055
+ { name: "user", type: "address", internalType: "address" },
2056
+ { name: "startIndex", type: "uint256", internalType: "uint256" },
2057
+ { name: "endIndex", type: "uint256", internalType: "uint256" }
2058
+ ],
2059
+ outputs: [
2060
+ {
2061
+ name: "",
2062
+ type: "tuple[]",
2063
+ internalType: "struct UserUpvoteContract.TokenInfo[]",
2064
+ components: [
2065
+ { name: "token", type: "address", internalType: "address" },
2066
+ { name: "feeTier", type: "uint24", internalType: "uint24" }
2067
+ ]
2068
+ }
2069
+ ],
2070
+ stateMutability: "view"
2071
+ },
2072
+ {
2073
+ type: "function",
2074
+ name: "getUserUpvotesGiven",
2075
+ inputs: [
2076
+ { name: "user", type: "address", internalType: "address" }
2077
+ ],
2078
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2079
+ stateMutability: "view"
2080
+ },
2081
+ {
2082
+ type: "function",
2083
+ name: "getUserUpvotesGivenPerToken",
2084
+ inputs: [
2085
+ { name: "user", type: "address", internalType: "address" },
2086
+ { name: "token", type: "address", internalType: "address" }
2087
+ ],
2088
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2089
+ stateMutability: "view"
2090
+ },
2091
+ {
2092
+ type: "function",
2093
+ name: "getUserUpvotesGivenPerTokenBatch",
2094
+ inputs: [
2095
+ { name: "user", type: "address", internalType: "address" },
2096
+ { name: "tokens", type: "address[]", internalType: "address[]" }
2097
+ ],
2098
+ outputs: [
2099
+ { name: "", type: "uint256[]", internalType: "uint256[]" }
2100
+ ],
2101
+ stateMutability: "view"
2102
+ },
2103
+ {
2104
+ type: "function",
2105
+ name: "getUserUpvotesReceived",
2106
+ inputs: [
2107
+ { name: "user", type: "address", internalType: "address" }
2108
+ ],
2109
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2110
+ stateMutability: "view"
2111
+ },
2112
+ {
2113
+ type: "function",
2114
+ name: "getUserUpvotesReceivedPerToken",
2115
+ inputs: [
2116
+ { name: "user", type: "address", internalType: "address" },
2117
+ { name: "token", type: "address", internalType: "address" }
2118
+ ],
2119
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2120
+ stateMutability: "view"
2121
+ },
2122
+ {
2123
+ type: "function",
2124
+ name: "getUserUpvotesReceivedPerTokenBatch",
2125
+ inputs: [
2126
+ { name: "user", type: "address", internalType: "address" },
2127
+ { name: "tokens", type: "address[]", internalType: "address[]" }
2128
+ ],
2129
+ outputs: [
2130
+ { name: "", type: "uint256[]", internalType: "uint256[]" }
2131
+ ],
2132
+ stateMutability: "view"
2133
+ },
2134
+ {
2135
+ type: "function",
2136
+ name: "isTokenInUserList",
2137
+ inputs: [
2138
+ { name: "user", type: "address", internalType: "address" },
2139
+ { name: "token", type: "address", internalType: "address" }
2140
+ ],
2141
+ outputs: [{ name: "", type: "bool", internalType: "bool" }],
2142
+ stateMutability: "view"
2143
+ },
2144
+ {
2145
+ type: "function",
2146
+ name: "owner",
2147
+ inputs: [],
2148
+ outputs: [{ name: "", type: "address", internalType: "address" }],
2149
+ stateMutability: "view"
2150
+ },
2151
+ {
2152
+ type: "function",
2153
+ name: "removeTokenFromList",
2154
+ inputs: [
2155
+ { name: "token", type: "address", internalType: "address" }
2156
+ ],
2157
+ outputs: [],
2158
+ stateMutability: "nonpayable"
2159
+ },
2160
+ {
2161
+ type: "function",
2162
+ name: "renounceOwnership",
2163
+ inputs: [],
2164
+ outputs: [],
2165
+ stateMutability: "nonpayable"
2166
+ },
2167
+ {
2168
+ type: "function",
2169
+ name: "setFeeBps",
2170
+ inputs: [
2171
+ { name: "newFeeBps", type: "uint256", internalType: "uint256" }
2172
+ ],
2173
+ outputs: [],
2174
+ stateMutability: "nonpayable"
2175
+ },
2176
+ {
2177
+ type: "function",
2178
+ name: "setUpvotePrice",
2179
+ inputs: [
2180
+ { name: "newUpvotePrice", type: "uint256", internalType: "uint256" }
2181
+ ],
2182
+ outputs: [],
2183
+ stateMutability: "nonpayable"
2184
+ },
2185
+ {
2186
+ type: "function",
2187
+ name: "totalUpvotesPerToken",
2188
+ inputs: [{ name: "", type: "address", internalType: "address" }],
2189
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2190
+ stateMutability: "view"
2191
+ },
2192
+ {
2193
+ type: "function",
2194
+ name: "transferOwnership",
2195
+ inputs: [
2196
+ { name: "newOwner", type: "address", internalType: "address" }
2197
+ ],
2198
+ outputs: [],
2199
+ stateMutability: "nonpayable"
2200
+ },
2201
+ {
2202
+ type: "function",
2203
+ name: "upvotePrice",
2204
+ inputs: [],
2205
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2206
+ stateMutability: "view"
2207
+ },
2208
+ {
2209
+ type: "function",
2210
+ name: "upvoteUser",
2211
+ inputs: [
2212
+ { name: "userToUpvote", type: "address", internalType: "address" },
2213
+ { name: "token", type: "address", internalType: "address" },
2214
+ { name: "numUpvotes", type: "uint256", internalType: "uint256" },
2215
+ { name: "feeTier", type: "uint24", internalType: "uint24" }
2216
+ ],
2217
+ outputs: [],
2218
+ stateMutability: "payable"
2219
+ },
2220
+ {
2221
+ type: "function",
2222
+ name: "userTokenCount",
2223
+ inputs: [{ name: "", type: "address", internalType: "address" }],
2224
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2225
+ stateMutability: "view"
2226
+ },
2227
+ {
2228
+ type: "function",
2229
+ name: "userTokenExists",
2230
+ inputs: [
2231
+ { name: "", type: "address", internalType: "address" },
2232
+ { name: "", type: "address", internalType: "address" }
2233
+ ],
2234
+ outputs: [{ name: "", type: "bool", internalType: "bool" }],
2235
+ stateMutability: "view"
2236
+ },
2237
+ {
2238
+ type: "function",
2239
+ name: "userTokenFeeTiers",
2240
+ inputs: [
2241
+ { name: "", type: "address", internalType: "address" },
2242
+ { name: "", type: "address", internalType: "address" }
2243
+ ],
2244
+ outputs: [{ name: "", type: "uint24", internalType: "uint24" }],
2245
+ stateMutability: "view"
2246
+ },
2247
+ {
2248
+ type: "function",
2249
+ name: "userTokenSeen",
2250
+ inputs: [
2251
+ { name: "", type: "address", internalType: "address" },
2252
+ { name: "", type: "address", internalType: "address" }
2253
+ ],
2254
+ outputs: [{ name: "", type: "bool", internalType: "bool" }],
2255
+ stateMutability: "view"
2256
+ },
2257
+ {
2258
+ type: "function",
2259
+ name: "userTokens",
2260
+ inputs: [
2261
+ { name: "", type: "address", internalType: "address" },
2262
+ { name: "", type: "uint256", internalType: "uint256" }
2263
+ ],
2264
+ outputs: [
2265
+ { name: "token", type: "address", internalType: "address" },
2266
+ { name: "feeTier", type: "uint24", internalType: "uint24" }
2267
+ ],
2268
+ stateMutability: "view"
2269
+ },
2270
+ {
2271
+ type: "function",
2272
+ name: "userUpvotesGiven",
2273
+ inputs: [{ name: "", type: "address", internalType: "address" }],
2274
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2275
+ stateMutability: "view"
2276
+ },
2277
+ {
2278
+ type: "function",
2279
+ name: "userUpvotesGivenPerToken",
2280
+ inputs: [
2281
+ { name: "", type: "address", internalType: "address" },
2282
+ { name: "", type: "address", internalType: "address" }
2283
+ ],
2284
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2285
+ stateMutability: "view"
2286
+ },
2287
+ {
2288
+ type: "function",
2289
+ name: "userUpvotesReceived",
2290
+ inputs: [{ name: "", type: "address", internalType: "address" }],
2291
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2292
+ stateMutability: "view"
2293
+ },
2294
+ {
2295
+ type: "function",
2296
+ name: "userUpvotesReceivedPerToken",
2297
+ inputs: [
2298
+ { name: "", type: "address", internalType: "address" },
2299
+ { name: "", type: "address", internalType: "address" }
2300
+ ],
2301
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
2302
+ stateMutability: "view"
2303
+ },
2304
+ {
2305
+ type: "function",
2306
+ name: "withdrawErc20",
2307
+ inputs: [
2308
+ { name: "to", type: "address", internalType: "address" },
2309
+ { name: "token", type: "address", internalType: "address" }
2310
+ ],
2311
+ outputs: [],
2312
+ stateMutability: "nonpayable"
2313
+ },
2314
+ {
2315
+ type: "event",
2316
+ name: "OwnershipTransferred",
2317
+ inputs: [
2318
+ { name: "previousOwner", type: "address", indexed: true, internalType: "address" },
2319
+ { name: "newOwner", type: "address", indexed: true, internalType: "address" }
2320
+ ],
2321
+ anonymous: false
2322
+ },
2323
+ {
2324
+ type: "event",
2325
+ name: "TokenAddedToList",
2326
+ inputs: [
2327
+ { name: "user", type: "address", indexed: true, internalType: "address" },
2328
+ { name: "token", type: "address", indexed: true, internalType: "address" },
2329
+ { name: "feeTier", type: "uint24", indexed: false, internalType: "uint24" }
2330
+ ],
2331
+ anonymous: false
2332
+ },
2333
+ {
2334
+ type: "event",
2335
+ name: "TokenRemovedFromList",
2336
+ inputs: [
2337
+ { name: "user", type: "address", indexed: true, internalType: "address" },
2338
+ { name: "token", type: "address", indexed: true, internalType: "address" }
2339
+ ],
2340
+ anonymous: false
2341
+ },
2342
+ {
2343
+ type: "event",
2344
+ name: "UserUpvoted",
2345
+ inputs: [
2346
+ { name: "upvoter", type: "address", indexed: true, internalType: "address" },
2347
+ { name: "upvotedUser", type: "address", indexed: true, internalType: "address" },
2348
+ { name: "token", type: "address", indexed: true, internalType: "address" },
2349
+ { name: "numUpvotes", type: "uint256", indexed: false, internalType: "uint256" }
2350
+ ],
2351
+ anonymous: false
2352
+ },
2353
+ { type: "error", name: "IncorrectEthSent", inputs: [] },
2354
+ { type: "error", name: "MaxTokensReached", inputs: [] },
2355
+ { type: "error", name: "NoAlphaToWithdraw", inputs: [] },
2356
+ {
2357
+ type: "error",
2358
+ name: "OwnableInvalidOwner",
2359
+ inputs: [{ name: "owner", type: "address", internalType: "address" }]
2360
+ },
2361
+ {
2362
+ type: "error",
2363
+ name: "OwnableUnauthorizedAccount",
2364
+ inputs: [{ name: "account", type: "address", internalType: "address" }]
2365
+ },
2366
+ {
2367
+ type: "error",
2368
+ name: "PoolNotFound",
2369
+ inputs: [
2370
+ { name: "tokenA", type: "address", internalType: "address" },
2371
+ { name: "tokenB", type: "address", internalType: "address" },
2372
+ { name: "feeTier", type: "uint24", internalType: "uint24" }
2373
+ ]
2374
+ },
2375
+ { type: "error", name: "ReentrancyGuardReentrantCall", inputs: [] },
2376
+ {
2377
+ type: "error",
2378
+ name: "Slot0ReadFailed",
2379
+ inputs: [{ name: "pool", type: "address", internalType: "address" }]
2380
+ },
2381
+ {
2382
+ type: "error",
2383
+ name: "StringsInsufficientHexLength",
2384
+ inputs: [
2385
+ { name: "value", type: "uint256", internalType: "uint256" },
2386
+ { name: "length", type: "uint256", internalType: "uint256" }
2387
+ ]
2388
+ },
2389
+ { type: "error", name: "TokenAlreadyInList", inputs: [] },
2390
+ { type: "error", name: "TokenNotInUserList", inputs: [] },
2391
+ { type: "error", name: "UserCannotUpvoteSelf", inputs: [] },
2392
+ { type: "error", name: "WithdrawFailed", inputs: [] },
2393
+ { type: "error", name: "ZeroUpvotes", inputs: [] }
2394
+ ];
2395
+
1935
2396
  // src/constants.ts
1936
2397
  var SCORE_CONTRACT = {
1937
2398
  address: "0x0000000fa09b022e5616e5a173b4b67fa2fbcf28",
@@ -1973,9 +2434,25 @@ var MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER = {
1973
2434
  address: "0x7A9EF0AC6F6a254cd570B05D62D094D3aa5067f1",
1974
2435
  abi: multi_version_uniswap_pool_info_retriever_default
1975
2436
  };
1976
- var WETH_ADDRESS = "0x4200000000000000000000000000000000000006";
2437
+ var WETH_BY_CHAIN = {
2438
+ 8453: "0x4200000000000000000000000000000000000006",
2439
+ // Base (L2 predeploy)
2440
+ 1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
2441
+ // Ethereum mainnet
2442
+ };
2443
+ function getWethAddress(chainId) {
2444
+ const addr = WETH_BY_CHAIN[chainId];
2445
+ if (!addr) {
2446
+ throw new Error(`Score: No WETH address for chain ${chainId}`);
2447
+ }
2448
+ return addr;
2449
+ }
1977
2450
  var NULL_ADDRESS = "0x0000000000000000000000000000000000000000";
1978
2451
  var UPVOTE_PRICE_ETH = 25e-6;
2452
+ var USER_UPVOTE_CONTRACT = {
2453
+ address: "0xa4bc2c63dd0157692fd5f409389e5032e37d8895",
2454
+ abi: user_upvote_default
2455
+ };
1979
2456
  var encodeUpvoteKey = (tokenAddress) => {
1980
2457
  return `0x${BigInt(`0x${tokenAddress.slice(2)}`).toString(16).padStart(64, "0")}`;
1981
2458
  };
@@ -2367,6 +2844,280 @@ var ScoreClient = class {
2367
2844
  return [];
2368
2845
  }
2369
2846
  };
2847
+ function parseUserUpvoteMessage(message, topic) {
2848
+ try {
2849
+ if (topic.startsWith("ub-")) {
2850
+ const [
2851
+ upvotedUserString,
2852
+ actualToken,
2853
+ numUpvotes,
2854
+ tokenWethPrice,
2855
+ wethUsdcPrice,
2856
+ alphaWethPrice,
2857
+ userTokenBalance
2858
+ ] = decodeAbiParameters(
2859
+ [
2860
+ { type: "string" },
2861
+ { type: "address" },
2862
+ { type: "uint256" },
2863
+ { type: "uint256" },
2864
+ { type: "uint256" },
2865
+ { type: "uint256" },
2866
+ { type: "uint256" }
2867
+ ],
2868
+ message.data
2869
+ );
2870
+ return {
2871
+ upvotedUserString,
2872
+ actualToken,
2873
+ numUpvotes: Number(numUpvotes),
2874
+ tokenWethPrice,
2875
+ wethUsdcPrice,
2876
+ alphaWethPrice,
2877
+ userTokenBalance
2878
+ };
2879
+ } else {
2880
+ const [
2881
+ upvotedUserString,
2882
+ actualToken,
2883
+ numUpvotes,
2884
+ tokenWethPrice,
2885
+ wethUsdcPrice,
2886
+ alphaWethPrice
2887
+ ] = decodeAbiParameters(
2888
+ [
2889
+ { type: "string" },
2890
+ { type: "address" },
2891
+ { type: "uint256" },
2892
+ { type: "uint256" },
2893
+ { type: "uint256" },
2894
+ { type: "uint256" }
2895
+ ],
2896
+ message.data
2897
+ );
2898
+ return {
2899
+ upvotedUserString,
2900
+ actualToken,
2901
+ numUpvotes: Number(numUpvotes),
2902
+ tokenWethPrice,
2903
+ wethUsdcPrice,
2904
+ alphaWethPrice
2905
+ };
2906
+ }
2907
+ } catch {
2908
+ return null;
2909
+ }
2910
+ }
2911
+ function extractTokenAddressesFromMessages(messages, topic) {
2912
+ const tokenSet = /* @__PURE__ */ new Set();
2913
+ const validMessages = [];
2914
+ for (const message of messages) {
2915
+ const parsed = parseUserUpvoteMessage(message, topic);
2916
+ if (parsed) {
2917
+ tokenSet.add(parsed.actualToken.toLowerCase());
2918
+ validMessages.push(parsed);
2919
+ }
2920
+ }
2921
+ return {
2922
+ tokenAddresses: Array.from(tokenSet),
2923
+ validMessages
2924
+ };
2925
+ }
2926
+ function validateUserUpvoteMessage(message, topic) {
2927
+ return parseUserUpvoteMessage(message, topic) !== null;
2928
+ }
2929
+ function calculatePriceInUsdc(parsedMessage, tokenDecimals = 18) {
2930
+ const { tokenWethPrice, wethUsdcPrice } = parsedMessage;
2931
+ if (tokenWethPrice > 0n && wethUsdcPrice > 0n) {
2932
+ const scale = 10 ** (36 - tokenDecimals);
2933
+ const tokenWethPriceNum = Number(tokenWethPrice) / scale;
2934
+ const wethUsdcPriceNum = Number(wethUsdcPrice) / 1e6;
2935
+ return tokenWethPriceNum * wethUsdcPriceNum;
2936
+ }
2937
+ return void 0;
2938
+ }
2939
+ function calculateUserTokenBalance(rawBalance, tokenDecimals = 18) {
2940
+ return Number(rawBalance) / Math.pow(10, tokenDecimals);
2941
+ }
2942
+ function validateUpvoteParams(params) {
2943
+ if (isAddressEqual(params.sender, params.userToUpvote)) {
2944
+ return { valid: false, error: "Cannot upvote yourself" };
2945
+ }
2946
+ if (!Number.isInteger(params.numUpvotes)) {
2947
+ return { valid: false, error: "Number of upvotes must be a whole number" };
2948
+ }
2949
+ if (params.numUpvotes <= 0) {
2950
+ return { valid: false, error: "Number of upvotes must be greater than zero" };
2951
+ }
2952
+ return { valid: true };
2953
+ }
2954
+ function calculateUpvoteCost(numUpvotes, upvotePrice) {
2955
+ return BigInt(numUpvotes) * upvotePrice;
2956
+ }
2957
+ function computeEnrichedFields(parsed, tokenInfo) {
2958
+ const decimals = tokenInfo?.decimals ?? 18;
2959
+ const priceInUsdc = calculatePriceInUsdc(parsed, decimals);
2960
+ const userTokenBalance = parsed.userTokenBalance ? calculateUserTokenBalance(parsed.userTokenBalance, decimals) : 0;
2961
+ const userTokenBalanceUsdValue = priceInUsdc !== void 0 && userTokenBalance > 0 ? priceInUsdc * userTokenBalance : void 0;
2962
+ return { priceInUsdc, userTokenBalance, userTokenBalanceUsdValue };
2963
+ }
2964
+ function buildUserUpvote(parsed, timestamp, tokenInfo) {
2965
+ return {
2966
+ tokenAddress: parsed.actualToken,
2967
+ numUpvotes: parsed.numUpvotes,
2968
+ timestamp,
2969
+ ...computeEnrichedFields(parsed, tokenInfo),
2970
+ upvotedUserAddress: parsed.upvotedUserString,
2971
+ tokenInfo
2972
+ };
2973
+ }
2974
+ function buildUserUpvoteReceived(parsed, upvoterAddress, timestamp, tokenInfo) {
2975
+ return {
2976
+ upvoterAddress,
2977
+ tokenAddress: parsed.actualToken,
2978
+ numUpvotes: parsed.numUpvotes,
2979
+ timestamp,
2980
+ ...computeEnrichedFields(parsed, tokenInfo),
2981
+ tokenInfo
2982
+ };
2983
+ }
2984
+
2985
+ // src/user-upvote/UserUpvoteClient.ts
2986
+ var UserUpvoteClient = class {
2987
+ constructor(params) {
2988
+ this.chainId = params.chainId;
2989
+ this.client = getPublicClient({
2990
+ chainId: params.chainId,
2991
+ rpcUrl: params.overrides?.rpcUrls
2992
+ });
2993
+ this.contractAddress = params.overrides?.contractAddress ?? USER_UPVOTE_CONTRACT.address;
2994
+ }
2995
+ async getUserUpvotesGiven({ user }) {
2996
+ const data = await readContract(this.client, {
2997
+ address: this.contractAddress,
2998
+ abi: USER_UPVOTE_CONTRACT.abi,
2999
+ functionName: "getUserUpvotesGiven",
3000
+ args: [user]
3001
+ });
3002
+ return data;
3003
+ }
3004
+ async getUserUpvotesReceived({ user }) {
3005
+ const data = await readContract(this.client, {
3006
+ address: this.contractAddress,
3007
+ abi: USER_UPVOTE_CONTRACT.abi,
3008
+ functionName: "getUserUpvotesReceived",
3009
+ args: [user]
3010
+ });
3011
+ return data;
3012
+ }
3013
+ async getUserUpvotesGivenPerTokenBatch({
3014
+ user,
3015
+ tokens
3016
+ }) {
3017
+ const data = await readContract(this.client, {
3018
+ address: this.contractAddress,
3019
+ abi: USER_UPVOTE_CONTRACT.abi,
3020
+ functionName: "getUserUpvotesGivenPerTokenBatch",
3021
+ args: [user, tokens]
3022
+ });
3023
+ return data;
3024
+ }
3025
+ async getUserUpvotesReceivedPerTokenBatch({
3026
+ user,
3027
+ tokens
3028
+ }) {
3029
+ const data = await readContract(this.client, {
3030
+ address: this.contractAddress,
3031
+ abi: USER_UPVOTE_CONTRACT.abi,
3032
+ functionName: "getUserUpvotesReceivedPerTokenBatch",
3033
+ args: [user, tokens]
3034
+ });
3035
+ return data;
3036
+ }
3037
+ async getTotalUpvotesPerToken({
3038
+ token
3039
+ }) {
3040
+ const data = await readContract(this.client, {
3041
+ address: this.contractAddress,
3042
+ abi: USER_UPVOTE_CONTRACT.abi,
3043
+ functionName: "getTotalUpvotesPerToken",
3044
+ args: [token]
3045
+ });
3046
+ return data;
3047
+ }
3048
+ async getUserTokensInRange({
3049
+ user,
3050
+ startIndex,
3051
+ endIndex
3052
+ }) {
3053
+ const data = await readContract(this.client, {
3054
+ address: this.contractAddress,
3055
+ abi: USER_UPVOTE_CONTRACT.abi,
3056
+ functionName: "getUserTokensInRange",
3057
+ args: [user, BigInt(startIndex), BigInt(endIndex)]
3058
+ });
3059
+ return data.map((item) => ({
3060
+ token: item.token,
3061
+ feeTier: Number(item.feeTier)
3062
+ }));
3063
+ }
3064
+ async getUserTokenCount({ user }) {
3065
+ const data = await readContract(this.client, {
3066
+ address: this.contractAddress,
3067
+ abi: USER_UPVOTE_CONTRACT.abi,
3068
+ functionName: "getUserTokenCount",
3069
+ args: [user]
3070
+ });
3071
+ return Number(data);
3072
+ }
3073
+ async isTokenInUserList({
3074
+ user,
3075
+ token
3076
+ }) {
3077
+ const data = await readContract(this.client, {
3078
+ address: this.contractAddress,
3079
+ abi: USER_UPVOTE_CONTRACT.abi,
3080
+ functionName: "isTokenInUserList",
3081
+ args: [user, token]
3082
+ });
3083
+ return data;
3084
+ }
3085
+ async getUpvotePrice() {
3086
+ const data = await readContract(this.client, {
3087
+ address: this.contractAddress,
3088
+ abi: USER_UPVOTE_CONTRACT.abi,
3089
+ functionName: "upvotePrice"
3090
+ });
3091
+ return data;
3092
+ }
3093
+ async upvoteUser({
3094
+ walletClient,
3095
+ userToUpvote,
3096
+ token = NULL_ADDRESS,
3097
+ numUpvotes,
3098
+ feeTier = 0,
3099
+ value: providedValue
3100
+ }) {
3101
+ const sender = walletClient.account?.address;
3102
+ if (sender) {
3103
+ const validation = validateUpvoteParams({ sender, userToUpvote, numUpvotes });
3104
+ if (!validation.valid) {
3105
+ throw new Error(validation.error);
3106
+ }
3107
+ }
3108
+ const value = providedValue ?? calculateUpvoteCost(numUpvotes, await this.getUpvotePrice());
3109
+ const hash = await walletClient.writeContract({
3110
+ address: this.contractAddress,
3111
+ abi: USER_UPVOTE_CONTRACT.abi,
3112
+ functionName: "upvoteUser",
3113
+ args: [userToUpvote, token, BigInt(numUpvotes), BigInt(feeTier)],
3114
+ value,
3115
+ chain: null,
3116
+ dataSuffix: getBaseDataSuffix(this.chainId)
3117
+ });
3118
+ return hash;
3119
+ }
3120
+ };
2370
3121
  var encodePoolKey = (poolKey) => {
2371
3122
  if (!poolKey || !isValidPoolKey2(poolKey)) {
2372
3123
  return "0x";
@@ -2415,10 +3166,10 @@ var V4_HOOKS = [
2415
3166
  ];
2416
3167
  var LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS = 0.1 * 1e18;
2417
3168
  var LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS = 0.01 * 1e18;
2418
- function normalizeTokenPairs(pairs) {
3169
+ function normalizeTokenPairs(pairs, wethAddress) {
2419
3170
  return pairs.map((pair) => {
2420
3171
  const tokenA = pair.tokenAddress;
2421
- const tokenB = pair.baseTokenAddress || WETH_ADDRESS;
3172
+ const tokenB = pair.baseTokenAddress || wethAddress;
2422
3173
  return {
2423
3174
  originalPair: pair,
2424
3175
  tokenA: tokenA.toLowerCase() < tokenB.toLowerCase() ? tokenA : tokenB,
@@ -2435,7 +3186,7 @@ function buildDiscoveryArgs(normalizedPairs) {
2435
3186
  v4Hooks: V4_HOOKS
2436
3187
  };
2437
3188
  }
2438
- function parsePoolDiscoveries(poolResults, pairs) {
3189
+ function parsePoolDiscoveries(poolResults, pairs, wethAddress) {
2439
3190
  if (!poolResults) {
2440
3191
  return {
2441
3192
  v2PoolAddresses: [],
@@ -2472,7 +3223,7 @@ function parsePoolDiscoveries(poolResults, pairs) {
2472
3223
  v2PoolAddresses.push(poolAddressValue);
2473
3224
  v2PoolAddressToPair[poolAddress] = {
2474
3225
  tokenAddress: pair.tokenAddress,
2475
- baseTokenAddress: pair.baseTokenAddress || WETH_ADDRESS,
3226
+ baseTokenAddress: pair.baseTokenAddress || wethAddress,
2476
3227
  fee: feeNum
2477
3228
  };
2478
3229
  }
@@ -2483,7 +3234,7 @@ function parsePoolDiscoveries(poolResults, pairs) {
2483
3234
  v3PoolAddresses.push(poolAddressValue);
2484
3235
  v3PoolAddressToPair[poolAddress] = {
2485
3236
  tokenAddress: pair.tokenAddress,
2486
- baseTokenAddress: pair.baseTokenAddress || WETH_ADDRESS,
3237
+ baseTokenAddress: pair.baseTokenAddress || wethAddress,
2487
3238
  fee: feeNum
2488
3239
  };
2489
3240
  }
@@ -2494,7 +3245,7 @@ function parsePoolDiscoveries(poolResults, pairs) {
2494
3245
  v4PoolKeys.push(poolKey);
2495
3246
  v4PoolKeyToPair[poolKeyString] = {
2496
3247
  tokenAddress: pair.tokenAddress,
2497
- baseTokenAddress: pair.baseTokenAddress || WETH_ADDRESS,
3248
+ baseTokenAddress: pair.baseTokenAddress || wethAddress,
2498
3249
  fee: feeNum
2499
3250
  };
2500
3251
  }
@@ -2621,19 +3372,19 @@ function constructPoolKey(info, pair, version, v4PoolKey) {
2621
3372
  return void 0;
2622
3373
  }
2623
3374
  }
2624
- function getWethBalanceWei(pool) {
2625
- if (pool.token0?.toLowerCase() === WETH_ADDRESS.toLowerCase()) {
3375
+ function getWethBalanceWei(pool, wethAddress) {
3376
+ if (pool.token0?.toLowerCase() === wethAddress.toLowerCase()) {
2626
3377
  return Number(pool.token0Balance);
2627
- } else if (pool.token1?.toLowerCase() === WETH_ADDRESS.toLowerCase()) {
3378
+ } else if (pool.token1?.toLowerCase() === wethAddress.toLowerCase()) {
2628
3379
  return Number(pool.token1Balance);
2629
3380
  } else {
2630
3381
  return Number(pool.baseTokenBalance);
2631
3382
  }
2632
3383
  }
2633
- function filterV2V3PoolsByLiquidity(pools, threshold) {
3384
+ function filterV2V3PoolsByLiquidity(pools, threshold, wethAddress) {
2634
3385
  return pools.filter((pool) => {
2635
3386
  if (!pool.poolAddress) return false;
2636
- const wethBalanceWei = getWethBalanceWei(pool);
3387
+ const wethBalanceWei = getWethBalanceWei(pool, wethAddress);
2637
3388
  return wethBalanceWei >= threshold;
2638
3389
  });
2639
3390
  }
@@ -2656,7 +3407,7 @@ function selectBestV2V3PoolByFee(pools) {
2656
3407
  return a.fee < b.fee ? a : b;
2657
3408
  });
2658
3409
  }
2659
- function selectBestPoolPerPair(allPools) {
3410
+ function selectBestPoolPerPair(allPools, wethAddress) {
2660
3411
  const poolsByPair = {};
2661
3412
  for (const pool of allPools) {
2662
3413
  const key = pool.tokenAddress.toLowerCase() + "_" + pool.baseTokenAddress.toLowerCase();
@@ -2671,7 +3422,8 @@ function selectBestPoolPerPair(allPools) {
2671
3422
  } else {
2672
3423
  const v2v3PoolsWithPreferredLiquidity = filterV2V3PoolsByLiquidity(
2673
3424
  group,
2674
- LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS
3425
+ LIQUIDITY_THRESHOLD_TO_PREFER_V2_V3_POOLS,
3426
+ wethAddress
2675
3427
  );
2676
3428
  if (v2v3PoolsWithPreferredLiquidity.length > 0) {
2677
3429
  best = selectBestV2V3Pool(v2v3PoolsWithPreferredLiquidity);
@@ -2682,7 +3434,8 @@ function selectBestPoolPerPair(allPools) {
2682
3434
  } else {
2683
3435
  const v2v3PoolsWithAcceptableLiquidity = filterV2V3PoolsByLiquidity(
2684
3436
  group,
2685
- LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS
3437
+ LIQUIDITY_THRESHOLD_TO_CONSIDER_V2_V3_POOLS,
3438
+ wethAddress
2686
3439
  );
2687
3440
  if (v2v3PoolsWithAcceptableLiquidity.length > 0) {
2688
3441
  best = selectBestV2V3PoolByFee(v2v3PoolsWithAcceptableLiquidity);
@@ -2698,10 +3451,12 @@ function selectBestPoolPerPair(allPools) {
2698
3451
  }
2699
3452
  async function discoverPools({
2700
3453
  publicClient,
2701
- pairs
3454
+ pairs,
3455
+ chainId = 8453
2702
3456
  }) {
2703
3457
  if (pairs.length === 0) return [];
2704
- const normalizedPairs = normalizeTokenPairs(pairs);
3458
+ const wethAddress = getWethAddress(chainId);
3459
+ const normalizedPairs = normalizeTokenPairs(pairs, wethAddress);
2705
3460
  const discoveryArgs = buildDiscoveryArgs(normalizedPairs);
2706
3461
  const poolResults = await readContract(publicClient, {
2707
3462
  address: MULTI_VERSION_UNISWAP_BULK_POOL_FINDER.address,
@@ -2709,7 +3464,7 @@ async function discoverPools({
2709
3464
  functionName: "getPoolsMultiVersion",
2710
3465
  args: [discoveryArgs]
2711
3466
  });
2712
- const discoveries = parsePoolDiscoveries(poolResults, pairs);
3467
+ const discoveries = parsePoolDiscoveries(poolResults, pairs, wethAddress);
2713
3468
  const totalPools = discoveries.v2PoolAddresses.length + discoveries.v3PoolAddresses.length + discoveries.v4PoolKeys.length;
2714
3469
  if (totalPools === 0) return [];
2715
3470
  const poolInfos = await readContract(publicClient, {
@@ -2720,7 +3475,7 @@ async function discoverPools({
2720
3475
  discoveries.v2PoolAddresses,
2721
3476
  discoveries.v3PoolAddresses,
2722
3477
  discoveries.v4PoolKeys,
2723
- WETH_ADDRESS
3478
+ wethAddress
2724
3479
  ]
2725
3480
  });
2726
3481
  if (!Array.isArray(poolInfos) || poolInfos.length === 0) return [];
@@ -2729,7 +3484,7 @@ async function discoverPools({
2729
3484
  if (!poolData || !poolData.pair) return null;
2730
3485
  return {
2731
3486
  tokenAddress: poolData.pair.tokenAddress,
2732
- baseTokenAddress: poolData.pair.baseTokenAddress || WETH_ADDRESS,
3487
+ baseTokenAddress: poolData.pair.baseTokenAddress || wethAddress,
2733
3488
  poolAddress: poolData.version === 4 ? null : info.poolAddress,
2734
3489
  price: calculatePoolPrice(info, poolData.pair, poolData.version),
2735
3490
  baseTokenBalance: String(info.baseTokenBalance || 0),
@@ -2746,7 +3501,7 @@ async function discoverPools({
2746
3501
  )
2747
3502
  };
2748
3503
  }).filter((p) => p !== null);
2749
- const bestPools = selectBestPoolPerPair(allPools);
3504
+ const bestPools = selectBestPoolPerPair(allPools, wethAddress);
2750
3505
  return bestPools.map(
2751
3506
  (pool) => ({
2752
3507
  tokenAddress: pool.tokenAddress,
@@ -2765,15 +3520,17 @@ async function discoverPools({
2765
3520
  }
2766
3521
  async function discoverTokenPool({
2767
3522
  publicClient,
2768
- tokenAddress
3523
+ tokenAddress,
3524
+ chainId = 8453
2769
3525
  }) {
2770
3526
  const results = await discoverPools({
2771
3527
  publicClient,
2772
- pairs: [{ tokenAddress }]
3528
+ pairs: [{ tokenAddress }],
3529
+ chainId
2773
3530
  });
2774
3531
  return results[0] ?? null;
2775
3532
  }
2776
3533
 
2777
- export { ALL_STRATEGY_ADDRESSES, DYNAMIC_SPLIT_STRATEGY, LEGACY_UPVOTE_V1_ADDRESS, LEGACY_UPVOTE_V2_ADDRESS, MULTI_VERSION_UNISWAP_BULK_POOL_FINDER, MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER, NULL_ADDRESS, PURE_ALPHA_STRATEGY, SCORE_CONTRACT, SUPPORTED_SCORE_CHAINS, ScoreClient, UNIV234_POOLS_STRATEGY, UPVOTE_APP, UPVOTE_PRICE_ETH, UPVOTE_STORAGE_APP, WETH_ADDRESS, calculatePriceFromSqrtPriceX96, decodeStrategyMetadata, decodeUpvoteMessage, decodeUpvoteStorageBlob, discoverPools, discoverTokenPool, encodePoolKey, encodeUpvoteKey, extractStrategyAddress, extractTokenAddressFromScoreKey, getFeedContentKey, getScoreKey, getStorageScoreKey, getStorageUpvoteContext, getTokenScoreKey, isDynamicSplitStrategy, isPureAlphaStrategy, isStrategyMessage, isTokenScoreKey, isUniv234PoolsStrategy, isUserUpvoteMessage, selectStrategy, tokenAddressToUpvoteKeyString };
3534
+ export { ALL_STRATEGY_ADDRESSES, DYNAMIC_SPLIT_STRATEGY, LEGACY_UPVOTE_V1_ADDRESS, LEGACY_UPVOTE_V2_ADDRESS, MULTI_VERSION_UNISWAP_BULK_POOL_FINDER, MULTI_VERSION_UNISWAP_POOL_INFO_RETRIEVER, NULL_ADDRESS, PURE_ALPHA_STRATEGY, SCORE_CONTRACT, SUPPORTED_SCORE_CHAINS, ScoreClient, UNIV234_POOLS_STRATEGY, UPVOTE_APP, UPVOTE_PRICE_ETH, UPVOTE_STORAGE_APP, USER_UPVOTE_CONTRACT, UserUpvoteClient, buildUserUpvote, buildUserUpvoteReceived, calculatePriceFromSqrtPriceX96, calculatePriceInUsdc, calculateUpvoteCost, calculateUserTokenBalance, decodeStrategyMetadata, decodeUpvoteMessage, decodeUpvoteStorageBlob, discoverPools, discoverTokenPool, encodePoolKey, encodeUpvoteKey, extractStrategyAddress, extractTokenAddressFromScoreKey, extractTokenAddressesFromMessages, getFeedContentKey, getScoreKey, getStorageScoreKey, getStorageUpvoteContext, getTokenScoreKey, getWethAddress, isDynamicSplitStrategy, isPureAlphaStrategy, isStrategyMessage, isTokenScoreKey, isUniv234PoolsStrategy, isUserUpvoteMessage, parseUserUpvoteMessage, selectStrategy, tokenAddressToUpvoteKeyString, validateUpvoteParams, validateUserUpvoteMessage };
2778
3535
  //# sourceMappingURL=index.mjs.map
2779
3536
  //# sourceMappingURL=index.mjs.map