From f10ece8ff30d87c014093948b32fdd4474ac8fd7 Mon Sep 17 00:00:00 2001 From: vasmohi Date: Mon, 19 Aug 2024 10:16:06 +0530 Subject: [PATCH 1/3] feat: Added section explaining inter contract calls --- .../lottery-game-smart-contract/index.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/quick-start/developers/lottery-game-smart-contract/index.md b/docs/quick-start/developers/lottery-game-smart-contract/index.md index 3fd36e55..4952d0a4 100644 --- a/docs/quick-start/developers/lottery-game-smart-contract/index.md +++ b/docs/quick-start/developers/lottery-game-smart-contract/index.md @@ -1380,6 +1380,41 @@ Enter the required param : ELF Enter the required param : $WALLET_ADDRESS ``` +## Understanding Inter-Contract Calls in aelf + +In this section, we'll explore how inter-contract calls work in the aelf blockchain using the lottery game example from the tutorial. This will help you understand how different smart contracts can interact to perform complex operations. + +### 1. **Smart Contract Overview** + - **Lottery Contract**: Manages the game, including buying tickets, drawing winners, and distributing prizes. + - **Token Contract**: Handles the token transactions needed for buying lottery tickets. + +### 2. **Ticket Purchase Process** + - **Initiating Purchase**: When a user wants to buy a lottery ticket, they interact with the Lottery Contract. + - **Token Transfer Requirement**: The Lottery Contract must verify that the user has enough tokens and transfer those tokens to the lottery’s account to complete the purchase. + +### 3. **Making an Inter-Contract Call** + - **Calling Token Contract**: The Lottery Contract needs to interact with the Token Contract to transfer tokens. + - **Method Invocation**: It calls a method in the Token Contract, such as `Transfer`. + - **Parameters**: The call includes details like the sender’s address, the recipient’s address (the lottery account), and the amount of tokens. + - **Encoding and Sending**: The parameters are encoded into a transaction format and sent to the Token Contract. + +### 4. **Processing in the Token Contract** + - **Token Transfer**: The Token Contract processes the transfer request by deducting tokens from the user’s account and adding them to the lottery account. + - **Return Response**: The Token Contract then returns a result indicating whether the transfer was successful or if it failed. + +### 5. **Handling the Response** + - **Lottery Contract’s Role**: Once the Lottery Contract receives the response from the Token Contract, it checks if the transfer was successful. + - **Next Steps**: If successful, the Lottery Contract updates the user's lottery ticket entries and continues with the game logic. + +#### 6. **Authorization and Security** + - **Permission Checks**: Ensures that the Lottery Contract is authorized to invoke methods in the Token Contract. + - **Secure Transactions**: Ensures that token transfers are secure and correctly authorized. + +### 7. **Error Handling** + - **Failure Management**: If the token transfer fails (e.g., due to insufficient funds), the Lottery Contract handles the error by potentially reverting the transaction or notifying the user. + +By following these steps, you can see how inter-contract calls in aelf allow different contracts to work together smoothly. This modular approach helps in building complex applications like a lottery game by ensuring secure and authorized interactions between contracts. + ## 🎯 Conclusion From c3fd0ddc4305b67fd1481f5e6c7eb9386fc03c75 Mon Sep 17 00:00:00 2001 From: vasmohi Date: Mon, 26 Aug 2024 09:28:48 +0530 Subject: [PATCH 2/3] fix: Added point of Initializing contract state in inter-contract calls --- .../developers/lottery-game-smart-contract/index.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/quick-start/developers/lottery-game-smart-contract/index.md b/docs/quick-start/developers/lottery-game-smart-contract/index.md index 4952d0a4..3378ccb3 100644 --- a/docs/quick-start/developers/lottery-game-smart-contract/index.md +++ b/docs/quick-start/developers/lottery-game-smart-contract/index.md @@ -1392,25 +1392,28 @@ In this section, we'll explore how inter-contract calls work in the aelf blockch - **Initiating Purchase**: When a user wants to buy a lottery ticket, they interact with the Lottery Contract. - **Token Transfer Requirement**: The Lottery Contract must verify that the user has enough tokens and transfer those tokens to the lottery’s account to complete the purchase. -### 3. **Making an Inter-Contract Call** +### 3. **Initialising Contract Reference State** + - **Setting Contract Address**: The Lottery Contract must first initialize its reference to the Token Contract by setting the correct contract address in its state. + +### 4. **Making an Inter-Contract Call** - **Calling Token Contract**: The Lottery Contract needs to interact with the Token Contract to transfer tokens. - **Method Invocation**: It calls a method in the Token Contract, such as `Transfer`. - **Parameters**: The call includes details like the sender’s address, the recipient’s address (the lottery account), and the amount of tokens. - **Encoding and Sending**: The parameters are encoded into a transaction format and sent to the Token Contract. -### 4. **Processing in the Token Contract** +### 5. **Processing in the Token Contract** - **Token Transfer**: The Token Contract processes the transfer request by deducting tokens from the user’s account and adding them to the lottery account. - **Return Response**: The Token Contract then returns a result indicating whether the transfer was successful or if it failed. -### 5. **Handling the Response** +### 6. **Handling the Response** - **Lottery Contract’s Role**: Once the Lottery Contract receives the response from the Token Contract, it checks if the transfer was successful. - **Next Steps**: If successful, the Lottery Contract updates the user's lottery ticket entries and continues with the game logic. -#### 6. **Authorization and Security** +### 7. **Authorization and Security** - **Permission Checks**: Ensures that the Lottery Contract is authorized to invoke methods in the Token Contract. - **Secure Transactions**: Ensures that token transfers are secure and correctly authorized. -### 7. **Error Handling** +### 8. **Error Handling** - **Failure Management**: If the token transfer fails (e.g., due to insufficient funds), the Lottery Contract handles the error by potentially reverting the transaction or notifying the user. By following these steps, you can see how inter-contract calls in aelf allow different contracts to work together smoothly. This modular approach helps in building complex applications like a lottery game by ensuring secure and authorized interactions between contracts. From 3121b3b52434c1be09412848e05af52f8a231036 Mon Sep 17 00:00:00 2001 From: vasmohi Date: Mon, 26 Aug 2024 09:38:06 +0530 Subject: [PATCH 3/3] fix: fixed english --- .../quick-start/developers/lottery-game-smart-contract/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/quick-start/developers/lottery-game-smart-contract/index.md b/docs/quick-start/developers/lottery-game-smart-contract/index.md index 3378ccb3..ccd37b9f 100644 --- a/docs/quick-start/developers/lottery-game-smart-contract/index.md +++ b/docs/quick-start/developers/lottery-game-smart-contract/index.md @@ -1392,7 +1392,7 @@ In this section, we'll explore how inter-contract calls work in the aelf blockch - **Initiating Purchase**: When a user wants to buy a lottery ticket, they interact with the Lottery Contract. - **Token Transfer Requirement**: The Lottery Contract must verify that the user has enough tokens and transfer those tokens to the lottery’s account to complete the purchase. -### 3. **Initialising Contract Reference State** +### 3. **Initializing Contract Reference State** - **Setting Contract Address**: The Lottery Contract must first initialize its reference to the Token Contract by setting the correct contract address in its state. ### 4. **Making an Inter-Contract Call**