Building a Relaying Transaction Server
Learn how to build a relaying transaction server with Sequence, enabling your server to dispatch transactions on behalf of users seamlessly.
With Sequence, you can create a smart contract wallet your server can use to dispatch transactions for your users without you having to be worried about transaction speed, throughput and re-orgs.
The only difference from using a typical Sequence wallet when sending transactions to the blockchain, is at the smart contract level the msg.sender
is one of the Sequence Relayers wallet addresses. For Sequence Builder standard contracts, this is not a problem when combined with a relayed Transactions API request.
By default, Sequence transactions will be executed sequentially.
The following steps will guide you through how to create you server and mint collectibles to a wallet address:
- Environment Setup with Express Server: Create a NodeJs based server using the library Express to accept HTTP requests
- Project & Access Key Management: Claim a public access key to interact with the Sequence stack
- Deploy Collectible Contract: Deploy a collectible contract in order to be able to submit transactions to the blockchain to mint tokens to a wallet address
- Construct Sponsored Relayer with Transactions API: Craft a function to be used in an Express route to call the Sequence Transactions API from a sponsored contract
Additonal Features:
- (Optional) Relay with Wallet Owned Currency: Craft a function to be used in an Express route to call the Sequence Transactions API paid for using a wallet owned currency
- (Optional) Relay Parallel Transactions: Perform transaction batching to send a currency
Environment Setup with Express Server
Ensure that pnpm
(or some other node package manager) is installed with the following command:
Then, clone down the following express template code
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications, which will be used in this guide.
After the code is locally on your machine, run your server and client with the following command:
Contained in the code is a route called /mint
that can be called from the cli for testing.
Give a try with this example curl request:
You should see the following output:
Project & Access Key Management
First start by following this walkthrough for how to sign up to the Sequence Builder and to learn how to create a project.
Then, in order to use the Transactions API, you’ll need to upgrade your Billing to Developer
which can be with this walkthrough.
Finally, a Public Access Key
is required for the Transactions API, which can be acquired following this walkthough.
Finally update the update the .env.example
to .env
with the following:
Deploy Collectible Contract
Follow this walkthrough to deploy a collectible contract.
If you’re using a non-testnet and need to sponsor your contract, you can perform this action by following this walkthrough
Finally, update the .env
with your deployed collectible contract:
Construct Sponsored Relayer with the Transactions API
The full code for this section can be found here
First, using the template code provided in step #1, we’ll need to add a few packages
Then, your server will need an EOA wallet that will be able to sign messages. It will be the owner of your server-side Sequence wallet which will be used to dispatch transactions.
Openning a session may trigger a migration of your Sequence wallet to a new version, this could be v1
to v2
or v2
to future versions.
Migration is a one-way process, once your wallet is migrated it cannot be reverted to a previous version.
To catch any unwanted migration, you can use the onMigration
callback.
To implement the callContract
function, include the following code that uses a single signer to relay transactions:
Finally, update the .env
with a private key for a wallet that can be generated from the following app which is used for demo purposes. For production, we recommend to generate private keys securely locally on your computer via this example script.
Then, update the PKEY
variable with the key:
Grant Minter Role to Relayer Wallet Address
One must update the role access of the contract in the Builder to only receive requests from the minter wallet address.
You can do this in Sequence Builder by providing minter permission
to your Sequence Wallet Transactions API Address
.
To do so, open your project, navigate to the Contracts
page, select your Linked contracts
and under Write Contract
tab expand the grantRole
method.
Complete with the following details:
bytes32 role
: 0x9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a6
address account
: <Generated Sequence Transactions API Wallet Address>
Where the role
string inputted is the result of keccak256("MINTER_ROLE")
in solidity or ethers.solidityPackedKeccak256(ethers.toUtf8Bytes("MINTER_ROLE"))
in javascript
This makes it so that only your specific address can mint from the contract, it will error otherwise.
Complete the role update by clicking write
and sign the sponsored transaction.
You application is now ready for you to send a test transaction from the client frontend by signing into your wallet and clicking mint.
Give it a try!
Don’t forget to update the access key in the client in the initWallet
function
(Optional) Relay with Wallet Owned Currency
You can also enforce a specific way to pay for gas fees:
(Optional) Relay Parallel Transactions
If you want to send multiple independent transactions without needing to batch them, you can also send them in distinct nonce spaces.
Using distinct nonce spaces for your transactions signals to the transactions API that there’s no dependency between them and that they can be executed on-chain in any order.
This allows the transactions to be dispatched immediately in an unbuffered way without having to wait for a full batch.
Here is an example of how to do that:
Was this page helpful?