# LYNC Unity Aptos SDK

LYNC Unity Aptos SDK is a no-code Modular Unity SDK supporting PC (MacOS and Windows) and Mobile (Android and iOS) on Aptos.&#x20;

**Platform Supported:** PC (Windows and MacOs) and Mobile (Android and iOS)\
**Network Supported:** Aptos Testnet and Mainnet

This release includes the following:

* Social Logins
* Keyless Login
* Pontem Wallet Login
* Custom Transactions in Social Login and Web3 Wallet like Pontem
* Paymaster inbuilt to sponsor transactions for your users.

### Get your API Key <a href="#get-your-api-key" id="get-your-api-key"></a>

Please get your API key before downloading the SDK from [**here**](https://www.lync.world/form.html)

### Installation <a href="#installation" id="installation"></a>

Download the LYNC Unity Aptos SDK from [Here​](https://github.com/LYNC-WORLD/LYNC-Unity-Aptos-SDK/releases)

Example Project: [https://github.com/LYNC-WORLD/LYNC-Unity-Aptos-SDK](https://github.com/LYNC-WORLD/LYNC-Unity-Aptos-SDK)​

Import the SDK .unitypackage file to your project. or simply drag and drop .unitypackage file to your project.

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FH2fupyxtcx6FWFbbojVd%2Fimage.png?alt=media&#x26;token=6ce98174-6f19-42e4-bf1e-97bc411a3722" alt="" width="561"><figcaption><p>Assets -> Import Package -> Custom Package</p></figcaption></figure>

Once the LYNC Aptos SDK package has finished importing into your Unity project, you can begin integrating it into your game.

The Folder structure looks like this

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FlRMmqCwxSny3c4K1wz8M%2Fimage.png?alt=media&#x26;token=9213aaf1-5728-4555-a90a-c49ca5bfc268" alt="" width="390"><figcaption><p>SDK Folder Structure</p></figcaption></figure>

### Integrating LYNC Aptos SDK in Unity

There is 1 Example Projects present in the SDK:&#x20;

Assets/LYNC-APTOS-SDK/Example/APTOSExample.unity

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FkFoqMSrw1YHugLYOVsI4%2Fimage.png?alt=media&#x26;token=a5e51e87-80e3-4e51-8f12-8a97749abb3c" alt="" width="503"><figcaption><p>Example Projects</p></figcaption></figure>

You can find the example scene in the folders. Simply pass the API key in LyncManager GameObject.

To test, Build and Run after adding this scene in (Scene in Build).

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FTFrUA0LsQTBhdyz4zDWc%2Fimage.png?alt=media&#x26;token=5345f33d-3c25-4178-9b8f-e9ea6500252d" alt="" width="563"><figcaption><p>Pass LYNC API Key​</p></figcaption></figure>

## **Setup the Project**

To use LYNC Aptos SDK. Attach LYNC Manager Prefab(Assets/LYNC-APTOS-SDK/LYNC Manager.prefab), on the starting scene.\
This will serve as the starting point for your project. In LYNC Manager Prefab, be sure to provide the following details:

1. LYNC API Key ([The API Key can be generated from here](https://lync.world/form.html))
2. Choose Network -> Testnet / Mainnet
3. Sponsor Transactions -> If you want to sponsor transactions for users (Please contact [LYNC](https://calendly.com/shanu-lync/30-minute-meeting) to setup Paymaster)
4.  Login Options -> Allowing users to choose which login method to login from\


    <figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FkydSdfu0r6hnFbiIx5lR%2Fspaces_utQhQ1f3Hy2b3URu0DVp_uploads_rySZfqFuINHgJN9tuqyE_image%20copy.png?alt=media&#x26;token=b01ac38b-2e80-4df5-8d0c-d7f3e8b0871d" alt=""><figcaption></figcaption></figure>
5. Pass a deep link name (example: lyncaptos/gameName etc.)

### Integrating Login or Transaction Layer via LYNC Aptos SDK in Unity

The Sample Code for Login can be found at APTOSExample.cs.

> **Make sure to Import LYNC.**

**Example (Event Trigger):**

LYNC ready Should be a function which has an argument of type "LyncManager"

```csharp
LyncManager.onLyncReady += LyncReady;

private void LyncReady(LyncManager Lync)
    {
        // Once LYNC is ready, you can do any steps like Login, Logout, Transactions etc.
        
        //To Login:
        Lync.WalletAuth.ConnectWallet((wallet) =>
        {
            OnWalletConnected(wallet);
        });
        
        //To Logout:
        Lync.WalletAuth.Logout();
        
    }
```

**To Check if the user is logged in or not:**

```csharp
using LYNC;

private AuthBase authBase;

    authBase = await AuthBase.LoadSavedAuth();
        if (authBase.WalletConnected)
        {
            // User is Already Login
            OnWalletConnected(authBase);
        }
        else{
             // Ask user to login
        }
```

On Wallet Connected (TypeOfLoginMethod)

```csharp
//To OnWalletConnected(TypeOfLoginMethod):
        
        private void OnWalletConnected(AuthBase _authBase)
        {
            if (AuthBase.AuthType == AUTH_TYPE.FIREBASE)
            {
                Populate(_authBase as FirebaseAuth);
            }
    
            if (AuthBase.AuthType == AUTH_TYPE.PONTEM)
            {
                WalletAddressText.text = AbbreviateWalletAddressHex(_authBase.PublicAddress);
                StartCoroutine(API.CoroutineGetBalance(_authBase.PublicAddress, res =>
                {
                    balance.text = res.ToString();
                    Debug.Log("BALANCE"+balance);
                }, err =>
                {
                    Debug.Log("Error");
                }));
            }
        }
        

        public void Populate(FirebaseAuth firebaseAuth = null)
        {
            WalletAddressText.text = (firebaseAuth == null ? "Disconnected" : AbbreviateWalletAddressHex(firebaseAuth.AptosFirebaseAuthData.publicKey));
            balance.text = (firebaseAuth == null ? "0" : firebaseAuth.AptosFirebaseAuthData.balance) + " APT";
        }
```

**To Logout directly:**

```csharp
LyncManager.Instance.WalletAuth.Logout();
```

## Transaction Flow&#x20;

There are two methods for proceeding with a transaction:

1. **Gasless Transaction -** Game Developer/ Game Studio will be sponsoring the transaction fee.
2. **User Paid Transaction-** The Gamer/ User will be responsible for paying the gas fee and cost if any, required to do the transaction.

### Gasless Transaction

To Enable Gasless Transactions or to Sponsor Transactions for your users, \
Set Sponsor Transactions as true, and [contact LYNC team to setup your paymaster](https://calendly.com/shanu-lync/30-minute-meeting)

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FuVbs355gSlX1ErNgRn6v%2Fimage.png?alt=media&#x26;token=62563937-b71d-40bd-970a-3296951dd01a" alt="" width="563"><figcaption><p>Sponsor Transactions</p></figcaption></figure>

To do transactions, APTOSExample.cs can be taken as a reference.

Pass in the Contract Address, Contract Name, Function Name and Network.

Arguments are not compulsory parameters, but if the function accepts any argument, make sure to pass them.

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2Fno21EFjiBDZKuzpf3gRJ%2Fimage.png?alt=media&#x26;token=6b502e01-6595-4134-910c-1b59a54513f4" alt=""><figcaption><p>Transaction Object</p></figcaption></figure>

```csharp
LyncManager.Instance.TransactionsManager.SendTransaction(Transaction);
```

You can create a public Transaction Object, pass in the parameters and hit the function call where you want to do the transactions

```csharp
public Transaction mintTxn;

TransactionResult txData = await LyncManager.Instance.TransactionsManager.SendTransaction(mintTxn);

if (txData.success)
    SuccessfulTransaction(txData.hash, "MINT");
else
    ErrorTransaction(txData.error);

```

Or You can create a Transaction Object,&#x20;

```csharp
public Transaction mintTxn;

//LyncManager.Instance.TransactionsManager.SendTransaction( new Transaction(ContractAddress, ContractName, FunctionName,ListOfArguments));

TransactionResult txData = await LyncManager.Instance.TransactionsManager.SendTransaction(
new Transaction("0x55db3f109405348dd4ce271dc92a39a6e1cbc3d78cf71f6bf128b1c8a9dfac33","tst_unity","set_data_bytes",arguments));

if (txData.success)
    SuccessfulTransaction(txData.hash, "MINT");
else
    ErrorTransaction(txData.error);

```

List of Arguments:&#x20;

```csharp
//List<TransactionArgument>{
//    new TransactionArgument{ argument = value, type = ARGUMENT_TYPE.STRING }
//};

List<TransactionArgument> arguments = new List<TransactionArgument>{
    new TransactionArgument{ argument = "0xb66b180422a4886dac85b8f68cc42ec1c6bafc824e196d437fdfd176192c25fccfc10e47777699420eec0c54a0176861a353a43dd45b338385e1b975709f2000", type = ARGUMENT_TYPE.STRING }
};
```

### Some common bugs and their resolutions

**Problem:** Newtonsoft JSON is missing.

**Solution**: Please, Add this as a git URL in adding package

```
com.unity.nuget.newtonsoft-json
```

<figure><img src="https://3890225134-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FutQhQ1f3Hy2b3URu0DVp%2Fuploads%2FMqsMNwxFzsk5sIlBlF9N%2Fimage.png?alt=media&#x26;token=38886f78-11bf-410c-9a26-40f860c7e88d" alt=""><figcaption><p>Add newtonsoft Json</p></figcaption></figure>
