-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add polygon mainnet support and fix wallet signature issue (#13)
* feat: integrate sdk + nft contract for onboarding * feat: fix signing, add local storage wrapper, and fix up smaller ui issues * feat: add mainnet polygon as supported chain
- Loading branch information
Showing
16 changed files
with
277 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,64 @@ | ||
import { useAccount } from "wagmi"; | ||
import { useNFTsQuery } from "../surfaces/profile/NftSection"; | ||
import { useAccount, useChainId } from "wagmi"; | ||
import { useEffect, useState } from "react"; | ||
import { localSmartContractStore } from "./localStorage"; | ||
|
||
export type AppState = | ||
| { | ||
state: "LOADING"; | ||
eoaAddress: undefined; | ||
scwAddress: undefined; | ||
scwAddresses: undefined; | ||
} | ||
| { | ||
state: "UNCONNECTED"; | ||
eoaAddress: undefined; | ||
scwAddress: undefined; | ||
scwAddresses: undefined; | ||
} | ||
| { | ||
state: "NO_SCW"; | ||
eoaAddress: string; | ||
scwAddress: undefined; | ||
scwAddresses: undefined; | ||
} | ||
| { | ||
state: "HAS_SCW"; | ||
eoaAddress: string; | ||
scwAddress: string; | ||
scwAddresses: string[]; | ||
}; | ||
|
||
export function useAppState(): AppState { | ||
const { address, isConnected } = useAccount(); | ||
const scwAddress = address ? localStorage.getItem(address) : undefined; | ||
const chainId = useChainId(); | ||
|
||
const [state, setState] = useState<AppState>({ | ||
state: "UNCONNECTED", | ||
eoaAddress: undefined, | ||
scwAddress: undefined, | ||
scwAddresses: undefined, | ||
}); | ||
useEffect(() => { | ||
if (!isConnected || !address) { | ||
setState({ | ||
state: "UNCONNECTED", | ||
eoaAddress: undefined, | ||
scwAddress: undefined, | ||
scwAddresses: undefined, | ||
}); | ||
return; | ||
} | ||
|
||
if (!scwAddress) { | ||
const scwAddresses = localSmartContractStore.smartAccountAddresses( | ||
address, | ||
chainId | ||
); | ||
if (scwAddresses.length === 0) { | ||
setState({ | ||
state: "NO_SCW", | ||
eoaAddress: address as `0x${string}`, | ||
scwAddress: undefined, | ||
scwAddresses: undefined, | ||
}); | ||
} else { | ||
setState({ | ||
state: "HAS_SCW", | ||
eoaAddress: address as `0x${string}`, | ||
scwAddress: scwAddress!, | ||
scwAddresses: scwAddresses, | ||
}); | ||
} | ||
}, [address, isConnected, scwAddress]); | ||
}, [address, isConnected, chainId]); | ||
return state; | ||
} |
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,38 @@ | ||
const key = (address: string, chainId: number) => | ||
`smartContractAccount-${address}-${chainId}`; | ||
|
||
const smartAccountAddresses = (address: string, chainId: number) => { | ||
const scAddresses = JSON.parse( | ||
localStorage.getItem(key(address, chainId)) ?? "[]" | ||
); | ||
return scAddresses; | ||
}; | ||
|
||
const addSmartContractAccount = ( | ||
address: string, | ||
scAddress: string, | ||
chainId: number | ||
) => { | ||
const k = key(address, chainId); | ||
const addresses = JSON.parse(localStorage.getItem(k) ?? "[]"); | ||
localStorage.setItem(k, JSON.stringify([...addresses, scAddress])); | ||
}; | ||
|
||
const removeSmartContractAccount = ( | ||
address: string, | ||
scAddress: string, | ||
chainId: number | ||
) => { | ||
const k = key(address, chainId); | ||
const scAddresses = JSON.parse(localStorage.getItem(k) ?? "[]"); | ||
if (!scAddresses.includes(scAddress)) { | ||
scAddresses.splice(scAddresses.indexOf(scAddress), 1); | ||
localStorage.setItem(k, JSON.stringify([...scAddresses, scAddress])); | ||
} | ||
}; | ||
|
||
export const localSmartContractStore = { | ||
smartAccountAddresses, | ||
addSmartContractAccount, | ||
removeSmartContractAccount, | ||
}; |
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,41 @@ | ||
import { SimpleSmartAccountOwner } from "@alchemy/aa-core"; | ||
import { useCallback } from "react"; | ||
import { toHex } from "viem"; | ||
import { useWalletClient } from "wagmi"; | ||
|
||
type SimpleSmartAccountOwnerResult = | ||
| { | ||
isLoading: false; | ||
owner: SimpleSmartAccountOwner; | ||
} | ||
| { | ||
isLoading: true; | ||
owner: undefined; | ||
}; | ||
|
||
export function useSimpleAccountOwner(): SimpleSmartAccountOwnerResult { | ||
const walletClientQuery = useWalletClient(); | ||
// We need this to by pass a viem bug https://github.com/wagmi-dev/viem/issues/606 | ||
const signMessage = useCallback( | ||
(data: string | Uint8Array) => | ||
walletClientQuery.data!.request({ | ||
// ignore the type error here, it's a bug in the viem typing | ||
// @ts-ignore | ||
method: "personal_sign", | ||
// @ts-ignore | ||
params: [toHex(data), walletClientQuery.data.account.address], | ||
}), | ||
[walletClientQuery.data] | ||
); | ||
const getAddress = useCallback( | ||
() => Promise.resolve(walletClientQuery.data!.account.address), | ||
[walletClientQuery.data] | ||
); | ||
if (walletClientQuery.isLoading) { | ||
return { | ||
isLoading: true, | ||
owner: undefined, | ||
}; | ||
} | ||
return { isLoading: false, owner: { signMessage, getAddress } }; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { OwnedNFTsResponse } from "../declarations/api"; | ||
import { callEndpoint } from "./http"; | ||
|
||
export function getNFTs(address: string): Promise<OwnedNFTsResponse> { | ||
export function getNFTs( | ||
address: string, | ||
chainId: number | ||
): Promise<OwnedNFTsResponse> { | ||
return callEndpoint("GET", "/api/nfts", { | ||
address, | ||
chainId, | ||
}); | ||
} |
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
Oops, something went wrong.