Skip to content
Send Transactions

Overview

Simple Crypto’s Send Transaction API removes the complexity from submitting blockchain transactions.

Submitting transactions come with a number of pain points including:

  • Setting transaction fees
  • Dropped transactions during high congestion
  • Mananging the nonce of accounts

These painpoints are frustrating for both users and developers. As a result, many developers try building their own version of the Send Transaction API.

💡

Through the code and our documentation, we often refer to sending transactions as "relaying." If you see "relay", think send transaction!

Getting Started

Install

  npm i @simplecrypto/relay-provider
  pnpm i @simplecrypto/relay-provider
  yarn add @simplecrypto/relay-provider

Import

ES Modules / Typescript

import { RelayProvider } from "@simplecrypto/relay-provider";

const provider = new RelayProvider(window.ethereum, {
	baseUrl: "https://api.simplecrypto.dev",
  apiKey: "your_api_key"
});

CommonJS

const { RelayProvider } = require("@simplecrypto/relay-provider");

const provider = new RelayProvider(window.ethereum, {
	baseUrl: "https://api.simplecrypto.dev",
  apiKey: "your_api_key"
});

Ethers.js

Our RelayProvider is an EIP1193 provider which means it’s a lower level provider and it will typically be used under the hood by your Ethereum SDK. Here’s an example of how to create an ethers.js Web3Provider using your RelayProvider.

import { RelayProvider } from "@simplecrypto/relay-provider";
import { ethers } from "ethers";

const relayProvider = new RelayProvider(window.ethereum, {
	baseUrl: "https://api.simplecrypto.dev",
  apiKey: "your_api_key"
});

const web3Provider = new ethers.providers.Web3Provider(relayProvider);

Custom Provider

The examples above use window.ethereum as an underlying ethereum provider, but if you don't want to rely on a browser wallet or aren't using Send Transaction in your browser, you can use your own JSON Rpc Endpoint. We'd recommend Alchemy if you don't have one already.

import { RelayProvider } from "@simplecrypto/relay-provider";
import { ethers } from "ethers";

const relayProvider = RelayProvider.fromJsonRpcEndpoint("https://eth-mainnet.g.alchemy.com/v2/<your_alchemy_api_key>", {
	baseUrl: "https://api.simplecrypto.dev",
  apiKey: "your_api_key"
});

const web3Provider = new ethers.providers.Web3Provider(relayProvider);

Send a Transaction

There are a couple ways to send transactions through Simple Crypto. In this example, we'll call a simple smart contract on Polygon on using the popular Ethereum library ethers.js.

Our demo smart contract has a function greet that emits a greeting when called.

This process is identical to submitting a transaction with Ethers.js without using Simple Crypto's API under the hood.

import { ethers } from "ethers";
import { RelayProvider } from "@simplecrypto/relay-provider";

const GREETER_ADDRESS = "0x7Cc37C2a77CBbf28E254BFb2c37A6F82e0C0ee97";

const relayProvider = new RelayProvider(window.ethereum, {
	baseUrl: "https://api.simplecrypto.dev",
  apiKey: "your_api_key"
});

const provider = new ethers.providers.Web3Provider(relayProvider);

/**
 * Create an ethers.js Contract class which is a helpful class for interacting with your smart contracts.
 * The constructor takes 3 arguments:
 *   1. The address of the contract.
 *   2. The ABI of the contract. Normally this is a JSON file but ethers also supports
 *      human readable ABIs like you see below
 *   3. A "signer" which is used for signing and handling all requests to the blockchain.
 *      The signer will need to use Simple Crypto under the hood to make requests to our wallet.
 */
const greeterContract = new ethers.Contract(
  GREETER_ADDRESS,
  ["function greet() public"],
  provider.getSigner(),
);

const txResponse = await greeterContract.greet();

Query a TX

Now, let's query for the transaction receipt from the previous step. You'll notice once again that you don't have to make any changes to how you're already using ethers.js.

// take the txResponse variable from the previous step
const receipt = await txResponse.wait();

Great! Now we know the transaction has confirmed on chain and we have the transaction receipt.

Directly Use The API

If you need more flexibility, you can integrate directly with our API. Under the hood, there's 2 core API endpoints:

  1. Submit a transaction
  2. Query for a transaction

Reach out to shekar@simplecrypto.dev if you're interested in this offering.

Last updated on February 7, 2023