-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/on-chain-graph-sdk' into test/on-chain-graph-sdk
- Loading branch information
Showing
8 changed files
with
126 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Client } from "@xmtp/xmtp-js"; | ||
import { WalletType } from "../../types/xmtp-messaging"; | ||
|
||
const CACHE_EXPIRATION = 1000 * 60 * 10; // 10 minutes | ||
|
||
type XMTPClientCache = Map< | ||
string, | ||
{ | ||
client: Client; | ||
createdAt: number; | ||
} | ||
>; | ||
|
||
const clientCache: XMTPClientCache = new Map(); | ||
|
||
async function getClientCacheKey(wallet: WalletType) { | ||
if ("address" in wallet) { | ||
return wallet.address as string; | ||
} | ||
if ("getAddress" in wallet) { | ||
return wallet.getAddress(); | ||
} | ||
} | ||
|
||
function isClientCacheValid(createdAt: number) { | ||
return Date.now() - createdAt < CACHE_EXPIRATION; | ||
} | ||
|
||
export async function getClientFromCache(wallet: WalletType) { | ||
const key = await getClientCacheKey(wallet); | ||
if (!key) return null; | ||
const cachedData = clientCache.get(key); | ||
if (!cachedData || !isClientCacheValid(cachedData.createdAt)) return null; | ||
return cachedData.client; | ||
} | ||
|
||
export async function putClientIntoCache(wallet: WalletType, client: Client) { | ||
const key = await getClientCacheKey(wallet); | ||
if (!key) return null; | ||
clientCache.set(key, { | ||
client, | ||
createdAt: Date.now(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Client } from "@xmtp/xmtp-js"; | ||
import { BrowserProvider, Eip1193Provider } from 'ethers'; | ||
import { WalletType } from "../../types/xmtp-messaging"; | ||
import { getClientFromCache, putClientIntoCache } from "./clientCache"; | ||
|
||
// get browser based (MetaMask, Coinbase etc) wallet | ||
async function getBrowserBasedWallet():Promise<WalletType> { | ||
if (!("ethereum" in window)) { | ||
throw new Error("Browser based wallet not found"); | ||
} | ||
const provider = new BrowserProvider(window.ethereum as Eip1193Provider); | ||
return provider.getSigner(); | ||
} | ||
|
||
// get xmtp client from cache or create one | ||
export async function getXMTPClient( | ||
wallet?: WalletType, | ||
cacheXMTPClient?: boolean | ||
): Promise<Client> { | ||
let xmtpClient; | ||
let userWallet = wallet; | ||
// try to get browser based (MetaMask, Coinbase etc) wallet if wallet is not provided | ||
if (!userWallet) { | ||
userWallet = await getBrowserBasedWallet(); | ||
} | ||
// try to get xmtp client from cache if caching is enabled | ||
if (cacheXMTPClient) { | ||
xmtpClient = await getClientFromCache(userWallet); | ||
} | ||
// if cached xmtp client doesn't exist then create one | ||
if (!xmtpClient) { | ||
xmtpClient = await Client.create(userWallet, { env: "production" }); | ||
} | ||
// put xmtp client into cache if caching is enabled | ||
if (cacheXMTPClient) { | ||
putClientIntoCache(userWallet, xmtpClient); | ||
} | ||
return xmtpClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "./processAddressesViaAirstack"; | ||
export * from "./clientCache"; | ||
export * from "./getProcessedAddresses"; | ||
export * from "./getXMTPClient"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters