MarcoWorms / SaveState

save and load string-based states in EVM blockchains

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SaveState Contract

Save and load string-based states in EVM blockchains

Integration Guide

The contract ABI can be found here.

To interact with the contract you need to call the following functions (examples for Fantom):

saveState(string memory state)

This function saves a state for a user.

import { useAccount, useContractWrite, usePrepareContractWrite } from 'wagmi'

const { config } = usePrepareContractWrite({
    address: '0x91afcced2446b635e4988ce4e664234e0ed9c7b0',
    abi: contractABI,
    functionName: 'saveState',
})
const { write } = useContractWrite(config)

const state = {
    counter: 123456
}

write(JSON.stringify(state))

loadState()

This function loads a state for a user.

Example:

import { useAccount, useContractRead, usePrepareContractRead } from 'wagmi'

const { config } = usePrepareContractRead({
    address: '0x91AFCCEd2446B635E4988ce4E664234E0Ed9C7b0',
    abi: contractABI,
    functionName: 'loadState',
})
const { read } = useContractRead(config)

const state = JSON.parse(read()) //{ counter: 123456 }

Saving Many States For A User (Keys)

saveState(string memory key, string memory state)

This function saves a state with a provided key. Default key is "default" (used when function is called without a key).

Example:

import { useAccount, useContractWrite, usePrepareContractWrite } from 'wagmi'

const { config } = usePrepareContractWrite({
    address: '0x91AFCCEd2446B635E4988ce4E664234E0Ed9C7b0',
    abi: contractABI,
    functionName: 'saveState',
})
const { write } = useContractWrite(config)

const state = {
    counter: 123456
}
write('key', JSON.stringify(state))

loadState(string memory key)

This function loads a state with a provided key. Default key is "default" (used when function is called without a key).

Example:

import { useAccount, useContractRead, usePrepareContractRead } from 'wagmi'

const { config } = usePrepareContractRead({
    address: '0x91AFCCEd2446B635E4988ce4E664234E0Ed9C7b0',
    abi: contractABI,
    functionName: 'loadState',
})
const { read } = useContractRead(config)

const state = JSON.parse(read('key')) //{ counter: 123456 }

About

save and load string-based states in EVM blockchains


Languages

Language:Solidity 100.0%