Deploying Alpen smart contracts
In this walkthrough we will go through creating a very simple Counter contract, deploying it, and doing some simple contract calls.
Deploying a counter contract
This assumes that you have your local git configured.
Follow the official steps to install Foundry.
Initialize a project with
init
forge init Counter
cd CounterForge will automatically add its standard counter at src/Counter.sol with the code below
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
function setNumber(uint256 newNumber) public {
number = newNumber;
}
function increment() public {
number++;
}
}Compile the contract
Test and build the Counter using Forge
Setup the environment
Before we deploy the contract, we need to make sure we can connect to the Alpen public RPC.
Add the following line to a new .env file in your Counter project folder:
Run source .env to load these parameters in the active shell session and query the endpoint to verify we're on the correct network
The expected result is 8150 in hex format: {"jsonrpc":"2.0","id":1,"result":"0x1fd6"}
You can also run a full node! See Alpen full node operator guide to host your own Alpen RPC endpoints.
Generate a deployer testnet wallet
From now on, you'll need a wallet with an active balance to deploy your contracts. Creating a dedicated deployer is a best practice to keep its private key separated from a standard user account.
Create a new mnemonic with
You'll get an output similar to the following:
Backup or transcribe the 12-word Phrase, and save the Address for later.
Use import command with your mnemonic passphrase to add deployer to the Foundry keystore
Setup a wallet password (or keep it blank) to proceed. See the official Foundry cast documentation for more information on this step.
Foundry keeps all imported wallets in the common directory at ~/.foundry/keystores. If you already created and funded a deployer account, you can skip step 6.
Add balance to the deployer wallet
To get a few testnet coins, open the Alpen faucet at https://faucet.testnet.alpenlabs.io, click enter address, and copy/paste the deployer Address we saved before.
Alternatively, you can receive more coins by Using the Alpen CLI, but the time to bridge funds from Alpen signet is approximately 3 hours.
Deploy the contract
(Optional) Test the Counter using Alpen testnet RPC
Perform a dry run of the contract deployment with forge create
In the result, check that "chainId": "0x1fd6" is pointing at the right network. Issue the command echo $((0x1fd6)) to verify that the resulting chain ID number is 8150 :
Finally, broadcast the transaction with the command
Test the deployed contract
When the deployment is complete, Forge should return the following result. Note the Deployed to address, which will be our contract address
Append the contract address to the .env file to get a result similar to this one:
And issue the command source .env to have the smart contract address handy for the next steps.
Smoke-test the current state of our Counter with
Congratulations! We have now successfully deployed the Counter contract on Alpen Testnet.
Interacting with the Counter using cast
In this section, you will interact with the Counter contract using a separate account that we'll call user.
Create a user account and fund it with the faucet.
Follow the same steps used for the deployer
And, similarly to the deployer, import the passphrase to the keystore:
Fund the user account using the faucet
Open the Alpen testnet faucet at https://faucet.testnet.alpenlabs.io, click enter address, and copy/paste the user wallet address to receive a few testnet coins.
We can avoid the faucet entirely by sending coins from our deployer using Cast. First, make sure we have enough balance with the commands
Then use the send command to transfer funds
Note that we use ether to instruct cast that it has to send the chain's native currency (on Alpen's testnet it is sBTC)
Interact with the Counter contract
First, view the current state of our Counter contract
Then, increase it by one unit with the command
You should get a full execution report similar to the one below, with the line status 1(success) confirming that our contract call was successful
Call again number() to see the new state of the Counter contract
Optional: verify the contract on Alpen's Blockscout explorer
Alpen's Blockscout explorer supports forge verify-contract to publish a contract source code and show a Verified contract icon next to its address.
Issue the following command to send a verification request
Right after it finished, open Alpen explorer at the verified contracts page to see our Counter listed at the top of the list.
Last updated