Next JS / React
Initialise a new Next JS application with Typescript and Tailwind CSS. You can read more on how to do that on the official Next JS website.
Or
Clone the starter template from github below:
Install all the required dependencies using:
This will install all the necessary packages including oneramp.
Or you can manually install it using:
Now you can run the project using:
Initializing the package
Let's first set up the essentials. Import the necessary modules, including oneramp and various libraries. The useConnect
hook is imported from a custom file called 'useConnect,' which handles wallet connections.
Initialize several state variables using useState
to keep track of loading indicators, error messages, blockchain explorer links, and user input, including the amount and phone number.
Additionally, declare two constants, clientPub
and secretKey
, which should be replaced with your OneRamp client credentials.
When a user clicks the "Off Ramp" button, the handleSubmit
function is triggered. It sets the loading state to indicate that an action is in progress.
Next, it checks if the user has entered values for both the amount and phone number fields. If either field is empty, an alert is displayed, informing the user to fill in all the required fields, and the function exits.
This section of handleSubmit
, we connect to the user's Ethereum wallet, access the provider from the user's browser window and create an Ethereum provider using ethers.
After that, the code sends a request to the user's wallet to fetch their accounts, and it obtains a signer.
With these components in place, we create an instance of the OneRamp
class, specifying the 'Mumbai' network, clientPub
, secretKey
, the provider, and the signer.
In the final part, the code initiates an off-ramp transaction using the offramp
method provided by the OneRamp
instance. If the transaction is successful, the UI is updated with the transaction details, and the input fields are cleared.
However, if there's an error, it is logged, the loading state is updated, and an error message is displayed.
Last updated