Instantiating The Contract Object (Client-side)
The process is similar on the client-side, except it will be the fan that needs to provide the wallet instead of the client. Therefore, there are no private keys (Passport handles that) and you do not need to identify a provide (Passport does that as well). Again, sharing the client-side version of code construction you saw in the previous section, we would request the provide from Passport, and then the signer (a mix of the provider and the private key as generated securely by Passport) can be retrieved using getSigner. With that, the signer can be used to generate the contract object that will be called to access the contract functionality.
Calling Contracts
Calling a contract doesn’t necessarily require an identity, but with the contract create it, you will get access to any read or write function that is permitted by the Smart contract. In the code below, the credenza royalty contract is going to be called to do a point check on a LoyaltyContract type contract at address contractAddressLoyalty for user 0x375fa2f7fec390872a04f9c147c943eb8e48c43d.
In this code, the value of points is written to the pts variable and you can imagine writing some logic for your application that relies on this value. Our example merely uses the value to write to the console. Note that “toString()” is used. That is because values returned from the blockchain are of type BigNumber, which acts differently in blockchain and a conversation is required to make it readable for our console.
Writing into a contract is similar to reading. Again, you will retrieve a contract object, and then call the method to perform the blockchain permutation. Remember that read actions are free, but write actions cost “gas” (either ETH or MATIC, depending whether you are on Ethereum or Polygon). Your gas fees will depend on your agreement with credenza, so make sure there is sufficient funds available for each write. The code below will simply add 42 points to the account of 0x375fa2f7fec390872a04f9c147c943eb8e48c43d.
IMPORTANT NOTE: When writing to the blockchain, you need to generate a wallet using a private key. Do not expose the private key in your client-side code. While we show the ability to create a transaction request on the browser, we highly recommend you ensure all writes happen on the server side where the private key can be secured. The appendices highlight specific programmability of Credenza smart contracts and specific required to instantiate contracts and access methods to retrieve or modify data held in them.