Accessing the Blockchain
Your function has direct access to a selection of our supported blockchains through the dedicated .rpc
domain. As a shorthand, you can access the blockchain that triggered your function through the chain-provider
domain if it's listed below.
Chain | URL |
---|---|
Current Chain | http://chain-provider |
Ethereum Mainnet | http://eth-mainnet.rpc |
Sepolia Network (Ethereum Testnet) | http://sepolia.rpc |
Arbitrum One | http://arbitrum-one.rpc |
BSC (BNB) Mainnet | http://bnb-mainnet.rpc |
BSC (BNB) Testnet | http://bnb-testnet.rpc |
Cosmos Osmosis Mainnet | http://cosmos-osmosis1.rpc |
Mantle Mainnet | http://mantle-mainnet.rpc |
Polygon (Matic) Mainnet | http://polygon-mainnet.rpc |
StarkNet Mainnet | http://starknet-mainnet.rpc |
Available Packages
For all networks aside from StarkNet
, use the ethers
library to communicate with our nodes. For StarkNet
, use starknet.js
.
See Importing Libraries for more package details.
Samples
Ethereum
The following snippet gets the balance of vitalik.eth
on Ethereum mainnet, regardless of the event that triggered the function.
import { ethers } from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
import { Big } from "https://cdn.skypack.dev/[email protected]";
const provider = new ethers.JsonRpcProvider("http://eth-mainnet.rpc");
export async function triggerHandler() {
return { balance: new Big(await provider.getBalance("vitalik.eth")) };
}
StarkNet
The following snippet injects the chain ID into the provided data, regardless of the event that triggered the function. This is simply for illustration purposes, as in practice, the chainId
is already available to the function in the context
object.
import * as starknet from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
const provider = new starknet.RpcProvider({
nodeUrl: "http://starknet-mainnet.rpc",
});
export async function triggerHandler(context, data) {
const contextChainId = context.chainId;
const providerChainId = await provider.getChainId();
return {
contextChainId,
providerChainId,
doesProviderChainIdMatchContext: Number(providerChainId) === Number(context.chainId),
};
}
Cosmos Osmosis
The following snippet demonstrates interacting with the Cosmos Osmosis chain by retrieving the details of the given pool, regardless of the event that triggered the function, and returning a subset of the response.
import { osmosis } from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
import { Big } from "https://cdn.skypack.dev/[email protected]";
const providerUri = "http://cosmos-osmosis1.rpc";
const { createRPCQueryClient } = osmosis.ClientFactory;
const provider = await createRPCQueryClient({ rpcEndpoint: providerUri });
// https://celatone.osmosis.zone/osmosis-1/pools/1705
const poolId = 1705;
export async function triggerHandler(context, data) {
let response = await provider.osmosis.poolmanager.v1beta1.pool({ poolId: poolId });
return {
pool: {
id: new Big(response.pool.id),
address: response.pool.address,
incentivesAddress: response.pool.incentivesAddress,
},
};
}
Updated 29 days ago