{"id":26403022,"url":"https://github.com/fzingg/starcli-starknetjs-stepbystep","last_synced_at":"2025-03-17T15:29:35.079Z","repository":{"id":199543883,"uuid":"703155244","full_name":"fzingg/starcli-starknetjs-stepbystep","owner":"fzingg","description":"A step by step by on how to deploy and interact with Starknet smart contracts, using both the Starkcli tool and Starknetjs sdk.","archived":false,"fork":false,"pushed_at":"2023-10-30T18:18:45.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-10-30T19:28:27.658Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fzingg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-10T17:41:41.000Z","updated_at":"2023-10-27T18:16:49.000Z","dependencies_parsed_at":"2023-10-14T17:53:03.625Z","dependency_job_id":"22e8ba11-a197-4ca7-82c7-0bd967e47199","html_url":"https://github.com/fzingg/starcli-starknetjs-stepbystep","commit_stats":null,"previous_names":["fzingg/starkcli-starknetjs-stepbystep"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzingg%2Fstarcli-starknetjs-stepbystep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzingg%2Fstarcli-starknetjs-stepbystep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzingg%2Fstarcli-starknetjs-stepbystep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzingg%2Fstarcli-starknetjs-stepbystep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fzingg","download_url":"https://codeload.github.com/fzingg/starcli-starknetjs-stepbystep/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244057894,"owners_count":20390988,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-17T15:29:34.534Z","updated_at":"2025-03-17T15:29:35.073Z","avatar_url":"https://github.com/fzingg.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# starkli-starknetjs-stepbystep\nA step by step guide on how to deploy and interact with Starknet smart contracts, using both the Starcli tool and Starknetjs sdk.\n\nIt can be useful for achieving few quests of the amazing [node guardians](https://nodeguardians.io/) platform where you can learn Cairo and Starknet, amongs many others skills.\n\n## Table of contents\n\n- [Useful resources and tools](https://github.com/fzingg/starcli-starknetjs-stepbystep#useful-resources-and-tools)\n- [Argent X smart wallet account](https://github.com/fzingg/starcli-starknetjs-stepbystep#argent-x-smart-wallet-account)\n- [Starkli](https://github.com/fzingg/starcli-starknetjs-stepbystep#starkli)\n- [Starkli - Setting up environment](https://github.com/fzingg/starcli-starknetjs-stepbystep#setting-up-environment)\n- [Starkli - Creating a signer](https://github.com/fzingg/starcli-starknetjs-stepbystep#creating-a-signer)\n- [Starkli - Creating an account descriptor](https://github.com/fzingg/starcli-starknetjs-stepbystep#creating-an-account-descriptor)\n- [Starkli - Setting up Environment Variables](https://github.com/fzingg/starcli-starknetjs-stepbystep#setting-up-environment-variables)\n- [Starkli - Build a cairo smart-contract](https://github.com/fzingg/starcli-starknetjs-stepbystep#build-a-cairo-smart-contract)\n- [Starkli - Declaring your contract](https://github.com/fzingg/starcli-starknetjs-stepbystep#declaring-your-contract)\n- [Starkli - Deploying Smart Contracts on Starknet](https://github.com/fzingg/starcli-starknetjs-stepbystep#deploying-smart-contracts-on-starknet)\n- [Starkli - Interacting with the Starknet contract](https://github.com/fzingg/starcli-starknetjs-stepbystep#interacting-with-the-starknet-contract)\n- [Starknet.js](https://github.com/fzingg/starcli-starknetjs-stepbystep#starknetjs)\n- [Starknet.js - What is Starknet.js ?](https://github.com/fzingg/starcli-starknetjs-stepbystep#what-is-starknetjs-)\n- [Starknet.js - Installation](https://github.com/fzingg/starcli-starknetjs-stepbystep#installation)\n- [Starknet.js - Create a project for running samples files](https://github.com/fzingg/starcli-starknetjs-stepbystep#create-a-project-for-running-samples-files)\n- [Starknet.js - Connect to an existing account](https://github.com/fzingg/starcli-starknetjs-stepbystep#connect-to-an-existing-account)\n- [Starknet.js - Connect to a deployed contract](https://github.com/fzingg/starcli-starknetjs-stepbystep#connect-to-a-deployed-contract)\n- [Starknet.js - Interact with your contract\n](https://github.com/fzingg/starcli-starknetjs-stepbystep#interact-with-your-contract)\n- [Starknet.js -]()\n- [Starknet.js -]()\n- [Starknet.js -]()\n\n## Useful resources and tools\n\n- [Create an ArgentX wallet](https://www.argent.xyz/learn/how-to-create-an-argent-x-wallet/)\n- [The starknet book](https://book.starknet.io/chapter_1/environment_setup.html)\n- [Starknet.js guide](https://www.starknetjs.com/docs/guides/intro)\n- [The Cairo programming language](https://book.cairo-lang.org/title-page.html)\n- [Starknet by examples](https://starknet-by-example.voyager.online/starknet-by-example.html)\n- [Various type convertor (Felt, Hex, String, ...)](https://www.stark-utils.xyz/converter)\n-  [StarkNet’s Cairo Language: the Felt Integer Type Explained](https://www.youtube.com/watch?v=jcrAq71WwSM)\n- [Smart Contract Development With Starknet \u0026 Cairo Language with Katana Local Node](https://livesoftwaredeveloper.com/articles/8/smart-contract-development-with-starknet-cairo-language-with-katana-local-node)\n- [Cairo and javascript data transformation](https://www.starknetjs.com/docs/guides/define_call_message/)\n\n## Argent X smart wallet account\nWe will need to create an argentX smart wallet and an account, which will be used when interacting with Starknet smart contracts.\n\n- Follow the guide: [Create an ArgentX wallet](https://www.argent.xyz/learn/how-to-create-an-argent-x-wallet/)\n- Fund your wallet with ETH. Use [Starknet Goerli Faucet](https://faucet.goerli.starknet.io/) as needed.\n\nNote: Instead of set up a walllet and funding it, you canuse Katana as a local node.\n\n## Starkli\n\nWe will setup starkli then compile, deploy and interact with a starknet smart contract written in Cairo.\n\n### Setting up environment\n\n1. Install `starkliup`\n```\ncurl https://get.starkli.sh | sh\n```\n2. Restart the terminal\n3. Install starkli\n```\nstarkliup\n```\n4. Restart the terminal and run\n```\nstarkli --version\n```\n\n### Creating a signer\n\nThe Signer is an essential smart contract capable of signing transactions in Starknet. You’ll need the private key from your smart wallet to create one, from which the public key can be derived.\n\nStarkli enables secure storage of your private key through a keystore file. This encrypted file can be accessed using a password and is generally stored in the default Starkli directory.\n\n1. Create the default directory:\n```\nmkdir ~/.starkli-wallets/deployer -p\n```\n2. Export your private key from your Argent X wallet account and saved it.\n```\nExport the private key from your Braavos or Argent wallet. For Argent X, you can find it in the \"Settings\" section → Select your Account → \"Export Private Key\". For Braavos, you can find it in the \"Settings\" section → \"Privacy and Security\" → \"Export Private Key\".\n```\n\n3. Generate the keystore file:\n```\nstarkli signer keystore from-key ~/.starkli-wallets/deployer/my_keystore_1.json\nEnter private key:\nEnter password:\n```\nIn the private key prompt, paste the private key of your smart wallet. In the password prompt, enter a password of your choice. You will need this password to sign transactions using Starkli.\n\n### Creating an account descriptor\n\n1. Get your Argent X account address\n2. Create the account descriptor:\n```\nstarkli account fetch \u003cSMART_WALLET_ADDRESS\u003e --output ~/.starkli-wallets/deployer/my_account_1.json\n```\nPaste your account address in place of \u003cSMART_WALLET_ADDRESS\u003e\n\n### Setting up Environment Variables\n\n```\nexport STARKNET_ACCOUNT=~/.starkli-wallets/deployer/my_account_1.json\nexport STARKNET_KEYSTORE=~/.starkli-wallets/deployer/my_keystore_1.json\n```\n\n### Build a cairo smart-contract\n1. Create a scarb project:\n```\nscarb new smartcontract\n```\n2. Create a first_contract.cairo file\nCopy this repo sample `cairo contract/first_contract.cairo` to your project src directory `smart_contract/src`\n\n3. Compile the contract:\n\n```\nscarb build\n```\nThis creates a compiled contract in target/dev/ as `smartcontract_Ownable.sierra.json`\n\n### Declaring your contract\n\n1. Run this command to declare your contract using the default Starknet Sequencer’s Gateway:\n\n```\nstarkli declare target/dev/smartcontract_Ownable.sierra.json\n```\n2. At the `Enter keystore password` use the Argent X account password you set up.\n\n3. After running the command, you’ll receive a contract class hash. This unique hash serves as the identifier for your contract class within Starknet. For example:\n\n```\nClass hash declared: 0x04c70a75f0246e572aa2e1e1ec4fffbe95fa196c60db8d5677a5c3a3b5b6a1a8\n```\n\nYou can think of this hash as the contract class’s 'address.' Use a block explorer like [StarkScan](https://testnet.starkscan.co/class/0x04c70a75f0246e572aa2e1e1ec4fffbe95fa196c60db8d5677a5c3a3b5b6a1a8) to verify this hash on the blockchain.\n\n### Deploying Smart Contracts on Starknet\n\nTo deploy a smart contract, you’ll need to instantiate it on Starknet’s testnet. This process involves executing a command that requires two main components:\n\n1. The class hash of your smart contract.\n2. Any constructor arguments that the contract expects. (In our example, the constructor expects an 'owner' address.)\n\nThe command would look like this:\n```\nstarkli deploy \\\n    \u003cCLASS_HASH\u003e \\\n    \u003cCONSTRUCTOR_INPUTS\u003e\n```\n\nHere’s a specific example with an actual class hash and constructor inputs (as the owner address use the address of your smart wallet so you can invoke the transfer_ownership function later):\n```\nstarkli deploy \\\n    0x04c70a75f0246e572aa2e1e1ec4fffbe95fa196c60db8d5677a5c3a3b5b6a1a8 \\\n    0x02cdAb749380950e7a7c0deFf5ea8eDD716fEb3a2952aDd4E5659655077B8510\n```\n\nAfter executing the command and entering your password, you should see output like the following:\n\n```\nDeploying class 0x04c70a75f0246e572aa2e1e1ec4fffbe95fa196c60db8d5677a5c3a3b5b6a1a8 with salt 0x065034b27a199cbb2a5b97b78a8a6a6c6edd027c7e398b18e5c0e5c0c65246b7...\nThe contract will be deployed at address 0x02a83c32d4b417d3c22f665acbc10e9a1062033b9ab5b2c3358952541bc6c012\nContract deployment transaction: 0x0743de1e233d38c4f3e9fb13f1794276f7d4bf44af9eac66e22944ad1fa85f14\nContract deployed:\n0x02a83c32d4b417d3c22f665acbc10e9a1062033b9ab5b2c3358952541bc6c012\n```\n\nThe contract is now live on the Starknet testnet. You can verify its status using a block explorer like StarkScan. On the \"Read/Write Contract\" tab, you’ll see the contract’s external functions.\n\n### Interacting with the Starknet contract\n\n#### Calling a read Function\n\nThe call command enables you to query a smart contract function without sending a transaction. For instance, to find out who the current owner of the contract is, you can use the `get_owner` function, which requires no arguments.\n\n```\nstarkli call \\\n    \u003cCONTRACT_ADDRESS\u003e \\\n    get_owner\n```\n\nReplace \u003cCONTRACT_ADDRESS\u003e with the address of your contract. The command will return the owner’s address, which was initially set during the contract’s deployment:\n\n```\n[\n    \"0x02cdab749380950e7a7c0deff5ea8edd716feb3a2952add4e5659655077b8510\"\n]\n```\n\n#### Invoking a Write Function\n\nYou can modify the contract’s state using the invoke command. For example, let’s transfer the contract’s ownership with the transfer_ownership function.\n\n```\nstarkli invoke \\\n    \u003cCONTRACT_ADDRESS\u003e \\\n    transfer_ownership \\\n    \u003cNEW_OWNER_ADDRESS\u003e\n```\n\nReplace \u003cCONTRACT_ADDRESS\u003e with the address of the contract and \u003cNEW_OWNER_ADDRESS\u003e with the address you want to transfer ownership to.\n\n## Starknet.js\n\n### What is Starknet.js ?\n\nStarknet.js is a library that helps to connect your website or your Decentralized Application (DAPP) to the blockchain-based Starknet network, using Javascript / Typescript language.\n\nFull documentation: [Starknet.js](https://www.starknetjs.com/docs/guides/what_s_starknet.js)\n\nGreat tutorials implemating React full project: [Starknet.js examples](https://book.starknet.io/ch02-07-01-examples.html)\n\n### Installation\n\n- use the main branch\n\n```\nnpm install starknet\n```\n\n- to use latest features (merges in develop branch)\n```\nnpm install starknet@next\n```\n\n### Create a project for running samples files\n\nJust run the command:\n```\nnpm init\n```\nThen you can copy all samples files from `starknetjs samples` directory of this repo to your local project.\n\n### Connect to an existing account\n\nWe are going to use our [Argent X smart wallet account](https://github.com/fzingg/starcli-starknetjs-stepbystep#argent-x-smart-wallet-account) we created previously.\n\nWe will need :\n- The address of the account (public data)\n- The private key of the account (very sensistive data)\n\nWe will use the `Account(provider, accountAddress, privateKey)` method.\n\nIn the sample javascript file of this repo `starknetjs samples/connect_existing_account.js` replace the `privateKey` and `accountAddress` with your argentX account values.\n\nRun the script: \n```\nnode connect_existing_account.js\n```\n\nyou should see the account information in the console:\n```\naccount Account {\n  provider: SequencerProvider {\n    responseParser: SequencerAPIResponseParser {},\n    baseUrl: 'https://alpha4.starknet.io',\n    feederGatewayUrl: 'https://alpha4.starknet.io/feeder_gateway',\n    gatewayUrl: 'https://alpha4.starknet.io/gateway',\n    chainId: '0x534e5f474f45524c49',\n    headers: undefined,\n    blockIdentifier: 'pending'\n  },\n  deploySelf: [AsyncFunction: deployAccount],\n  address: '0x0296xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',\n  signer: Signer {\n    pk: '0x54xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\n  },\n  cairoVersion: '0'\n}\n```\n\n### Connect to a deployed contract\n\nPreviously we deployed a contract.\nWe will now connect to it by using the `new Contract(testAbi, testAddress, provider)` method.\n\nIn the sample javascript file of this repo `starknetjs samples/connect_deployed_contract.js` replace the `ContractAddress` with your deployed contract address.\n\nRun the script: \n```\nnode connect_deployed_contract.js\n```\n\nYou should see all information about your contract in the console.\n\n### Interact with your contract\n\n#### Calling a read function\n\nOur previsously deployed contract has a `get_owner` function which returns the address of the deployer account.\n\nWe will call that method directly on the contract instance we connected to previously: \n```\nmyTestContract.get_owner();\n```\n\nIn the sample javascript file of this repo `starknetjs samples/interact_read_contract.js` replace the `ContractAddress` with your deployed contract address.\n\nRun the script: \n```\nnode interact_read_contract.js\n```\n\nYou should see your account address which is the owner of your contract.\n\nnote: We use `\"0x\" + BigInt(OwnerAddress).toString(16)` to convert the number returned by starknet to a HEX address.\n\n#### Write to contract memory, with meta-class\n\nOur previsously deployed contract has a `transfer_ownership` function which transfers the account ownership of the contract.\n\nThe method definition is: \n\n```\nfn transfer_ownership(ref self: ContractState, new_owner: ContractAddress)\n```\n\nSo from our starknet script we will have to pass the `new_owner` account address.\n\nWe will use that way in the script:\n\n```\nconst myCall = myTestContract.populate(\"transfer_ownership\", { new_owner: NewOwnerAddress });\n    const res = await myTestContract.transfer_ownership(myCall.calldata);\n```\n\nIn the sample javascript file of this repo `starknetjs samples/interact_write_contract.js` replace :\n- the `ContractAddress` with your deployed contract address.\n- the `privateKey` with your own account private key.\n- the `accountAddress` corrsponding (the own you used previously to deploy the contract).\n- the `NewOwnerAddress` with an a new account address (you can create another account on your ArgentX wallet for example).\n\nRun the script: \n```\nnode interact_write_contract.js\n```\n\nThen you can verify the ownership has been correctly transfered by checking on the [testnet starknet explorer](https://testnet.starkscan.co/). (go to Read/write Contract tab, click Read button, then the `get_owner` method).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffzingg%2Fstarcli-starknetjs-stepbystep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffzingg%2Fstarcli-starknetjs-stepbystep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffzingg%2Fstarcli-starknetjs-stepbystep/lists"}