Skip to main content

Writing Apps on SOCKET

Introduction​

In this guide, we’ll build a SuperToken application using the SOCKET Protocol.

You’ll learn how to:

  • Create a multi-chain application that deploys replicatble tokens across chains;
  • Use offchainVM to trigger onchain minting;
  • Test and Deploy your app across multiple chains.

Architecture Overview​

The System consists of 3 main components.

  • A Deployer Contract on offchainVM to deploy the SuperToken instances.
    • This contract which will be deployed to offchainVM;
  • An Application Gateway Contract on offchainVM that handles logic related to interacting with onchain contracts;
    • This contract which will be deployed to offchainVM;
    • AppGateway contract is the user hub of interactions;
  • An onchain ERC20 Token Contract that can be deployed on any chain.
    • This contract is expected to be deployed via the Deployer Contract;
    • AppGateway will be the owner and will trigger the mint and burn functions;

Key offchain Contract Concepts​

Onchain contract bytecode stored in the Deployer Contract​

The Deployer Contract has two key pieces of code to ensure that onchain deployments are replicable SuperToken's creationCode with constructor parameters is stored in a mapping. This stored code is used for deploying the token to the underlying chains and written in the constructor.

creationCodeWithArgs[superToken] = abi.encodePacked(
type(superToken).creationCode,
abi.encode(name_, symbol_, decimals_)
);

Using bytes32 variable is use a unique identifier for the SuperToken contract generated using the _createContractId function. This identifier allows us to fetch creationCode, onchain addresses and forwarder addresses from maps in AppGatewayBase. See here to know more about forwarder addresses.

bytes32 public superToken = _createContractId("superToken");

While this example handles a single contract, you can extend it to manage multiple contracts by storing their creation codes.

Onchain contract deployment with the Deployer Contract​

deployment flow

The deployContracts function takes a chainSlug as an argument that specifies the chain where the contract should be deployed.

function deployContracts(uint32 chainSlug) external async {
_deploy(superToken, chainSlug);
}

It calls the inherited _deploy function and uses the async modifier for interacting with underlying chains.

The initialize function is empty in this example. You can use it for setting chain-specific or dynamic variables after deployment if needed. For more details check this page.

What's next!​