3. Staking
Overview
Staking is a core functionality of the Chorus One SDK, enabling users to lock their ETH to support network operations and earn rewards.
This section will cover how to integrate staking into your application by creating a form which submits the transaction.
The Form Component
We will start by creating a basic form which consist of an input field for the amount to be staked and a button to submit the transaction.
Below is an implementation of this form in React:
import React, { useState } from 'react';
import { useAccount, useBalance } from 'wagmi';
export const FormComponent = ({ onSubmit }) => {
const { address } = useAccount();
const { data: balance } = useBalance({
address,
});
const [inputValue, setInputValue] = useState('');
return (
<div>
<form onSubmit={(e) => {
e.preventDefault();
onSubmit(inputValue);
setInputValue('');
}}>
<input
type="text"
placeholder="ETH amount"
onChange={(e) => setInputValue(e.target.value)}
value={inputValue}
/>
<span>ETH</span>
<div>Available to stake: {balance?.formatted} ETH</div>
<button type="submit">Submit</button>
</form>
</div>
);
};This form component uses the useAccount and useBalance hooks from wagmi to manage user account details and balance information. The onSubmit prop is a function that handles the actual staking process, which you can define in your application logic.
This simple yet effective form provides the basic functionality to integrate staking into your Ethereum-based application.

Writing the Staking Transaction Function
Now, we will focus on the main operation of our application: submitting a staking transaction.
Below is a representative snippet of the function:
The Transaction object returned by buildStakeTransaction includes the following parameters:
to(Hex): The address of the contract to interact with(the vault address).data(Hex): A contract hashed method call with encoded arguments (the transformation of the method call into this encoded and hashed form is handled by theencodeFunctionDatamethod from the viem library).value(bigint): The amount of ETH being used in the transaction. In this case, it's the amount being staked.
Ensuring Correct Amount Format for Staking
The amountToStake parameter must be a string representing the amount of ETH to deposit. For example, '1' represents 1 ETH.
If you have the amount as a bigint, convert it to a string using the formatEther function from viem. Example:
This ensures the amountToStake parameter is in the correct format for the staking transaction function.
Next Steps
Having integrated the basic staking functionality into your application, you're now ready to expand its capabilities.
To continue exploring our application's functionality, you can proceed to the next section: Unstaking.
Last updated
Was this helpful?