Ethereum: Instructions for the testing Bitcoin revenue program -controlled
While you create a website on which users can send bitcoins to a new individual usage address, you must implement a way to check when a payment has been received. In this article we will examine how this can be done with the help of the smart contracts from Ethereum and the Web3.js library.
** What is a Bitcoin receipt?
In Bitcoin, a receipt (or transaction) is a message that is sent to the blockchain network by a miner or a single user and confirms that you have successfully confirmed your transaction. When a user sends bitcoins to a new address, the recipient receives this receipt as part of the transaction confirmation process.
Program -controlled review of the receipts
In order to check when a payment is received in the program, you must use the Smart Contracts and the Web3.JS library from Ethereum. Here is a high -ranking overview of the steps:
- Create a new contract : Create a new solidity contract for the Ethereum blockchain, the Bitcoin transactions. This contract contains a logic for checking the receipts.
- Define the receipt event
: Define an event that is broadcast after receipt of a payment, e.g. B. «PayMenteced».
- Implement the verification logic : In the «VerifyRecept» function, check whether the transaction sent to the new address matches the expected signature of a receipt message.
- Update the blockchain status
: If the receipt is valid, update the blockchain status with the new remaining amount and additional information.
Example code
Here is an example of how you can implement this with Solidity, Web3.js and the ERC 20 standard token from Ethereum:
`Solidity
Pragma solidity ^0.8.0;
Import «
Import «
Contract recipient {
// Define the receipt event
Event PayMenteced (Uint256 recipient, Uint256 amount);
// Define a function to check the documents
Function VerifyRecept (
Address to payable recipient,
Uint256 amount,
Character sequence signature
) Public returns (BOOL) {
// Check whether the transaction matches the expected signature
Require (bytes (signature) .Lental == 42, «invalid signature»);
// Call the data transaction
TransactionData Storage TxData = GettransactionData (recipient, amount);
// Check whether the transaction is valid and has a validure
Demand (txdata.transactionhash! = Address (0), «invalid transaction»);
Require (cracking.signature! = «», «Signature is empty»);
Return;
}
// Define the function to obtain transaction data
Get transaction data function (
Address to payable recipient,
Uint256 amount
) Internal view retacted (TransactionData storage) {
Transaction TX = TXS [recipient] [amount];
Spread Spread Storage;
}
}
// Define the subsequent lateral judgment structure
Structure -receipt measurement {
Bytes32 signature;
String message;
}
// Define the transaction data structure
Struct TransactionData {
Address receiver;
Uint256 amount;
}
// Define the transaction structure
Structural spit {
Numbers address according to the sender;
Uint256 amount;
Signature string;
}
`
Explanation
In this example, we have defined a «receipt» contract with two functions: «verifyrecept» and «GettransactionData». The «VerifyRecept» function checks whether the transaction matches the expected signature of a receipt message. It uses Web3.js library to get the transaction data from the blockchain.