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

Fix the issue with indefinite loading #726

Open
wants to merge 1 commit into
base: main
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
67 changes: 41 additions & 26 deletions dapps/universal-provider-solana/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UniversalProvider from "@walletconnect/universal-provider";
import { WalletConnectModal } from "@walletconnect/modal";
import { useState } from "react";
import { useState, useEffect } from "react";
import { signMessage, sendTransaction, SolanaChains } from "./utils/helpers";

const projectId = import.meta.env.VITE_PROJECT_ID;
Expand All @@ -20,34 +20,46 @@ const modal = new WalletConnectModal({
});

// 4. create provider instance
const provider = await UniversalProvider.init({
logger: "error",
projectId: projectId,
metadata: {
name: "WalletConnect x Solana",
description: "Solana integration with WalletConnect's Universal Provider",
url: "https://walletconnect.com/",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
});

const App = () => {
const [isConnected, setIsConnected] = useState(false);
const [provider, setProvider] = useState<UniversalProvider | null>(null);
const [address, setAddress] = useState<string | null>(null);

useEffect(() => {
const initProvider = async () => {
const newProvider = await UniversalProvider.init({
logger: "error",
projectId: projectId,
metadata: {
name: "WalletConnect x Solana",
description: "Solana integration with WalletConnect's Universal Provider",
url: "https://walletconnect.com/",
icons: ["https://avatars.githubusercontent.com/u/37784886"],
},
});

// 5. get address once loaded
const address =
provider.session?.namespaces.solana?.accounts[0].split(":")[2];
setProvider(newProvider);

// 6. handle display_uri event and open modal
provider.on("display_uri", async (uri: string) => {
console.log("uri", uri);
await modal.openModal({
uri,
});
});
// Set up event listener
newProvider.on("display_uri", async (uri: string) => {
console.log("uri", uri);
await modal.openModal({ uri });
});
};

initProvider();
}, []);

useEffect(() => {
if (provider && provider.session) {
const newAddress = provider.session?.namespaces.solana?.accounts[0].split(":")[2];
setAddress(newAddress);
}
}, [provider, provider?.session]);

// 7. handle connect event
const connect = async () => {
if (!provider) return;
try {
await provider.connect({
namespaces: {
Expand All @@ -59,31 +71,34 @@ const App = () => {
},
});
setIsConnected(true);
console.log("session", provider.session);
console.log("::::::: session", provider.session);
} catch {
console.log("Something went wrong, request cancelled");
console.log("####### Something went wrong, request cancelled");
}
modal.closeModal();
};

// 8. handle disconnect event
const disconnect = async () => {
if (!provider) return;
await provider.disconnect();
setIsConnected(false);
};

// 9. handle signMessage and sendTransaction
const handleSign = async () => {
if (!provider || !address) return;
const res = await signMessage(
`Can i have authorize this request pls bossman - ${Date.now()}`,
provider,
address!
address
);
console.log(res);
};

const handleSend = async () => {
const res = await sendTransaction(address!, 1000, provider, address!);
if (!provider || !address) return;
const res = await sendTransaction(address, 1000, provider, address);
console.log(res);
};

Expand Down
2 changes: 1 addition & 1 deletion dapps/universal-provider-solana/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function verifyTransactionSignature(
tx: Transaction
) {
return nacl.sign.detached.verify(
tx.serializeMessage(),
new Uint8Array(tx.serializeMessage()),
bs58.decode(signature),
bs58.decode(address)
);
Expand Down
3 changes: 3 additions & 0 deletions dapps/universal-provider-solana/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfil

// https://vitejs.dev/config/
export default defineConfig({
build: {
target: 'esnext', // Change this to 'esnext' or a more recent version
},
plugins: [react()],
optimizeDeps: {
esbuildOptions: {
Expand Down