Rise In Logo



Move on Sui Course

Functions 1

  • In this lesson, you will create a function to create developer card NTFs.
  • You will work with events, SUI tokens and object tables.
  • Events are important since they let the frontend know about the changes on the blockchain.
  • We use object tables to store developer card NTFs.

You can access the codes we wrote in this lesson below. 👇

  // We are initating our contract.
  // DevHub created a shared object so that users can modify or alter their cards
  fun init(ctx: &mut TxContext) {
    transfer::share_object(
      DevHub {
        id: object::new(ctx),
        owner: tx_context::sender(ctx),
        counter: 0,
        cards: object_table::new(ctx),
      }
    );
  }

  // This function creates new card and adds it to the table
  public entry fun create_card(
    name: vector<u8>,
    title: vector<u8>,
    img_url: vector<u8>,
    years_of_exp: u8,
    technologies: vector<u8>,
    portfolio: vector<u8>,
    contact: vector<u8>,
    payment: Coin<SUI>,
    devhub: &mut DevHub,
    ctx: &mut TxContext
  ) {
    let value = coin::value(&payment); // get the tokens transferred with the transaction
    assert!(value == MIN_CARD_COST, INSUFFICIENT_FUNDS); // check if the sent amount is correct
    transfer::public_transfer(payment, devhub.owner); // tranfer the tokens

    // Here we increase the counter before adding the card to the table
    devhub.counter = devhub.counter + 1;

    // Create new id
    // Id is created here because we are going to use it with both devcard and the event
    let id = object::new(ctx);

    // Emit the event
    event::emit(
      CardCreated { 
        id: object::uid_to_inner(&id), 
        name: string::utf8(name), 
        owner: tx_context::sender(ctx), 
        title: string::utf8(title), 
        contact: string::utf8(contact) 
      }
    );

    // Creating the new DevCard
    let devcard = DevCard {
      id: id,
      name: string::utf8(name),
      owner: tx_context::sender(ctx),
      title: string::utf8(title),
      img_url: url::new_unsafe_from_bytes(img_url),
      description: option::none(),
      years_of_exp,
      technologies: string::utf8(technologies),
      portfolio: string::utf8(portfolio),
      contact: string::utf8(contact),
      open_to_work: true,
    };

    // Adding card to the table
    object_table::add(&mut devhub.cards, devhub.counter, devcard);
  }


Comments

Anonymous

0/500

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!