Sample Code

Below are various sample code that demonstrates various use-cases you may encounter writing custom actions:

Get a booth owner

Within the ++ action arm of your custom action handler, obtain booth information using the following hoon:

::  note that store and context are both provided in the outer/parent core in
::    which the 'on' arm resides
=/  booth  (~(get by booths.store) booth-key.context)
::  the call above will return an object of type (unit json)
::    you'll want to ensure this is not null, and if not, grab the inner
::    json object using `need`
=/  booth  ?~(booth ~ (need booth))
::  at this point, booth will either be null or a valid json object. however,
::  before you can work with the booth as json, you'll need to verify it's a valid
::  json type using the following code
=/  booth  ?:(?=([%o *] booth) p.booth ~)
::  here, booth will be a (map @t json) instance or null (default of map). from this
::   point forward you can, manipulate/query the booth data using stdlib map functions
::  for example, if you wanted to get the booth owner:
=/  booth-owner  (~(get by booth) 'owner')
=/  booth-owner  ?~(booth-owner '' (so:dejs:format (need booth-owner)))
::  booth-owner here is either empty string (not found) or the owner
::    (ship name e.g. ~zod)
...

Get a proposal's strategy

Within the ++ action arm of your custom action handler, obtain booth information using the following hoon. Remember that within the context of the custom-action handler (outer core's ++ on gate), you have both a booth-key and proposal-key in the context.

Therefore given booth-key and proposal-key (both in custom action context), you can obtain the detail of a proposal as follows:

::  get a dictionary (map @t json) of proposals for a given booth (by booth-key)
=/  proposals  (~(get by proposals.store) booth-key.context)
=/  proposals  ?~(proposals ~ (need proposals))
=/  proposals  ?:(?=([%o *] proposals) p.proposals ~)
::  get a specific proposal (json) within a booth by querying the proposals
::   return by the calls above
=/  proposal  (~(get by proposals) proposal-key.context)
=/  proposal  ?~(proposal ~ (need proposal))
=/  proposal  ?:(?=([%o *] proposal) p.proposal ~)
::  query the proposal as needed (see proposal detail below)
::  for example, to get the proposal's voting strategy
=/  strategy  (~(get by proposal) 'strategy')
=/  strategy  ?~(strategy 'unknown' (so:dejs:format (need strategy)))
::  strategy will either be 'unknown' (not exists in json)
::    or strategy value (e.g. 'single-choice')
...

Get a participant's role

::  get a dictionary (map @t json) of participants for a given booth (by booth-key)
=/  participants  (~(get by participants.store) booth-key.context)
=/  participants  ?~(participants ~ (need participants))
=/  participants  ?:(?=([%o *] participants) p.participants ~)
::  get a specific participant (json) within a booth by querying the participants
::   return by the calls above
=/  participant  (~(get by participants) participant-key.context)
=/  participant  ?~(participant ~ (need prparticipantoposal))
=/  participant  ?:(?=([%o *] participant) p.participant ~)
::  query the participant as needed (see participant detail below)
::  for example, to get the participant's role
=/  role  (~(get by participant) 'role')
=/  role  ?~(role 'unknown' (so:dejs:format (need role)))
::  role will either be 'unknown' (not exists in json)
::    or role value (e.g. 'member')
...

Last updated