Write Data

In this section, we will learn how to use the Aptos SDK to submit transactions and write data in Web3, as well as the differences between data writing in Web3 vs Web2.



Overview

  • The most notable distinction between writing data in Web3 vs Web2, is that every write operation in Web3 is treated as a blockchain transaction.
  • Users are required to approve these transactions (writes). This can be done with a wallet.
  • Similar to sending HTTP POST requests or GraphQL mutations for writing data in Web2, in Web3, you use the Aptos SDK to submit transactions and write data.

Web 2Web 3
Tools for Writing DataRESTful APIs, GraphQL, etc.Aptos SDK
Transaction TypeEach write is a data transaction in centralized serversEach write is a blockchain transaction
User Approval RequiredNoUsers need to sign transactions with their wallet


Aptogotchi Example

Source Code

1. Construct your payload object for the feed function.


const payload = {
function: `${NEXT_PUBLIC_CONTRACT_ADDRESS}::main::feed`,
typeArguments: [],
functionArguments: [NEXT_PUBLIC_ENERGY_INCREASE],
};

type: entry_function_payload

  • entry_function_payload is the type you want to use to write data on-chain.
  • Think of entry functions in Move as “setter functions” in languages like Java.

function: ${CONTRACT_ADDRESS}::main::feed

  • CONTRACT_ADDRESS is the address where this smart contract is deployed.
  • main is the Module name, under the aptogotchi contract.
  • feed is the entry function name.

arguments : [ENERGY_INCREASE]

  • ENERGY_INCREASE is the single, integer parameter for the function feed.

const response = await signAndSubmitTransaction({
sender: account.address,
data: payload,
});
await aptosClient.waitForTransaction({ transactionHash: response.hash });


2. Sign and submit the transaction using the signAndSubmitTransaction function, passing in the created payload.

  • Wait for the transaction to be confirmed.
  • It will return a hash that you pass into aptosClient.waitForTransaction, which will then process your transaction on the blockchain.