Interact with the Blockchain

Get an ethers.js Signer

๐Ÿ‘

Compatible with ethers.js

The SDK provides an ethers.js Signer, the same one you may already be using to support MetaMask, Coinbase Wallet, and other wallet providers.

There is no additional code to add support for EWS wallets!

const { user } = await sdk.auth.loginWithPaperModal();

const signer = await user.wallet.getEthersJsSigner({
  // Provide your RPC node URL on production.
  // Defaults to a public RPC node which is not recommended for production use.
  rpcEndpoint: "https://eth-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY",
});

Reference: getEthersJsSigner

Switch the wallet's network

const { user } = await sdk.auth.loginWithPaperModal();
// switches future interaction to be based on the Mumbai chain
await user.wallet.setChain({ chain: "Mumbai" });

Reference: setChain

Sign a message

Sign a message to verify the user owns this wallet address.
This action does not incur gas fees.

Use cases:

  • Allow the user to update off-chain data, like their username or profile picture, associated with this wallet address.
  • Sign in with Ethereum

Here's an example of a user signing a message to verify they are the owner of that wallet address.

import { ethers } from "ethers";

// On frontend client:
// Get a signer for an authenticated user.
const { user } = await sdk.auth.loginWithPaperModal();
const signer = await user.wallet.getEthersJsSigner();

const payload = "myNewUsername";
const signature = await signer.signMessage(payload);
// Pass `payload` and `signature` to your backend.

// On your backend:
const walletAddress = ethers.utils.verifyMessage(payload, signature);
console.log(`${walletAddress} is updating their username to '${payload}'.`);
// "0x... is updating their username to 'myNewUsername'."

Ethers.js reference: signer.signMessage

Send a transaction

Send a transaction to write to the blockchain.
This action requires gas in the user's wallet.

Use cases:

  • Transfer the native coin or ERC-20 token to another wallet or contract.
  • List an NFT on a marketplace.
  • Transfer an NFT to another wallet.
  • Update metadata on an NFT.
import { ethers } from "ethers";

// Get signer for an authenticated user.
const { user } = await sdk.auth.loginWithPaperModal();
const signer = await user.wallet.getEthersJsSigner();

// simple example of sending the native chain coin to some other address
const tx = {
  to: "0x8ba1f109551bD432803012645Ac136ddd64DBA72",
  value: ethers.utils.parseEther("0.1"),
};
const txResponse = await signer.sendTransaction(tx);
const txReceipt = await txResponse.wait();
console.log("Transaction sent:", txReceipt.transactionHash);

// if you want to call a smart contract 
const interface = new ethers.utils.Interface(["function executeSale(uint256 tokenId) public"]);
// encode the data to be sent to the contract
const dataToSend = interface.encodeFunctionData("executeSale", [tokenId]);
// send the transaction to write to the contract
const txResponseToContract = await signer.sendTransaction({to: '0x8ba1f109551bD432803012645Ac136ddd64DBA72', 
                                                     value: ethers.utils.parseEther("1"), 
                                                     data: dataToSend});
const txReceiptToContract = await txResponseToContract.wait()
console.log("Transaction sent:", txReceiptToContract.transactionHash);

Ethers.js reference: sendTransaction