Using Polygon

Using Polygon

The following highlight how to use Polygon with the SDK, CLI, and/or smart contracts.

Before diving into the details, be sure to check out the section about Polygon in Choosing a Chain, which details the speed, low costs, and various links / resources that may be helpful during development.

The Tableland deployment contracts across Polygon mainnets and testnets are unified across all libraries, so the SDK, CLI, and smart contracts.

SDK

Selecting a chain is rather straightforward when using the SDK. Upon connecting to Tableland, pass the chain as "polygon-mumbai":

const tableland = await connect({ chain: "polygon-mumbai" })

From there, chain interactions will now leverage Polygon Mumbai. For Polygon mainnet, simply do the following:

const tableland = await connect({ chain: "polygon" })

CLI

When using the CLI, you can check the different chains supported, which includes both the Polygon Mumbai testnet and mainnet (since the CLI is built on top of the SDK). To use Polygon, specify the chain in a command — for example, with a read query:

> tableland chains # check the chains, optionally
> tableland read "SELECT * FROM healthbot_80001_1" --chain "polygon-mumbai" 
> tableland read "SELECT * FROM healthbot_137_1" --chain "polygon" 

Smart Contracts

Selecting a chain is part of the smart contract deployment process. Since Polygon is EVM compatible, it’s easy to port over contracts that may have been developed on Ethereum or with Solidity since Polygon has full EVM support.

One of the most popular frameworks for deploying smart contracts is hardhat. Although the SDK and CLI examples above didn’t mention this, it is a best practice to use a provider for interacting with a chain. This ensures developers are not throttled and have full control of their deployment process.

When using hardhat, first set up a provider API key as well as a wallet private key. Make sure you store these in a .env that is never shared publicly. Sharing keys publicly could result in the loss of funds or unwanted usage of the API key itself.

In order to run hardhat with Polygon, first set up a project with hardhat using npx hardhat and follow the prompts (e.g., “Create a basic sample project”). Once a project is set up, one could run the following to deploy a contract on Polygon:

npx hardhat run scripts/deploy.js --network "polygon-mumbai"

But first, the hardhat.config.js should be configured to resemble the following. Use dotenv as a best practice for accessing local environment variables like PRIVATE_KEY, POLYGON_API_KEY, and POLYGON_MUMBAI_API_KEY. Note that the provider used here is Alchemy, but other providers can also be used with Tableland, too (including Etherscan and Infura):

module.exports = {
	solidity: "0.8.4",
	networks: {
		polygon: {
			url: `https://polygon-mainnet.g.alchemy.com/v2/${process.env.POLYGON_API_KEY ?? ""}`,
			accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
		},
		"polygon-mumbai": {
			url: `https://polygon-mumbai.g.alchemy.com/v2/${process.env.POLYGON_MUMBAI_API_KEY ?? ""}`,
			accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
		},
	},
}