Skip to content

Instantly share code, notes, and snippets.

@zeroXBami
Created December 24, 2020 07:50
Show Gist options
  • Save zeroXBami/c02a224d5b6895991a9a86c270ee3bb4 to your computer and use it in GitHub Desktop.
Save zeroXBami/c02a224d5b6895991a9a86c270ee3bb4 to your computer and use it in GitHub Desktop.
How to use wallet connect
- Import thư viện và tạo connector
import WalletConnect from "@walletconnect/client";
import QRCodeModal from "@walletconnect/qrcode-modal";
// Create a connector
const connector = new WalletConnect({
bridge: "https://bridge.walletconnect.org", // Required
qrcodeModal: QRCodeModal,
});
- Connect wallet:
if (!connector.connected) {
// create new session
connector.createSession();
}
// Subscribe to connection events
connector.on("connect", (error, payload) => {
if (error) {
throw error;
}
console.log("connect ok")
// Get provided accounts and chainId
const {
accounts,
chainId
} = payload.params[0];
this.setState({
account: accounts[0]
})
- Tạo transaction:
const data = this.state.smartcontract.methods.functionName(param....).encodeABI();
const tx = {
from: this.state.account, // Required
to: contractAddress, // Required (for non contract deployments)
data: data, // Required
gasPrice: "0x02540be400", // Optional
gas: "0x9c40", // Optional
value: "0x00", // Optional
nonce: "0x0114", // Optional
};
// Send transaction
connector
.sendTransaction(tx)
.then((result) => {
// Returns transaction id (hash)
console.log(result);
})
.catch((error) => {
// Error returned when rejected
console.error(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment