Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fresh web wallet #172

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/get-starknet flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"dependencies": {
"get-starknet": "workspace:^2.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"starknet": "^4.22.0"
},
"devDependencies": {
"@types/react": "^18.0.15",
Expand Down
82 changes: 77 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,95 @@ import "./App.css"
import {
type ConnectOptions,
type DisconnectOptions,
StarknetWindowObject,
connect,
disconnect,
} from "get-starknet"
import { useState } from "react"
import {
AccountInterface,
Contract,
ProviderInterface,
stark,
uint256,
} from "starknet"

const ADDR_GROM_TOKEN =
"0x053fe9d5fdce9c4cae6ad2a03b0ce2a3fc5cb3a4e6a18475addd1e09e67807b5"
const CLASS_HASH_GROM_TOKEN =
"0x2784249255cace9a06ef1e25fa31f438a937a251e41ae80852afb1700755c9e"

function App() {
const [walletName, setWalletName] = useState("")
const [swo, setSWO] = useState<StarknetWindowObject>()
const [balance, setBalance] = useState("")
const [txhash, setTxhash] = useState("")

function handleConnect(options?: ConnectOptions) {
return async () => {
const res = await connect(options)
console.log(res)
setWalletName(res?.name || "")
res?.on("accountsChanged", (account: string[]) =>
console.log("catch account changed: " + account[0]),
)
res?.on("networkChanged", (s?: string) =>
console.log("catch network change: " + s),
)
if (res !== null) setSWO(res)
}
}

function handleDisconnect(options?: DisconnectOptions) {
return async () => {
await disconnect(options)
setWalletName("")
setSWO(undefined)
}
}

const getERC20Balance = async () => {
if (swo !== undefined && swo.provider != undefined) {
const contractClass = await swo.provider.getClassAt(ADDR_GROM_TOKEN)
if (contractClass.abi) {
const ERC20Contract = new Contract(
contractClass.abi,
ADDR_GROM_TOKEN,
swo.provider,
)
const balance = await ERC20Contract.balanceOf(swo.account?.address)
console.log(uint256.uint256ToBN(balance.balance).toString())
setBalance(uint256.uint256ToBN(balance.balance).toString())
}
}
}

const transferToken = async () => {
const dest =
"0x448bda3c2c437ca22579fed986eb65519820c52f2249a00370ec0f3d1902d78"
// Execute tx transfer of 10 tokens
console.log(`Invoke Tx - Transfer 10 tokens`)
const toTransferTk = uint256.bnToUint256(10)
const transferCallData = stark.compileCalldata({
recipient: dest,
amount: {
type: "struct",
low: toTransferTk.low,
high: toTransferTk.high,
},
})

const { transaction_hash: transferTxHash } = await swo?.account?.execute(
{
contractAddress: ADDR_GROM_TOKEN,
entrypoint: "transfer",
calldata: transferCallData,
},
undefined,
{ maxFee: 900_000_000_000_000 },
)

console.log("Transfer Tx hash: " + transferTxHash)
setTxhash(transferTxHash)
}

return (
<div className="App">
<h1>get-starknet</h1>
Expand Down Expand Up @@ -55,10 +121,16 @@ function App() {
Disconnect and reset
</button>
</div>
{walletName && (
{swo && (
<div>
<h2>
Selected Wallet: <pre>{walletName}</pre>
Selected Wallet:
<pre>{swo.name}</pre>
<pre>{swo.selectedAddress}</pre>
<button onClick={getERC20Balance}>Get ERC20 Balance</button>
{balance && <pre>{balance}</pre>}
<button onClick={transferToken}>Transfer Token</button>
{txhash && <pre>Tx hash: {txhash}</pre>}
</h2>
</div>
)}
Expand Down
Loading