Kotlin

Core SDK - Kotlin

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

  • Standard API Requests

  • SDK Functions

  • Wallet connection

  • Further documentation


📚SDK LINKS

  • SDK reference

  • Github repository

Installation

  1. Add Maven Central to your repositories

repositories {
    mavenCentral()
}
  1. Add dependency to your app build.gradle file

dependencies {
    implementation 'com.XpansionChain.sdk:imx-core-sdk-kotlin-jvm:$version'
}
  1. Set the correct environment (defaults to Production)

val XpansionChain = XpansionChain(XpansionChainBase.Sandbox)

Standard API Requests

The Core SDK includes classes that interact with the XpansionChain APIs.

e.g. Get a list of collections ordered by name in ascending order

val response = XpansionChain.listCollections(
        pageSize = 20,
        orderBy = "name",
        direction = "asc"
)

OR

val response = XpansionChain.collectionsApi.listCollections(
    pageSize = 20,
    orderBy = "name",
    direction = "asc"
)

View the OpenAPI spec for a full list of API requests available in the Core SDK.

SDK Functions

Workflows

These are utility functions that will chain necessary API calls to complete a process or perform a transaction, including:

  • Register a user with XpansionChain

  • Buy cryptocurrency via MoonPay

  • Buy ERC721

  • Sell ERC721

  • Cancel listing

  • Transfer ERC20/ERC721/ETH

  • Deposit ERC20/ERC721/ETH

  • Withdraw ERC20/ERC721/ETH

  • Check if user is registered on chain

Wallet connection

In order to call authorised API and use workflow functions, you will need to pass in the connected wallet provider. This means you will need to implement your own Wallet L1 Signer and L2 StarkSigner.

L1 Signer

Example implementation of the L1 Signer using web3j:

class L1Signer(private val credentials: Credentials) : Signer {

    val web3j = Web3j.build(HttpService(NODE_URL))

    override fun getAddress(): CompletableFuture<String> {
        return CompletableFuture.completedFuture(credentials.address)
    }

    override fun signMessage(message: String): CompletableFuture<String> {
        val signatureData = Sign.signPrefixedMessage(message.toByteArray(), credentials.ecKeyPair)
        val retval = ByteArray(65)
        System.arraycopy(signatureData.r, 0, retval, 0, 32)
        System.arraycopy(signatureData.s, 0, retval, 32, 32)
        System.arraycopy(signatureData.v, 0, retval, 64, 1)
        val signed = Numeric.toHexString(retval)
        return CompletableFuture.completedFuture(signed)
    }

    @Suppress("TooGenericExceptionCaught")
    override fun sendTransaction(rawTransaction: RawTransaction): CompletableFuture<String> {
        val signedTransaction = TransactionEncoder.signMessage(rawTransaction, credentials)
        return web3j.ethSendRawTransaction(Numeric.toHexString(signedTransaction)).sendAsync()
            .thenApply { it.transactionHash }
    }
}

L2 StarkSigner

Use StarkKey.generateStarkPrivateKey() to create an instance of StandardStarkSigner, an implementation of StarkSigner.

🚨🚨🚨 Warning 🚨🚨🚨

You will have to persist the Stark private key. The key is randomly generated so cannot be deterministically re-generated.

val starkPrivateKey = StarkKey.generateStarkPrivateKey()
val starkSigner = StandardStarkSigner(starkPrivateKey)

Further documentation

  • See the Developer homepage for general information on building on XpansionChain.

  • Build on XpansionChain zkEVM:

    • Documentation

    • API reference

    • Support

  • Build onXpansionChain:

    • Documentation

    • API reference

    • Support

Last updated