Skip to content

createWrappedResponse

createWrappedResponse is a function that needs to be called in order to return a wrappedResponse in the getRelevantData function of setup.

This function needs 5 parameters passed into it:

  1. accountId - the account ID that's passed into getRelevantData
  2. accountCreated - a boolean indicating whether or not an account was created for the first time
  3. accountHash - the hash field on the account
  4. accountTimestamp - the timestamp field on the account
  5. account - the entire account object
💡

The example shown below demonstrates how to use this function within a chat application.

getRelevantData(accountId, tx) {
  let account = accounts[accountId]
  let accountCreated = false
  // Create the account if it doesn't exist
  if (typeof account === 'undefined' || account === null) {
    if (tx.type === 'register') {
      if (accountId === tx.aliasHash) {
        account = createAlias(accountId)
        accounts[accountId] = account
        accountCreated = true
      }
    } else if (tx.type === 'message') {
      if (accountId === tx.chatId) {
        account = createChat(accountId)
        accounts[accountId] = account
        accountCreated = true
      }
    }
  }
  if (typeof account === 'undefined' || account === null) {
    account = createAccount(accountId, tx.timestamp)
    accounts[accountId] = account
    accountCreated = true
  }
  // Wrap it for Shardus
  const wrapped = dapp.createWrappedResponse(accountId, accountCreated, account.hash, account.timestamp, account)
  return wrapped
}