# CryptoReceiver.cs

The CryptoReceiver script serves as a collection point for anything passed back through the solana.jslib functions. It stores a list of NFTs that can be accessed from any script by using `CryptoReceiver.CR.myNFTs` and is a list populated with `CryptoNFT` ScriptableObjects. It also holds a variable for an RPC Endpoint URL which can be changed from the `CryptoReceiver` Component in your Scene (by default set to Solana Public Endpoint for 'mainnet-beta).

## Functions

#### Awake()

On awake, the script checks that there are currently no other instances of it running, if there is, it will destroy itself. If there isn't, it will set to `DontDestroyOnLoad` then it will call the `GetSolanaWebJS()` function on the `solana.jslib` which will add the Solana Web3 JavaScript package to the web page so we can interact with it.

#### ReceiveNFTMints()

This was the original function of the asset for receiving all the NFT mint addresses. It receives them one by one from the `solana.jslib` and adds them to a List<> of strings. I kept this function in as some people may only need to check against the ownership of an NFT and not need to use the Metadata so could avoid the extra processing.

#### ReceiveNFT()

This function receives all the NFT Metadata that is sent from `solana.jslib`. It is all received in a single String so it uses `Split` with the separating character `¬` so we can use each part of it. The attributes also all come in a String which is then separated with `|` between each attribute and `~` between each trait/value. It then instantiated a new `CryptoNFT` ScriptableObject for the NFT containing the data received and adds it to the `myNFTs` List<>.

#### ReceiveSPLTokens()

This function receives all the wallets SPL Tokens and stores them in a List just like the NFTs with a SPLToken ScriptableObject which holds the mint address, amount as a `float` and the decimal amounts.

#### OnConnect(string service, string endpoint, OnConnected)

This is the function you need to call from to request the user to login with their Solana wallet. It accepts a string which is for the extension it will try to connect with. Currently you can pass `phantom` or `solflare` as the value. It also passes the RPC Endpoint URL to connect to. After a wallet is connected, it will call the OnConnected callback function which then sets the Wallet Address.

#### OnConnected()

This function receives the connected wallet's address after log in. It will saves it to the `walletAddress` variable and also create a more UI friendly address like `AaAaA.....BbBbB` and store it in the `shortAddress` variable. If the received address is `null`, its because either the user rejected/cancelled the login or the extension isn't installed and it's redirected them to the website to install it.


---

# 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/cryptoreceiver.cs.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.
