4⃣
Calling Smart Module Functions from Client
As we mentioned earlier, all interactions with the smart module occur through
dapplib
. Therefore, it is important to reference dapplib
in your client from where you’d be interacting with the blockchain. (In the starter projects for Hyperhack, we've already imported DappLib for you.)In your client
js
or jsx
file, reference dapplib
by adding the following import line. This import line will change depending upon the file structure you create within the /packages/client/src
folder.import DappLib from "@decentology/dappstarter-dapplib";
Now that the reference has been added successfully, the next step is to call specific Smart Module functions from
dapplib
. Dapplib
is a library of all the functions the Smart Module has implemented. So instead of interacting directly with the smart modules, we use dapplib
.For example, we want to interact with the
Tribes
Smart Module and within that we want to read the current Tribe of a user. The corresponding dapplib
function to that is TribesGetCurrentTribe
. We can call this dapplib function like and get the result like so: let data = {
tenantOwner: ACCOUNT.Admin,
account: ACCOUNT.Birbal
}
let returnVal = await DappLib.TribesGetCurrentTribe(data);
console.log(returnVal.result);
The
returnVal
will contain the data that is returned by the smart module function. If the function is writing to the blockchain, the returnVal
will contain the transaction hash. If the function is reading from the blockchain, the returnVal.result
will contain the value that is returned. In the case above, we are reading from the blockchain, so we console.log
the result.You may be wondering, "how do I know what data to pass in?" Although you never have to look at the definition of the dapplib function if you don't want to, you can find it inside
/packages/dapplib/src/dapp-lib.js
. From there, if you find the name of the Javascript function you would like to call (in this case TribesGetCurrentTribe
), there are comments above each function specifying what parameters you must pass in.In this example, the function was reading from the blockchain. Let's see an example where we are writing to the blockchain. Here, we are referring to the
Tribes
Smart Module once again. Within that module, we want to call the TribesJoinTribe
method in order to join a Tribe. So within your client, you'd write code like this.let data = {
tenantOwner: ACCOUNT.Admin,
signer: ACCOUNT.Birbal,
tribeName: tribe,
}
const result = await DappLib.TribesJoinTribe(data)
This will join a Tribe named
tribe
from the Birbal account under the Admin's Tenant. result
will contain the transaction hash, verifying that you are truly awesome!Last modified 1yr ago