The SKALE Gasless SDK enables gasless transactions on SKALE Network, allowing users to send transactions without needing to hold gas tokens in their wallets. This SDK supports both TypeScript/JavaScript and Rust implementations.
Background on Gasless Transactions
- Gasless transactions utilize SKALE Network’s native Proof-of-Work mechanism that uses a magic number to slot into the gas price field of a transaction
Gasless transactions may not be compatible directly with wallets that explicility require and check for native gas balance.
Installation
# NPM
npm add @dirtroad/gasless
# Yarn
yarn add @dirtroad/gasless
# PNPM
pnpm add @dirtroad/gasless
# Bun
bun add @dirtroad/gasless
# Add to Cargo.toml
[dependencies]
gasless = "0.1.3"
Basic Usage
The core functionality involves generating a magic gas price that allows for gasless transactions on SKALE Network.
import { mineGasForTransaction } from "@dirtroad/gasless";
import { createPublicClient, createWalletClient, http } from "viem";
import { skaleCalypsoTestnet } from "viem/chains";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
async function main() {
const privateKey = generatePrivateKey();
const client = createPublicClient({
chain: skaleCalypsoTestnet,
transport: http()
});
const wallet = createWalletClient({
chain: skaleCalypsoTestnet,
transport: http(),
account: privateKeyToAccount(privateKey)
});
const { gasPrice } = await mineGasForTransaction(100_000, wallet.account.address, 0);
const res = await wallet.sendTransaction({
to: "0x62Fe932FF26e0087Ae383f6080bd2Ed481bA5A8A",
data: `0x0c11dedd000000000000000000000000${wallet.account.address.substring(2)}`,
gas: BigInt(100_000),
gasPrice: BigInt(gasPrice)
});
const receipt = await client.waitForTransactionReceipt({
hash: res
});
console.log("Receipt of gasless tx: ", receipt);
}
main()
.catch((err) => {
console.error("err: ", err);
throw err;
})
use gasless::mine_gas_for_transaction;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let gas_amount = 100_000;
let address = "0x62Fe932FF26e0087Ae383f6080bd2Ed481bA5A8A".to_string();
let nonce = 0;
// Mine gas for transaction
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(mine_gas_for_transaction(gas_amount, address, nonce))?;
println!("Magic Value: {}", result.gas_price);
println!("Mining duration: {} ms", result.duration);
// Use the gas price in your transaction
// The result.gas_price can be used with any Web3 library
Ok(())
}
Important Notes
This SDK and functionality is built and exists solely for use with SKALE Chains with Proof-of-Work enabled. This does not work with non-sChains or sChains that have chosen to disable Proof-of-Work like SKALE on Base.
The SDK utilizes heavy mathematical operations on the active device CPU.