What is @safe-global/safe-gateway-typescript-sdk?
@safe-global/safe-gateway-typescript-sdk is a TypeScript SDK for interacting with the Safe Global Gateway API. It allows developers to manage and interact with Safe (formerly Gnosis Safe) smart contracts, providing functionalities such as retrieving Safe information, transaction history, and managing transactions.
What are @safe-global/safe-gateway-typescript-sdk's main functionalities?
Retrieve Safe Information
This feature allows you to retrieve information about a specific Safe, such as its owners, threshold, and nonce.
const { SafeGatewayClient } = require('@safe-global/safe-gateway-typescript-sdk');
const client = new SafeGatewayClient('https://safe-transaction.mainnet.gnosis.io');
async function getSafeInfo(safeAddress) {
const safeInfo = await client.getSafeInfo(safeAddress);
console.log(safeInfo);
}
getSafeInfo('0xYourSafeAddress');
Retrieve Transaction History
This feature allows you to retrieve the transaction history of a specific Safe, including executed and pending transactions.
const { SafeGatewayClient } = require('@safe-global/safe-gateway-typescript-sdk');
const client = new SafeGatewayClient('https://safe-transaction.mainnet.gnosis.io');
async function getTransactionHistory(safeAddress) {
const history = await client.getTransactionHistory(safeAddress);
console.log(history);
}
getTransactionHistory('0xYourSafeAddress');
Propose a Transaction
This feature allows you to propose a new transaction for a specific Safe. The transaction can then be confirmed by the required number of owners.
const { SafeGatewayClient } = require('@safe-global/safe-gateway-typescript-sdk');
const client = new SafeGatewayClient('https://safe-transaction.mainnet.gnosis.io');
async function proposeTransaction(safeAddress, transaction) {
const response = await client.proposeTransaction(safeAddress, transaction);
console.log(response);
}
const transaction = {
to: '0xRecipientAddress',
value: '1000000000000000000', // 1 ETH in wei
data: '0x',
operation: 0
};
proposeTransaction('0xYourSafeAddress', transaction);
Other packages similar to @safe-global/safe-gateway-typescript-sdk
ethers
The ethers.js library is a complete and compact library for interacting with the Ethereum blockchain and its ecosystem. It provides functionalities for managing wallets, interacting with smart contracts, and querying blockchain data. Compared to @safe-global/safe-gateway-typescript-sdk, ethers.js offers a broader range of functionalities but does not specifically target Safe smart contracts.
web3
The web3.js library is a collection of modules that contain functionalities for the Ethereum ecosystem. It allows developers to interact with Ethereum nodes, manage accounts, and interact with smart contracts. Similar to ethers.js, web3.js provides a wide range of functionalities but does not specifically focus on Safe smart contracts.
gnosis-safe
The gnosis-safe npm package is a JavaScript library for interacting with Gnosis Safe smart contracts. It provides functionalities for creating, managing, and interacting with Gnosis Safe wallets. While it offers similar functionalities to @safe-global/safe-gateway-typescript-sdk, it is not specifically designed as a TypeScript SDK and may lack some of the type safety and developer experience improvements.
Safe Gateway TypeScript SDK
A TypeScript SDK for the Safe Client Gateway
📖 Type reference | CGW Swagger
Usage policy
NB: Safe Client Gateway isn't meant for public use.
Please do not use this SDK if you're building, e.g., a Safe App.
Using the SDK
Install:
yarn add @safe-global/safe-gateway-typescript-sdk
Import:
import { getChainsConfig, type ChainListResponse } from '@safe-global/safe-gateway-typescript-sdk'
Use:
const chains = await getChainsConfig()
The SDK needs no initialization unless you want to override the base URL. You can set an alternative base URL like so:
import { setBaseUrl } from '@safe-global/safe-gateway-typescript-sdk'
setBaseUrl('https://safe-client.staging.5afe.dev')
The full SDK reference can be found here.
Adding an endpoint
Endpoint types are defined in src/types/gateway.ts
.
Each endpoint consists of:
- a function defined in
src/index.ts
(e.g. getBalances
) - a path definition (e.g.
'/chains/{chainId}/safes/{address}/balances/{currency}/'
) - operation definition (e.g.
safes_balances_list
) - response definition
To add a new endpoint, follow the pattern set by the existing endpoints.
Eslint & prettier
This command will run before every commit:
yarn eslint:fix
Tests
To run the unit and e2e tests locally:
yarn test
N.B.: the e2e tests make actual API calls on staging.