What is the best approach to implement Nami Wallet. Can some provide a boiler plate or any kind of documentation.?
3 Answers
Here is an open-source starter project: https://github.com/cheffNFT/namiwallettemplate
There is a demo in the Plutus GitHub repository since a few days: https://github.com/input-output-hk/plutus-apps/tree/main/plutus-pab/demo/pab-nami
Basically, you have the following parts;
- The PAB instance,
- the webserver backend,
- the webserver frontend.
I think, you should not expose the PAB through a public entry point. Instead have it running on local host. The PAB exposes post-, get-, and put-request that your webserver backend can use.
Your webserver backend should have a basic utility method to query the PAB consisting of 4 steps (all http request to the PAB using its standard http-endpoints):
- Activate a contract instance (this returns an instance-id that you need in steps 2 to 4).
- Post a request to the instance to execute your Plutus contracts.
- Wait 2 seconds and query the status of the instance. This status contains the transaction that you need to pass to the Nami wallet later on in your JavaScript code.
- Close the endpoint (or leave it open If you want to cache it).
You can use this utility function to construct your service functions (eg. buyTokens(amount, wallet_address)
)
In your frontend, you have JavaScript code that interacts with the Nami wallet. This code does the following steps:
- Connects to the Nami wallet extension and fetches the wallet address.
- Passes the wallet address (and needed arguments) to the service function of the webserver backend.
- Receives the partial transaction from the webserver backend and passes it to Nami to balance, sign, and submit it.
The only current complication is that in the last step 3, the balancing of the partial transaction cannot be performed by Nami because it does not yet expose a corresponding function. This will certainly change soon. For the moment a workaround is needed. One is shown in the Plutus PAB demo.