/ README.md
README.md
  1  # Tyle.Nft Server example
  2  
  3  This is an example for [Tyle.Nft](https://www.nuget.org/packages/Tyle.Nft/) NuGet package .Net Core.
  4  
  5  ## Summary
  6  
  7  Tyle.Nft is simply a library to work with nft and real-time database.
  8  In this example I built a grpc service to communicate with the client and rotate a cube.
  9  The rotation is stored in a nft , the token uri redirects to the api interfacing with rethinkdb.
 10  
 11  ## Run
 12  Replace with your db host in the controller and the service.
 13  You can publish to docker hub from visual studio then run :
 14  ```bash
 15  docker run -p 5001:5001 -p 5000:5000 MyService
 16  ```
 17  
 18  ## Usage
 19  
 20  ### Start Tyle.Nft
 21  First install the NuGet package.
 22  Then create a client to Tyle and connect to the db (you have to have rethinkdb up and running):  
 23  ```c#
 24  var Tyle = new Client();
 25  Tyle.DbConnect("localhost");
 26  ```
 27  Now you can start Web3 :
 28  ```c#
 29  Tyle.StartWeb3("Your api key (i.e. infuria)", "your wallet secret");
 30  ```
 31  ### Deploy the contract
 32  If you do not have a contract you can deploy it :
 33  ```c#
 34  var address = Tyle.DeployContract().Result;
 35  ```
 36  ### Mint the nft
 37  Now that you have a smart contract you can mint all the nft you want , like this :
 38  ```c#
 39  var modelUri = Tyle.ss.StoreModel("c:/Users/Pc/Desktop/cube.fbx");
 40  var token = new nft_token()
 41          {
 42              name = "Cube",
 43              description = "An awesome cube!",
 44              volume = 123.456f,
 45              model = modelUri,
 46              rotation = new nft_token.quaternion { x = 0, y = 0, z = 0, w = 0 }
 47          };
 48  
 49  Tyle.Mint(address, token,"https://tylegroup.com/api/Token1").Wait(); // Will save data off-chain
 50  ```
 51  #### Web api controller
 52  In this case I store also the model of the cube in ipfs (you have to run it locally)
 53  The apihost of the Mint function is the url to the api , you can make the controller 
 54  in your web api like this :
 55  ```c#
 56  using Tyle.Nft.Api;
 57  namespace MyService
 58  {
 59      [ApiController]
 60      [Route("api/[controller]")]
 61      public class Token1Controller : TokenController
 62      {
 63          public Token1Controller():base("YOUR DB HOST")
 64          {
 65  
 66          }
 67  
 68      }
 69  }
 70  ```
 71  You can retrieve your token object and also tokenId like this :
 72  ```c#
 73  Dictionary<int, nft_token> d = Tyle.GetTokenList<nft_token>(address).Result;
 74  int tokenid = d.FirstOrDefault(token => token.Value.name == "Cube").Key;
 75  ```
 76  
 77  So after built your protobuffer you can rotate the cube with a service like this : 
 78  ```c#
 79  public override Task<RotateReply> Rotate(RotateRequest request, ServerCallContext context)
 80          {
 81              
 82              var client = new Client();
 83              client.DbConnect("YOUR DB HOST");
 84              var token = client.db.GetToken(request.Address, request.Tokenid).o;
 85              if (token.rotation.y >= 360)
 86                  token.rotation.y = 0;
 87              else
 88                  token.rotation.y += 5;
 89              client.db.UpdateToken(request.Address, request.Tokenid, token);
 90  
 91              return Task.FromResult(new RotateReply
 92              {
 93                  Y = token.rotation.y
 94              });
 95          }
 96  ```
 97  
 98  At this point you can have your grpc service up and running and call all the procedures remotely , like rotating the cube for example.
 99  If you don't want to have real-time data you can omit the db and use the Mint method without passing the apiurl.
100  
101  Also if you want to serialize/deserialize data you can call ToByteArray and FromByteArray from Tyle.Nft.Byte (Note that not all the objects are serializable).