# Solana.jslib

This script is essentially the front man for interacting with the Solana Web3 API / RPC. It's functions are called from `CryptoReceiver.cs` apart from the Solana Transaction which can be called from any script.

#### GetSolanaWebJS()

This is called from the `Awake` function in `CryptoReceiver.cs` and it loads the Solana Web3 JS package into the page. This also links to 2 more functions to load the **@solana/spl-token** and **@metaplex/js** libraries.

#### SolanaLogin()

This function takes the `service` value from the `OnConnect` in `CryptoReceiver` and uses it to dictate which Wallet Extension it will attempt to connect with. It will pass back the wallet address to `CryptoReceiver` in a callback and also pass it to the `SolanaTokens` function.

#### SolanaTokens()

This function interacts with the Solana JSON RPC to get all the Token Accounts owned by your Wallet. It will then send all of the SPL Tokens to the `CryptoReceiver`.

#### UseMetaplex()

This function has replaced the previous `Vlawmz()` function. It's a more simple way of grabbing a list of owned NFT metadata using the @Mataplex/js library.

#### SendSolTokens()

This is the final function the the chain to grab the Metadata of NFTs. It fetches a JSON response from the Metadata URLs and then sends selective parts of the response for each one back to the `CryptoReceiver` script.

#### SendSolTransaction(destination, amount, callback)

This function can be called from any script and you can pass in a Destination address and amount to send. There is a demonstration of this in the `TransactionUIHandler.cs` Demo script. You will need to add this where you add variables at the start of the script.

```
[DllImport("__Internal")]
    private static extern string SendSolTransaction(string destination, Double amount, Action<bool> callback);
```

Then if you want to call it you can by passing in the destination wallet and also the amount as a `Double` value along with the callback function as the arguments like below. For an example amount, `0.1` would be 0.1 SOL.

```
SendSolTransaction("WalletAddressHere", 0.1, OnTransactionCallback);
```

Then you need a function to handle the callback, in the demo there is one provided which displays a popup and shows whether the transaction was successful or not.

```
[MonoPInvokeCallback (typeof (Action<bool>))]
public static void OnTransactionCallback(bool result)
{
    Debug.Log(result);
    gallery.transactionPopup.SetActive(true);
    if (result) {
        gallery.transactionResult.text = ("Success");
    }
    else {
        gallery.transactionResult.text = ("Failed");
    }
}
```

#### SendSPLTransaction(destination, amount, mint, decimals, callback)

This function can be called from any script and you can pass in a Destination address, amount, token mint address and the decimals of the token to send. There is a demonstration of this in the `TransactionUIHandler.cs` Demo script. You will need to add this where you add variables at the start of the script.

```
[DllImport("__Internal")]
    private static extern string SendSPLTransaction(string destination, Double amount, string mint, int decimals, Action<bool> callback);
```

Then if you want to call it you can by passing in the destination wallet, amount as a Double value , mint address for the token and its decimals along with the callback function as the arguments like below.

```
/SendSPLTransaction(destination.text, doubleAmount, mint, decimals, OnTransactionCallback);
```

Then you need a function to handle the callback, in the demo there is one provided which displays a popup and shows whether the transaction was successful or not. You can use the same Callback function for SOL and SPL Transactions.

```
[MonoPInvokeCallback (typeof (Action<bool>))]
public static void OnTransactionCallback(bool result)
{
    Debug.Log(result);
    gallery.transactionPopup.SetActive(true);
    if (result) {
        gallery.transactionResult.text = ("Success");
    }
    else {
        gallery.transactionResult.text = ("Failed");
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://puppy-gaming.gitbook.io/simple-crypto-webgl-templates/workflow-and-references/solana.jslib.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
