Typescript
Core SDK - Typescript
The XpansionChain Core SDK provides convenient access to XpansionChain's APIs and smart contracts to help projects build better web3 games and marketplaces.
💡ROLLUPS THIS SDK SUPPORTS
XpansionChain
CONTENTS
Installation
Initialization
Get data (on assets, orders, past transactions, etc.)
Operations requiring user signatures
Contract requests
Smart contract autogeneration
Metadata refresh
Migration guide
Further documentation
📚SDK LINKS
npm package
SDK reference
Code examples
Github repository
Installation
Add the npm package to your project:
Initialization
Initialize the Core SDK client with the network on which you want your application to run (see all networks available):
Config.SANDBOX
The default test network (currently, it is Goërli)
Config.PRODUCTION
Ethereum network
Get data (on assets, orders, past transactions, etc.)
These methods allow you to read data about events, transactions or current state on XpansionChain (layer 2). They do not require any user authentication because no state is being changed.
Examples of the types of data that are typically retrieved include:
Assets or details of a particular asset
Token balances for a particular user
Orders or details about a particular order
Historical trades and transfers
Get all collections and get assets from a particular collection:
Operations requiring user signatures
There are two types of operations requiring user signatures:
Transactions that update blockchain state
Operations that XpansionChain require authorization for
In order to get user signatures, applications need to generate "signers".
What are transactions that update blockchain state?
A common transaction type is the transfer of asset ownership from one user to another (ie. asset sale). These operations require users to sign (approve) them to prove that they are valid.
What are operations that require authorization?
These operations add to or update data in XpansionChain's databases and typically require the authorization of an object's owner (ie. a user creating a project on XpansionChain).
How do applications generate and use signers?
Signers are abstractions of user accounts that can be used to sign transactions. A user's private key is required to generate them.
There are two ways to get signers in your application:
Generate your own by obtaining and using the user's private keys
Use our Wallet SDK to connect to a user's wallet application
The first option, where an application obtains a user's private key directly, is risky because these keys allow anyone in possession of them full control of an account.
The second option provides an application with an interface to the user's account by prompting the user to connect with their wallet application (ie. mobile or browser wallet). Once connected the app can begin asking the user to sign transactions and messages without having to reveal their private key.
As XpansionChain enables applications to execute signed transactions on both Ethereum (layer 1) and StarkEx (layer 2), signers are required for both these layers.
1. Generate your own signers
The Core SDK provides functionality for applications to generate STARK (L2) private keys and signers.
🚨🚨🚨 Warning 🚨🚨🚨
If you generate your own STARK private key, you will have to persist it. The key is randomly generated so cannot be deterministically re-generated.
2. Generate signers using the Wallet SDK
The Wallet SDK Web provides connections to Metamask and WalletConnect browser wallets.
See this guide for how to set this up.
Examples
Create a project (requires an Ethereum layer 1 signer)
Deposit tokens from L1 to L2 (requires an Ethereum layer 1 signer)
Create an order (requires an Ethereum layer 1 and StarkEx layer 2 signer)
Contract requests
XpansionChain is built as a zkRollup (zero-knowledge rollup) in partnership with StarkWare. The choice of zkRollups is deliberate, as it offers unparalleled scalability without compromising security. This means the following:
Cost-effectiveness: When you mint or trade an NFT on XpansionChain, you enjoy the advantage of zero gas fees.
Security: All transactions are secured by zero-knowledge proofs, which makes XpansionChain the first ever layer 2 solution for NFTs on Ethereum.
The Core SDK provides an interface with the smart contracts necessary for interacting with the XpansionChain platform.
See all smart contracts available in the Core SDK.
See all smart contracts available in the Core SDK.
Smart contract autogeneration
The XpansionChain Solidity contracts can be found in the contracts
folder. Contract bindings in TypeScript are generated using hardhat.
Core
The Core contract is XpansionChain's main interface with the Ethereum blockchain, based on StarkEx.
Registration
The Registration contract is a proxy smart contract for the Core contract that combines transactions related to onchain registration, deposits, and withdrawals. When a user who is not registered onchain attempts to perform a deposit or a withdrawal, the Registration combines requests to the Core contract in order to register the user first. Users who are not registered onchain are not able to perform a deposit or withdrawal.
IERC20
Standard interface for interacting with ERC20 contracts, taken from OpenZeppelin.
IERC721
Standard interface for interacting with ERC721 contracts, taken from OpenZeppelin.
Metadata refresh
XpansionChain allows developers to request asset metadata refreshes on-demand. This introductory guide shows you how to use the Core Typescript SDK to request and check for updates on your metadata refreshes.
Pre-requisites
Installed the Core Typescript SDK
Initialized the Core Typescript SDK
Have satisified all the metadata refresh requirements
Create Ethereum Signer
First, we need to create an Ethereum Signer
to tell XpansionChain that you are the project_owner
of the project containing the collection of your assets:
Construct the refresh request
In order to construct the refresh request, we need to grab the token ids for the assets that require a metadata refresh:
CUSTOMISING LIST ASSETS
You can narrow down the results returned by listAssets. Please refer to the listAssets request SDK reference.
For more information regarding limits on metadata refreshes, please refer to metadata refresh limits.
Request the refresh
Checking the status of your request
The duration of the refresh depends on the amount of tokens in the request. You can check on the status of your request using the following code:
METADATA REFRESH STATUSES
For more information regarding refresh and summary statuses, please refer to viewing status of a metadata refresh.
Please note, currently there's no webhook or push notification that can notify you about the status of your request, hence, it's up to the developers to keep polling for any updates. The expected maximum time (estimate) to complete a refresh is approximately 48 hours. Updated metadata values will only be reflected in downstream systems e.g. marketplaces once the refresh for that asset is complete.
Checking the status of failed requests
If your requests fails for some reason, you should see the following response:
To check why your requests failed, you can use the following snippet:
METADATA REFRESH ERRORS
For more information regarding metadata refresh errors and various error codes, please refer to viewing metadata refresh errors.
Viewing all your current and previous refreshes
Migration guide
Migrate from XpansionChain JavaScript SDK
The Core TypeScript SDK was released recently to make integrating with XpansionChain more straightforward and intuitive. We listened to a lot of your feedback, spent much time iterating on the interface of the Core SDK and have landed on a new interface.
Our old imx-sdk-js is bloated, unintuitive and hard to grok. It’s worth investing the time to migrate to the new Core SDK.
Why should you migrate?
The new Core SDK is vastly improved when working with the XpansionChain API. It's easy to understand and use. The new Core SDK’s improvement statistics speak for themselves:
Hand-written code reduced from 10,589 lines to 2,365 lines (down 78% 📉)
Package size reduced from 1.9MB to 668kB (down 65% 📉)
Minified package size reduced from 575kB to 195kB (down 67% 📉)
External dependencies reduced from 42 to 9 (down 79% 📉)
The number of publicly-exposed functions was reduced from hundreds to just 45 via one XpansionChain class, making it easy to understand all the functionality on offer.
The confusing
fp-ts
library has been completely eradicated.
When should you migrate?
You should consider migrating to the Core SDK if you use the imx-sdk from the backend to:
Create projects
Create and manage collections
Manage metadata schema
Mint assets
Create and manage orders including bids
Execute trades
The Core SDK supports newer features like metadata refresh. If you’d like to trigger asset metadata refreshes using the SDK, you should consider migrating to the Core SDK.
What has changed in Core SDK v2.0.0
Changed:
Order creation and management targets upgraded v3 endpoints.
createOrder
->createOrderV3
cancelOrder
->cancelOrderV3
getSignableCancelOrder
->getSignableCancelOrderV3
getOrder
->getOrderV3
listOrders
->listOrdersV3
Trade creation and management targets upgraded v3 endpoints.
createTrade
->createTradeV3
listTrades
->listTradesV3
Migration path from imx-sdk to Core SDK v2.0.0 for use in the backend
Retrieve and persist your stark key using the generate-stark-key tool. Add an entry for your stark key in your secrets manager or .env file like the one below.
Remove imx-sdk from the list of dependencies
Add @imtbl/core-sdk as a dependency.
Call the method of choice. Check out the examples to see how to call individual endpoints.
Migration path from Core SDK v1.0.0 to Core SDK v2.0.0
Retrieve and persist your stark key using the generate-stark-key tool. Add an entry for your stark key in your secrets manager or .env file like the one below.
Remove imx-core-sdk v1.0.0 from the list of dependencies
Add @imtbl/core-sdk v2.0.0 as a dependency.
Call the method of choice. Check out the examples to see how to call individual endpoints.
Further documentation
See the Developer homepage for general information on building on XpansionChain.
Build on XpansionChain zkEVM:
Documentation
API reference
Support
Build on XpansionChain:
Documentation
API reference
Support
Last updated