#[starknet::interface]
. The functions are defined under the macro #[abi(embed_v0)]
where external functions are written.
Only deployed instances of the contract can be interacted with. You can refer to the How to Deploy page. Note down the address of your contract, as it is needed for the following part.
package.json{:md}
file is created. Change the type of the package to a module.
index.js{:md}
where we will write JavaScript code to interact with our contract. Let’s start our code by importing from Starknet-js, and from other libraries we will need:
Account
object that will be used to sign transactions, so we need to import the account private key. You can access it directly from your keystore with the following command using Starkli:
.env{:md}
file in your project folder, and paste your private key as shown in the following line:
.env{:md}
files is not recommended for production environments, please use .env{:md}
files only for development purposes! It is HIGHLY recommended to add .gitignore{:md}
, and include your .env file there if you will be pushing your project to GitHub.
:::
Now, import your private key from the environment variables and create your Account object.
./target/simple_storage_SimpleStorage.contract_class.json{:md}
to abi.json{:md}
in the Scarb project folder. The beginning of the content of the ABI file should look like this:
index.js{:md}
file:
fn get(self: @ContractState) -> u128
function, we will be able to read the stored_data
variable from the contract:
node index.js{:md}
in your project directory. After a short amount of time, you should see a “0” as the stored data.
Now, we will set a new number to the stored_data
variable by calling the fn set(self: @mut ContractState, new_data: u128)
function. This is an INVOKE{:md}
transaction, so we need to sign the transaction with our account’s private key and pass along the calldata.
The transaction is signed and broadcasted to the network and it can takes a few seconds for the transaction to be confirmed.