Skip to main content

Deploy and Interact with Lottery Game Smart Contract

In this step, we will deploy our Lottery Game smart contract to the aelf Testnet Sidechain (tDVW).

Step 1: Building Smart Contract

First, let’s compile and build our smart contract.

Navigate to the lottery_game/src directory and run the following command:

Terminal
dotnet build

You should see LotteryGame.dll.patched in the directory lottery_game/src/bin/Debug/net.6.0.

With our smart contract successfully compiled, we are now ready to deploy it!

Step 2: Deploying Your Smart Contract

The smart contract needs to be deployed on the chain before users can interact with it.

Run the following command to deploy a contract.

Terminal
aelf-deploy -a $WALLET_ADDRESS -p $WALLET_PASSWORD -c $CONTRACT_PATH/$CONTRACT_FILE.dll.patched -e https://tdvw-test-node.aelf.io/

Please wait for approximately 1 to 2 minutes. If the deployment is successful, it will provide you with the contract address.

result

tip

ℹ️ Note: Copy the smart contract address ($CONTRACT_ADDRESS) as we will reference it in the next step.

Step 3: Interacting with your Lottery Game Contract

Step 3.1: Approving Smart Contract Spending

To interact with a smart contract on aelf, you need to approve the spending of specific tokens from a specific smart contract. Here, we will approve our Lottery Game smart contract to spend tokens on our behalf.

Terminal
aelf-command send ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io Approve
tip

ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx is the contract address of Multitoken Contract on aelf Testnet Sidechain (tDVW).

When prompted, enter the following parameters to approve the spending of 90 ELF tokens:

Terminal
Enter the params one by one, type `Enter` to skip optional param:
? Enter the required param <spender>: $CONTRACT_ADDRESS
? Enter the required param <symbol>: ELF
? Enter the required param <amount>: 9000000000
tip

You may interact with your contract through this tool: https://aelf-contract-view.vercel.app/

Step 3.2: Initializing Lottery Game Contract

Terminal
aelf-command send $CONTRACT_ADDRESS -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io Initialize

This command initializes the contract and sets your $WALLET_ADDRESS as the owner of the contract.

Step 3.3: Depositing funds into the Lottery Game Contract

Terminal
aelf-command send $CONTRACT_ADDRESS -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io Deposit

Enter the amount you would like to deposit from your wallet. Remember that each token has different decimal values. For ELF, the decimal value is 8, so a value of 100,000,000 is equivalent to 1 ELF.

Step 3.4: Playing the Lottery Game

Terminal
aelf-command send $CONTRACT_ADDRESS -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io Play

Enter the amount you would like to play with from your wallet. Each time you play, there is a 50% chance you might win!

You may check your wallet's current ELF balance to see if you won or lost:

Terminal
aelf-command call ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io GetBalance

You will be prompted for the following:
Enter the required param <symbol>: ELF
Enter the required param <owner>: $WALLET_ADDRESS

You should see the Result displaying your wallet's ELF balance.

Step 4: Conclusion

You have now reached the end of the lottery game contract tutorial. You can invite your friends to play your lottery game contract using their ELF tokens. Here’s a recap of what you have learned:

  1. Develop and Deploy: You developed and deployed a smart contract that utilizes the MultiToken System Contract.
  2. Contract References: You learned how to reference other deployed contracts within your smart contract.

By mastering these skills, you are now equipped to build more complex and integrated smart contracts on the aelf platform. Happy coding!