Link an Account to Image, Text and Time Metadata with On-Chain Storage

Create Rust LookupMap data structures that store image IPFS URIs, text content and timestamps linked to NEAR wallet addresses. Learn how to put & fetch these efficiently via the UI.

Getting Started

Follow the Project Setup instructions to start the project in your local machine and try the recipes yourself. You can also clone or navigate the repo to get just what you need.

In the previous chapter:

Render a Data Table from Contract VIEW Methods

we explain how to create new game contracts and how some of its storage is set upon initialization:

impl Market {
    pub fn new(
        market: MarketData,
        management: Management,
        collateral_token: CollateralToken,
    ) -> Self {
        if env::state_exists() {

        let starts_at: Timestamp = env::block_timestamp().try_into().unwrap();
        let ends_at: Timestamp = starts_at + EVENT_PERIOD_NANOS;
        let reveal_window = ends_at + STAGE_PERIOD_NANOS;
        let resolution_window = reveal_window + STAGE_PERIOD_NANOS;

        // 7 days
        let self_destruct_window = resolution_window + 604_800_000_000_000;

        Self {
            market: MarketData {
            outcome_tokens: LookupMap::new(StorageKeys::OutcomeTokens),
            players: Vector::new(b"p"),
            resolution: Resolution {
                window: resolution_window,
                resolved_at: None,
                result: None,
            fees: Fees {
                price: CREATE_OUTCOME_TOKEN_PRICE,
                fee_ratio: FEE_RATIO,
                claimed_at: None,
            collateral_token: CollateralToken {
                balance: 0,
                fee_balance: 0,
                // @TODO collateral_token_decimals should be set by a cross-contract call to ft_metadata
            management: Management {
                buy_sell_threshold: BUY_SELL_THRESHOLD,

Storing image URIs

This technique has been in the blockchain industry for a while now, instead of storing the entire image file object, store only an IPFS hash of the image, or its equivalent.

Prompt Wars has IPFS utilities that you may implement in your dApp to achieve similar results:

These IPFS helpers are then used to orchestrate a new game contract:

Storing player data

Once a game is created, a player may submit a text prompt. Follow this walkthrough to learn how to link data to a player, on chain.

When a player submits a prompt, we call ft_transfer_call on the corresponding NEP141 contract, in this case USDT . If the transfer succeeds, it will make a cross-contract call to the game contract:

Let's look at the payload structure:

#[derive(Serialize, Deserialize)]
pub struct CreateOutcomeTokenArgs {
    // the outcome value, in this case, the prompt submitted to the competition
    pub prompt: String,

#[derive(Serialize, Deserialize)]
pub enum Payload {

Setting values from the client side

Once a contract client interface is instantiated, we may call its CHANGE methods:

In this case

static async ftTransferCall(wallet: Wallet, contractAddress: AccountId, args: FtTransferCallArgs)

Will be called upon the click of the "Submit Prompt" button, passing down the payload as it is expected by the Rust contract:

const onSubmit = async (prompt: Prompt) => {
    if (marketContractValues.isOver) {
        variant: "error",
        title: t("promptWars.marketisover.title"),
        children: <Typography.Text>{t("promptwars.marketisover.description")}</Typography.Text>,


    await ftTransferCall(prompt);

The clue to this storage strategy is in the definition of

#[derive(BorshSerialize, BorshDeserialize, Serialize)]
pub struct OutcomeToken {
    // the account id of the outcome_token
    pub outcome_id: OutcomeId,
    // the outcome value, in this case, the prompt submitted to the competition
    pub prompt: String,
    // the outcome value, in this case, the prompt submitted to the competition
    pub output_img_uri: Option<String>,
    // store the result from the image comparison: percentage_diff or pixel_difference
    pub result: Option<OutcomeTokenResult>,
    // total supply of this outcome_token
    pub total_supply: WrappedBalance,

an OutcomeToken is our naming convention of a player's prompt data, the image URI that got rendered from its prompt and the collateral token balance relative to the player's NEP141 deposit:

Rendering images from a player's OutcomeToken

The OutcomeToken  implementation has VIEW methods useful for getting any player's prompt data:

Last updated