Skip to content

Parse valid JSON array of objects and insert each object into a substrate pallet's StorageMap #20

@ltfschoen

Description

@ltfschoen

@xlc i've been trying to use lite-json, and i noticed that you actually created it!

i'm want to use off-chain workers to query an external API endpoint in our code here where i've added the off-chain workers example code https://github.com/DataHighway-DHX/node/blob/luke/rewards-allowance-new-offchain/pallets/mining/rewards-allowance/src/lib.rs#L2684

i want to retrieve data in the following valid JSON format from the body of the http request response:

{
  "data": [
  	{
	    "acct_id": "1234",
	    "mpower": "1",
	    "date_last_updated": "1636096907000"
	},
  	{
	    "acct_id": "1234",
	    "mpower": "1",
	    "date_last_updated": "1636096907000"
	}
  ]
}

then iterate through each object in the array and whilst doing so i'll populate a data structure with each object's values and insert it into the pallet's storage where for each object in the that array i create a key (which is a tuple that contains both the start of the current date that we received the response, and the account_id that was in the response), and a value (which just contains the other two values received including the account_id's mining power mpower and the date that date was last updated off-chain)

    /// Recently submitted mPower data.
    #[pallet::storage]
    #[pallet::getter(fn mpower_of_account_for_date)]
    pub(super) type MPowerForAccountForDate<T: Config> = StorageMap<_, Blake2_128Concat,
        (
            Date, // converted to start of date
            T::AccountId,
        ),
        (
            u128, // mPower
            Date, // date last updated off-chain
            T::BlockNumber, // block receive using off-chain workers
        ),
    >;

i'd also like to know how to serialize and deserialize that kind of object.

how may i do this with lite-json?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions