-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add useNonceForAddress, fix useAccount, starknetkit & useAccoun…
…d demo (#479)
- Loading branch information
Showing
42 changed files
with
5,617 additions
and
5,924 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useAccount } from "@starknet-react/core"; | ||
import { DemoContainer } from "../starknet"; | ||
|
||
export function Account() { | ||
return ( | ||
<DemoContainer hasWallet> | ||
<AccountInner /> | ||
</DemoContainer> | ||
); | ||
} | ||
|
||
function AccountInner() { | ||
const { address, connector } = useAccount(); | ||
|
||
return ( | ||
<div> | ||
{address ? ( | ||
<> | ||
<p> | ||
<span className="font-bold"> Connected Account: </span> {address} | ||
</p> | ||
<p> | ||
<span className="font-bold"> Connected Connector: </span>{" "} | ||
{connector?.id} | ||
</p> | ||
</> | ||
) : ( | ||
<p>Connect Wallet first</p> | ||
)} | ||
</div> | ||
); | ||
} |
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,40 @@ | ||
import { | ||
type Address, | ||
useAccount, | ||
useNonceForAddress, | ||
} from "@starknet-react/core"; | ||
import { DemoContainer } from "../starknet"; | ||
|
||
export function NonceForAddress() { | ||
return ( | ||
<DemoContainer hasWallet> | ||
<NonceForAddressInner /> | ||
</DemoContainer> | ||
); | ||
} | ||
|
||
function NonceForAddressInner() { | ||
const { account } = useAccount(); | ||
|
||
const { data, isLoading, isError, error } = useNonceForAddress({ | ||
address: account?.address as Address, | ||
}); | ||
|
||
return ( | ||
<div className="flex flex-col"> | ||
<h1 className="font-bold text-lg">Nonce for Address</h1> | ||
<div className="flex flex-col mt-2"> | ||
{account?.address ? ( | ||
<> | ||
<div>nonce: {data} </div> | ||
<div>isLoading: {isLoading ? "true" : "false"} </div> | ||
<div>isError: {isError ? "true" : "false"} </div> | ||
<div>error: {error ? error.message : "null"} </div> | ||
</> | ||
) : ( | ||
"Connect your wallet to start." | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
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,93 @@ | ||
import { | ||
type Connector, | ||
useAccount, | ||
useConnect, | ||
useContract, | ||
useNetwork, | ||
useSendTransaction, | ||
} from "@starknet-react/core"; | ||
import { DemoContainer } from "../starknet"; | ||
import { | ||
type StarknetkitConnector, | ||
useStarknetkitConnectModal, | ||
} from "starknetkit"; | ||
import type { Abi } from "starknet"; | ||
|
||
export function StarknetKit() { | ||
return ( | ||
<DemoContainer> | ||
<StarknetKitInner /> | ||
</DemoContainer> | ||
); | ||
} | ||
|
||
function StarknetKitInner() { | ||
const { connectAsync, connectors } = useConnect(); | ||
const { starknetkitConnectModal } = useStarknetkitConnectModal({ | ||
connectors: connectors as StarknetkitConnector[], | ||
}); | ||
|
||
const connectWallet = async () => { | ||
const { connector } = await starknetkitConnectModal(); | ||
if (!connector) { | ||
return; | ||
} | ||
await connectAsync({ connector: connector as Connector }); | ||
}; | ||
|
||
const { address } = useAccount(); | ||
|
||
const { chain } = useNetwork(); | ||
const { contract } = useContract({ | ||
abi, | ||
address: chain.nativeCurrency.address, | ||
}); | ||
|
||
const { isError, error, send } = useSendTransaction({ | ||
calls: | ||
contract && address | ||
? [contract.populate("transfer", [address, 1n])] | ||
: undefined, | ||
}); | ||
|
||
return ( | ||
<div> | ||
{address ? ( | ||
<> | ||
<p> | ||
<span className="font-bold"> Connected Account: </span> {address} | ||
</p> | ||
<div> | ||
<button onClick={() => send()} className="button"> | ||
Send Transaction | ||
</button> | ||
{isError && <p>Error: {error?.message}</p>} | ||
</div> | ||
</> | ||
) : ( | ||
<button onClick={connectWallet} className="button"> | ||
Connect Starknet Wallet (StarknetKit) | ||
</button> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
const abi = [ | ||
{ | ||
type: "function", | ||
name: "transfer", | ||
state_mutability: "external", | ||
inputs: [ | ||
{ | ||
name: "recipient", | ||
type: "core::starknet::contract_address::ContractAddress", | ||
}, | ||
{ | ||
name: "amount", | ||
type: "core::integer::u256", | ||
}, | ||
], | ||
outputs: [], | ||
}, | ||
] as const satisfies Abi; |
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,5 @@ | ||
import Demo from "../../components/demo"; | ||
|
||
# useAccount Demo | ||
|
||
<Demo.Account /> |
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,5 @@ | ||
import Demo from "../../components/demo"; | ||
|
||
# Nonce for Address | ||
|
||
<Demo.NonceForAddress /> |
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 @@ | ||
import Demo from "../../components/demo"; | ||
|
||
# StarknetKit Integration | ||
|
||
<Demo.StarknetKit /> |
Oops, something went wrong.