Bring Your Own Onramp
Introduction
If you want to embed any onramp provider in the widget, we’ve got your back! In this example we’ll integrate the Coinbase onramp but the principle is exactly the same for any other.
Pre-requisites
We assume you already have Dynamic integrated into a React based app, and are using the Dynamic Widget. The plan is to override the existing Buy button in the user profile page of the widget, so make sure you have onramps enabled in your Dynamic dashboard for that to show up.
Full Demo
You can find the full example app of this implementation code here, and the deployment at https://custom-onramp-example.vercel.app/.
Implementation
Install dependancies
The only other library we will need is the Coinbase Pay javascript SDK:
Scaffold our override file
Create a new file in your codebase called customOnramp.js
. In it let’s add the imports and declarations needed to get started:
For the following sections, unless otherwise told, place the code inside the now empty setupCoinbaseButtonOverride
function.
Setup the pay instance
Inside the setupCoinbaseButtonOverride, let’s set up a few things, including the CB pay instance:
Find the current Buy button
Add a new function (still inside setupCoinbaseButtonOverride) called findButtonInShadowDOM
which is responsible for detecting the button in the shadow dom:
Override the current button
Add another new function (still inside setupCoinbaseButtonOverride) called setupOverride
which is responsible replacing the existing button functionality with our own:
Poll for the button and onramp
Since we need both the button to be present and the onramp instance to exist for us to complete the override, we must poll for that state:
Return a cleanup function
We need to remember to tear things down again when we’re finished:
Use setupCoinbaseButtonOverride
You’ve now finished with the setupCoinbaseButtonOverride method, so let’s add it to one of our components. It doesn’t matter to much which one as long as it’s rendered at the same time as the Widget is. Note that it cannot be the same component you declare your DynamicContextProvider in, it must be inside the component tree.
Adding Dynamic & other imports
Here we’ll do it in the same component (Main.js as we have our DynamicWidget). Let’s do the relevant imports first, we’re going to need a couple of hooks from Dynamic, as well as our setupCoinbaseButtonOverride:
UseEffect, Dynamic hooks & widget
Next we’ll scaffold the Main component itself and create an empty useEffect which depends on the relevant Dynamic hooks:
Conditionals and calling our init
Everything from now on will be added inside the useEffect we just declared.
That’s it! Your Buy button now opens the coinbase onramp widget, while passing in all the relevant parameters it needs!
Was this page helpful?