-
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 smart contract for nft for onboarding (#10)
* feat: clean up components for profile and nft fetching * feat: add smart contract for nft
- Loading branch information
Showing
16 changed files
with
388 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useAccount } from "wagmi"; | ||
import { useNFTsQuery } from "./nfts"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export type AppState = | ||
| { | ||
state: "LOADING"; | ||
eoaAddress: undefined; | ||
scwAddress: undefined; | ||
} | ||
| { | ||
state: "UNCONNECTED"; | ||
eoaAddress: undefined; | ||
scwAddress: undefined; | ||
} | ||
| { | ||
state: "NO_SCW"; | ||
eoaAddress: string; | ||
scwAddress: undefined; | ||
} | ||
| { | ||
state: "HAS_SCW"; | ||
eoaAddress: string; | ||
scwAddress: string; | ||
}; | ||
|
||
export function useAppState(): AppState { | ||
const { address, isConnected } = useAccount(); | ||
const nfts = useNFTsQuery(address); | ||
const [state, setState] = useState<AppState>({ | ||
state: "UNCONNECTED", | ||
eoaAddress: undefined, | ||
scwAddress: undefined, | ||
}); | ||
useEffect(() => { | ||
if (!isConnected || !address) { | ||
setState({ | ||
state: "UNCONNECTED", | ||
eoaAddress: undefined, | ||
scwAddress: undefined, | ||
}); | ||
return; | ||
} | ||
|
||
if (nfts.isLoading) { | ||
setState({ | ||
state: "LOADING", | ||
eoaAddress: undefined, | ||
scwAddress: undefined, | ||
}); | ||
return; | ||
} | ||
const scwNFT = nfts?.data?.ownedNfts.find((value) => { | ||
return value.contract.address === "0x0000000000000000"; | ||
}); | ||
const scwAttribute = scwNFT?.metadata.attributes?.find((attribute) => { | ||
attribute.trait_type === "SCW"; | ||
}); | ||
if (!scwNFT || !scwAttribute) { | ||
setState({ | ||
state: "NO_SCW", | ||
eoaAddress: address as `0x${string}`, | ||
scwAddress: undefined, | ||
}); | ||
} else { | ||
setState({ | ||
state: "HAS_SCW", | ||
eoaAddress: address as `0x${string}`, | ||
scwAddress: scwAttribute.value!, | ||
}); | ||
} | ||
}, [address, isConnected, nfts]); | ||
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import {useQuery} from "@tanstack/react-query"; | ||
import {getNFTs} from "../http/endpoints"; | ||
import {OwnedNFTsResponse} from "../declarations/api"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { getNFTs } from "../http/endpoints"; | ||
|
||
export function useNFTsQuery(ethAddress?: string) { | ||
return useQuery(["nfts", ethAddress], () => { | ||
if (ethAddress) { | ||
return getNFTs(ethAddress); | ||
} else { | ||
return Promise.resolve({data: []} as unknown as OwnedNFTsResponse); | ||
} | ||
return ethAddress | ||
? getNFTs(ethAddress) | ||
: Promise.resolve({ | ||
ownedNfts: [], | ||
totalCount: 0, | ||
blockHash: "0x000000000000", | ||
}); | ||
}); | ||
} |
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
18 changes: 18 additions & 0 deletions
18
examples/alchemy-daapp/src/components/connect/ConnectPage.tsx
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,18 @@ | ||
import { Center, Heading, Text, VStack } from "@chakra-ui/react"; | ||
import { ConnectButton } from "@rainbow-me/rainbowkit"; | ||
|
||
export function ConnectPage() { | ||
return ( | ||
<Center> | ||
<VStack gap={4}> | ||
<Heading size="lg">Welcome to the Alchemy exmple D🅰️🅰️pp!</Heading> | ||
<Text align="center"> | ||
We're excited for you to start using account abstraction!! <br /> | ||
Click below to connect your wallet, and create your own account | ||
abstrated smart contract wallet. | ||
</Text> | ||
<ConnectButton /> | ||
</VStack> | ||
</Center> | ||
); | ||
} |
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 was deleted.
Oops, something went wrong.
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,7 @@ | ||
[default] | ||
src = 'src' | ||
out = 'out' | ||
libs = ['lib'] | ||
remappings = ['ds-test/=lib/ds-test/src/'] | ||
|
||
# See more config options https://github.com/gakonst/foundry/tree/master/config |
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,5 @@ | ||
## Version 1 | ||
|
||
Deployer: 0x15a411507e901f26965f0ebcd3155834b058a6b2 | ||
Deployed to: 0xb7b9424ef3d1b9086b7e53276c4aad68a1dd971c | ||
Transaction hash: 0x46f95ea67e2b103f607884a87fd25d53d619eaa7d67437f1d7cb51f7c99b6de2 |
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,2 @@ | ||
openzeppelin-contracts/=lib/openzeppelin-contracts/ | ||
forge-std/=lib/forge-std/src/ |
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,56 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.10; | ||
|
||
import "solmate/tokens/ERC721.sol"; | ||
import "openzeppelin-contracts/contracts/utils/Strings.sol"; | ||
import "openzeppelin-contracts/contracts/access/Ownable.sol"; | ||
|
||
error MintPriceNotPaid(); | ||
error MaxSupply(); | ||
error NonExistentTokenURI(); | ||
error WithdrawTransfer(); | ||
|
||
contract NFT is ERC721, Ownable { | ||
|
||
using Strings for uint256; | ||
string public baseURI; | ||
uint256 public currentTokenId; | ||
|
||
constructor( | ||
string memory _name, | ||
string memory _symbol, | ||
string memory _baseURI | ||
) ERC721(_name, _symbol) { | ||
baseURI = _baseURI; | ||
} | ||
|
||
function mintTo(address recipient) public payable returns (uint256) { | ||
uint256 newTokenId = ++currentTokenId; | ||
_safeMint(recipient, newTokenId); | ||
return newTokenId; | ||
} | ||
|
||
function tokenURI(uint256 tokenId) | ||
public | ||
view | ||
virtual | ||
override | ||
returns (string memory) | ||
{ | ||
if (ownerOf(tokenId) == address(0)) { | ||
revert NonExistentTokenURI(); | ||
} | ||
return | ||
bytes(baseURI).length > 0 | ||
? baseURI | ||
: ""; | ||
} | ||
|
||
function withdrawPayments(address payable payee) external onlyOwner { | ||
uint256 balance = address(this).balance; | ||
(bool transferTx, ) = payee.call{value: balance}(""); | ||
if (!transferTx) { | ||
revert WithdrawTransfer(); | ||
} | ||
} | ||
} |
Oops, something went wrong.