MoneriumPack
Monerium Pack enables using Safe with Monerium (opens in a new tab), a regulated platform that facilitates the use of e-money tokens on the blockchain.
Install dependencies
To use the MoneriumPack
, you need to install the Monerium SDK in addition to the @safe-global/onramp-kit
package.
_10yarn add @safe-global/onramp-kit @safe-global/protocol-kit @monerium/sdk
Reference
The MoneriumPack
class enables the use of Monerium services with Safe. To use it, create an instance of the pack and pass it to the SafeOnrampKit
init
method.
This pack allows you to "Login with Monerium" by creating a connection between your Safe address and your Monerium account. This pack starts an authentication flow that uses the Monerium SDK to gain access to your account.
_10const moneriumPack = new MoneriumPack({_10 clientId: 'YOUR_CLIENT_ID',_10 redirectUrl: 'URL_AFTER_AUTHENTICATION'_10 environment: 'sandbox'_10})_10await moneriumPack.init(moneriumInitOptions)
new MoneriumPack(moneriumConfig)
Parameters
moneriumConfig
- The configuration for the Monerium pack. The options are:
_10MoneriumProviderConfig {_10 clientId: string_10 redirectUrl: string_10 environment: 'production' | 'sandbox'_10}
The clientId
is the secret representing the "Authorization Code Flow" for your Monerium account. To get your clientId
:
- Log in to your account (opens in a new tab).
- Create a new application (opens in a new tab).
The redirectUrl
is an URI that will be used to send the user back to the app after they complete the authentication process.
The environment
is the environment for the Monerium SDK. You can choose between production
and sandbox
.
The production
environment will use the production Monerium services and the accounts will need to go through a KYC process. Real money will be transferred. The sandbox environment will use the Monerium sandbox services (opens in a new tab) and no KYC is required. Fake money will be used.
Caveats
You should always call the init()
method afterwards before interacting with the pack.
init(moneriumInitOptions)
The init
method initializes the Monerium SDK and the Safe services by creating a new instance of the SafeMoneriumClient
(opens in a new tab) class. This class extends the MoneriumClient
(opens in a new tab) class from the Monerium SDK and adds extra features to use it with the Safe services.
Parameters
The MoneriumInitOptions
options to be passed to the init
method are:
_10MoneriumInitOptions {_10 protocolKit: Safe_10}
protocolKit
- To use theMoneriumPack
, you need to add Protocol Kit as a dependency for your project and create an instance of theSafe
(opens in a new tab) class.
open(moneriumOpenOptions)
The open()
method initiates the authentication process with Monerium. It opens a popup window with the Monerium authentication page.
Parameters
The MoneriumOpenOptions
options to be passed to the open
method are:
_10MoneriumOpenOptions {_10 initiateAuthFlow?: boolean_10}
initiateAuthFlow
- This flag should be added the first timeopen()
is called to prompt the user to the authorization flow. Once authenticated, the dApp can call again theopen()
method to get theMoneriumClient
.
Take a look to the example (opens in a new tab) for more information.
Returns
A SafeMoneriumClient
instance. This instance contains all methods and properties from the Monerium SDK, plus some extra ones for using the Safe services. You can use them in your application to create any flow you need.
For more information about the available methods, refer to the Monerium SDK documentation (opens in a new tab).
The Monerium SDK will be enhanced with Safe-related methods, mainly used in the MoneriumPack
so you won't need to call them directly. The exception will be the send()
method, which is used to place the orders in the Monerium system.
The send(safeMoneriumOrder)
you can access as a property in the SafeMoneriumClient
instance method takes an order to be placed:
_10SafeMoneriumOrder {_10 safeAddress: string_10 amount: string_10 currency: Currency_10 counterpart: Counterpart_10 memo: string_10}
And it will do the following:
- Creates a
redeem
order with the correct format for the Monerium SDK to understand - Place the order to Monerium
- Propose a
signMessage
transaction to the Safe services with the required Monerium message to be signed and executed. Monerium systems are aware that the Safe is a multisig wallet and will wait for this order to be confirmed and executed before carrying out the order in their systems.
Caveats
- The order we use internally in the SDK for evaluating the
redirectUrl
,authCode
andrefreshToken
is important. Each property opens a different flow with Monerium and we evaluate the presence of theauthCode
, then therefreshToken
andredirectUrl
as default. Have this in mind if you use all of them together in your app
subscribe(event, handler)
You can subscribe to order status changes (opens in a new tab) through the Monerium API.
Parameters
event
- The event you want to subscribe to. You can choose between one of the following:
_10MoneriumEvent {_10 placed = 'placed',_10 pending = 'pending',_10 processed = 'processed',_10 rejected = 'rejected'_10}
handler
- The handler function that will be called when the event is triggered.
unsubscribe(event, handler)
Allow to unsubscribe to authentication state changes.
Parameters
event
- The event you want to unsubscribe to.handler
- The handler function that will be called when the event is triggered.
close()
The close
method will clean up the socket, subscriptions and browser storage.
Usage
Instantiate the class and call the init
method when the page or component are loaded, followed by the open(options)
method when you want to start the interaction.
The open
method starts the interaction with the pack and returns the Monerium SDK client enhanced with Safe specific methods.
_14// Instantiate and initialize the pack_14const moneriumPack = new MoneriumPack(moneriumConfig)_14moneriumPack.init({ protocolKit })_14_14// Open_14const safeMoneriumClient = await moneriumPack.open(moneriumPackOpenOptions)_14_14// Subscribe to events_14const handler = (event) => {}_14moneriumPack.subscribe(MoneriumEvent.placed, handler)_14moneriumPack.unsubscribe(MoneriumEvent.processed, handler)_14_14// Close_14await moneriumPack.close()