Skip to content

Commit

Permalink
feat: working solid solana web3 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanaden committed Sep 23, 2024
1 parent 7a400bc commit f6b88e5
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 121 deletions.
3 changes: 1 addition & 2 deletions examples/start-tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
"deploy": "vinxi build && wrangler pages deploy dist"
},
"dependencies": {
"@solana/web3.js": "^2.0.0-rc.1",
"@nanostores/solid": "^0.4.2",
"@solana-wallets-solid/solid": "workspace:*",
"@solana-wallets-solid/unified": "workspace:*",
"@solana/spl-token": "^0.4.8",
"@solana/web3.js": "^1.95.3",
"@solidjs/router": "^0.14.1",
"@solidjs/start": "^1.0.6",
"nanostores": "^0.11.3",
Expand Down
3 changes: 2 additions & 1 deletion examples/start-tailwind/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "./app.css"
import { Router } from "@solidjs/router"
import { FileRoutes } from "@solidjs/start/router"
import { Suspense } from "solid-js"
import { WalletProvider } from "@solana-wallets-solid/solid"

import Nav from "~/components/Nav"
import { WalletProvider } from "@solana-wallets-solid/solid"

export default function App() {
// const adapters = [
Expand Down Expand Up @@ -37,6 +37,7 @@ export default function App() {
autoConnect={true}
disconnectOnAccountChange={true}
localStorageKey="unified:wallet-stoarge-key"
env="devnet"
>
<unified-wallet-modal
autoConnect={true}
Expand Down
162 changes: 70 additions & 92 deletions examples/start-tailwind/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { A } from "@solidjs/router"

import { useWallet } from "@solana-wallets-solid/solid"
import { getTransferSolInstruction } from "@solana-program/system"
import { Show, createMemo } from "solid-js"
import {
Connection,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
address,
appendTransactionMessageInstruction,
createDefaultRpcTransport,
createSolanaRpcFromTransport,
createTransactionMessage,
lamports,
pipe,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signAndSendTransactionMessageWithSigners,
} from "@solana/web3.js"
// import {
// Connection,
// LAMPORTS_PER_SOL,
// PublicKey,
// SystemProgram,
// Transaction,
// } from "@solana/web3.js"

const SIGN_ARBITRARY_MSG = new TextEncoder().encode("Hello World")
export const MAINNET_RPC_ENDPOINT = import.meta.env.DEV
Expand All @@ -24,8 +22,7 @@ export const MAINNET_RPC_ENDPOINT = import.meta.env.DEV
const DEVNET_RPC_ENDPOINT = "https://api.devnet.solana.com"

export default function Home() {
const { signMessage, signTransaction, signAllTransactions, sendTransaction, connectedAccount } =
useWallet()
const { signMessage, getTransactionSendingSigner, connectedAccount } = useWallet()
const publicKey = createMemo(() => connectedAccount()?.pubKey)

async function signArbitary() {
Expand All @@ -39,88 +36,69 @@ export default function Home() {
}
}

// async function sendTxV2() {
//
// // Create an HTTP transport or any custom transport of your choice.
// const transport = createDefaultRpcTransport({ url: "https://api.devnet.solana.com" })
//
// // Create an RPC client using that transport.
// const rpc = createSolanaRpcFromTransport(transport)
//
// // Send a request.
// const recentBlockhashRes = await rpc.getLatestBlockhash().send()
// const recentBlockhash = recentBlockhashRes.value
//
// const pubKey = publicKey()
// if (!pubKey) {
// console.error("cannot send tx without logging in !")
// return
// }
//
// // Create the transfer SOL instruction by passing the signer as the source.
// const instruction = getTransferSolInstruction({
// amount: 1n,
// destination: address(pubKey),
// source: { address: address(pubKey)},
// })
//
// const transactionMessage = pipe(
// createTransactionMessage({ version: 0 }),
// tx => setTransactionMessageFeePayer(address(pubKey), tx),
// tx => setTransactionMessageLifetimeUsingBlockhash(recentBlockhash, tx),
// )
//
// console.log({ transactionMessage })
//
// const compiled = compileTransaction(transactionMessage)
//
// console.log({ compiled })
// // const decoder = getBytesDecoder();
// // decoder.decode
// // compiled.messageBytes
// // const bytes = new Uint8Array(compiled.messageBytes)
//
// console.log({ bytes: compiled.messageBytes })
// const sig = await signTransaction(compiled.messageBytes as unknown as Uint8Array)
//
// console.log({ sig })
//
//
// }

async function sendTxV1() {
const APPEAL_WALLET_PUBKEY = new PublicKey("Hm9YjuVadcekDPbLeCSFE83r1QLpS2ksmKk7Sn5BCpfL")
const rawPubKey = publicKey()
if (!rawPubKey) {
console.error("cannot sign tx, no pub key: ", { rawPubKey })
async function sendTxV2() {
const transactionSendingSigner = getTransactionSendingSigner()
if (!transactionSendingSigner) {
console.error("sendTx: missing TransactionSendingSigner!")
return
}
const pubKey = new PublicKey(rawPubKey)
const lamportsToSend = 0.1 * LAMPORTS_PER_SOL
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: pubKey,
toPubkey: APPEAL_WALLET_PUBKEY,
lamports: lamportsToSend,
}),
)

const connection = new Connection(DEVNET_RPC_ENDPOINT, "confirmed")
const latestHash = await connection.getLatestBlockhash("finalized")
if (transaction instanceof Transaction) {
transaction.recentBlockhash = latestHash.blockhash
transaction.feePayer = pubKey
}
const tx = new Uint8Array(
transaction.serialize({ verifySignatures: false, requireAllSignatures: false }),
// Create an HTTP transport or any custom transport of your choice.
const transport = createDefaultRpcTransport({ url: "https://api.devnet.solana.com" })
// Create an RPC client using that transport.
const rpc = createSolanaRpcFromTransport(transport)
const recentBlockhashRes = await rpc.getLatestBlockhash().send()
const recentBlockhash = recentBlockhashRes.value
const amount = lamports(1_00_000_000n)

const message = pipe(
createTransactionMessage({ version: 0 }),
m => setTransactionMessageFeePayerSigner(transactionSendingSigner, m),
m => setTransactionMessageLifetimeUsingBlockhash(recentBlockhash, m),
m =>
appendTransactionMessageInstruction(
getTransferSolInstruction({
amount,
destination: address("W4jj84Hs5Ts6BFK2nnmwbZYGGXzo5TdHdsLsakZqE5Y"),
source: transactionSendingSigner,
}),
m,
),
)
const res = await sendTransaction(tx)
console.log("successful tx: ", { res })

const res = await signAndSendTransactionMessageWithSigners(message)
console.log({ res })
}

// createEffect(() => {
// console.log({ showmodal: showModal() })
// })
// async function sendTxV1() {
// const APPEAL_WALLET_PUBKEY = new PublicKey("Hm9YjuVadcekDPbLeCSFE83r1QLpS2ksmKk7Sn5BCpfL")
// const rawPubKey = publicKey()
// if (!rawPubKey) {
// console.error("cannot sign tx, no pub key: ", { rawPubKey })
// return
// }
// const pubKey = new PublicKey(rawPubKey)
// const lamportsToSend = 0.1 * LAMPORTS_PER_SOL
// const transaction = new Transaction().add(
// SystemProgram.transfer({
// fromPubkey: pubKey,
// toPubkey: APPEAL_WALLET_PUBKEY,
// lamports: lamportsToSend,
// }),
// )
//
// const connection = new Connection(DEVNET_RPC_ENDPOINT, "confirmed")
// const latestHash = await connection.getLatestBlockhash("finalized")
// if (transaction instanceof Transaction) {
// transaction.recentBlockhash = latestHash.blockhash
// transaction.feePayer = pubKey
// }
// const tx = new Uint8Array(
// transaction.serialize({ verifySignatures: false, requireAllSignatures: false }),
// )
// const res = await sendTransaction(tx)
// console.log("successful tx: ", { res })
// }

return (
<main class="text-center mx-auto text-gray-700 p-4 space-y-8">
Expand Down Expand Up @@ -167,7 +145,7 @@ export default function Home() {
<button class="rounded-lg px-3 py-1.5 text-lg bg-blue-300 w-fit" onClick={signArbitary}>
Sign message!
</button>
<button class="rounded-lg px-3 py-1.5 text-lg bg-blue-300 w-fit" onClick={sendTxV1}>
<button class="rounded-lg px-3 py-1.5 text-lg bg-blue-300 w-fit" onClick={sendTxV2}>
Send tx!
</button>
</Show>
Expand Down
5 changes: 4 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@
"devDependencies": {
"@scure/base": "^1.1.9",
"@solana-mobile/wallet-adapter-mobile": "^2.1.3",
"@solana/transactions": "2.0.0-experimental.743e0a6",
"@solana/wallet-adapter-base": "^0.9.23",
"@solana/wallet-standard-features": "^1.2.0",
"@solana/wallet-standard-wallet-adapter-base": "^1.1.2",
"@wallet-standard/app": "1.0.1",
"@wallet-standard/base": "1.0.1",
"@wallet-standard/core": "1.0.1",
"@wallet-standard/features": "1.0.1",
"nanostores": "^0.11.3"
"@solana/web3.js": "^2.0.0-rc.1",
"nanostores": "^0.11.3",
"@solana-program/system": "0.5.1"
},
"peerDependencies": {
"nanostores": "^0.11.3"
Expand Down
Loading

0 comments on commit f6b88e5

Please sign in to comment.