# Magician-web3
**Repository Path**: linux2014/Magician-web3
## Basic Information
- **Project Name**: Magician-web3
- **Description**: Magician-web3 is a blockchain development toolkit. It consists of two functions. One is to scan the blockchain and monitor the transactions according to the developer's needs.
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: master
- **Homepage**: https://magician-io.com
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 3
- **Created**: 2022-11-07
- **Last Updated**: 2022-11-07
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
Magician-web3 is a blockchain development toolkit.
It consists of two functions. One is to scan the blockchain and monitor the transactions according to the developer's needs.
The other is some secondary packaging of web3j, which can reduce the workload of developers in some common scenarios. It is planned to support three chains, ETH (BSC, POLYGAN, etc.), SOL and TRON
## To explain this toolkit more intuitively, let me give you an example, for example.
I want the program to receive all the fields of a transaction message when a coin is received at a certain address. If I use this toolkit, I only need to write half a line of code by hand (not including the business operation after receiving the notification, because there is no escape from that, as with any tool).
If you add a scenario above where the program receives the appropriate transaction information when the number of coins received or sent to an address is within a certain range, then you don't need to change anything, just write another half line of code, and that half line of code is isolated and completely decoupled from the other half.
If you need to drop one of these two cases, just delete it or comment it out.
If there is a third case, then write another half line, one line at most.
Note: The half and one lines I mentioned above refer to the code that needs to be written by hand, not the total code, but the total code is not much, and each scenario corresponds to an implementation class that implements only two methods.
As a developer, you just need to pay attention to what kind of events to listen to, and nothing else needs to consume energy. For the transactions that call the contract, the developer may need to do secondary filtering, such as checking the logs to determine if the transaction is valid, parsing inputData to get more detailed information and making judgments etc.
## Running environment
JDK8+
## Documentation
[https://magician-io.com/chain](https://magician-io.com/chain)
## Example
### Importing dependencies
```xml
com.github.yuyenews
Magician-Web3
1.0.1
org.slf4j
slf4j-jdk14
1.7.12
```
### Create a scan task
Create a MonitorEvent
```java
import java.math.BigInteger;
/**
* Create an implementation class for EthMonitorEvent
*/
public class EthMonitorEventImpl implements EthMonitorEvent {
/**
* set filters
*
* Filter the transaction records according to these conditions and trigger the call method
* @return
*/
@Override
public EthMonitorFilter ethMonitorFilter() {
return EthMonitorFilter.builder()
.setMinValue(BigInteger.valueOf(100))
.setToAddress("0xasdasdasdasdasdasdasdasdas")
.setFunctionCode("0x1s8d5j6j");
}
/**
* This method is triggered when a transaction matching the above filter criteria is encountered
* @param transactionModel
*/
@Override
public void call(TransactionModel transactionModel) {
}
}
```
Start a monitoring task
```java
// Initialize the thread pool, the number of core threads must be >= the number of chains you want to scan, it is recommended to equal the number of chains to be scanned
EventThreadPool.init(1);
// Open a scan task, if you want to scan multiple chains, you can open multiple tasks,
// by copying the following code and modifying the corresponding configuration you can open a new task
MagicianBlockchainScan.create()
.setRpcUrl("https://data-seed-prebsc-1-s1.binance.org:8545/")
.setChainType(ChainType.ETH)
.setScanPeriod(5000)
.setScanSize(1000)
.setBeginBlockNumber(BigInteger.valueOf(24318610))
.addEthMonitorEvent(new EventOne())
.addEthMonitorEvent(new EventThree())
.addEthMonitorEvent(new EventTwo())
.start();
```
### Other utils
```java
// ABI codec
EthAbiCodec ethAbiCodec = MagicianWeb3.getEthBuilder().getEthAbiCodec();
// Encode the function as inputData
String inputData = ethAbiCodec.getInputData("mint",
new Address("0xqwasdasd"),
new Utf8String("https://asdasdasdsadasd.json")
);
// Get the function's signature
String funcCode = ethAbiCodec.getFunAbiCode("mint",
new Address("0xqwasdasd"),
new Utf8String("https://asdasdasdsadasd.json")
);
// Decode inputData into raw data
List result = ethAbiCodec.decoderInputData("0xasdasdas00000000adasd",
new TypeReference(){},
new TypeReference(){}
);
// ------------------------ More, if you are interested, you can visit our official website for more information ----------------------
Web3j web3j = Web3j.build(new HttpService(""));
String privateKey = "";
EthHelper ethHelper = MagicianWeb3.getEthBuilder().getEth(web3j, privateKey);
BigInteger amount = ethHelper.balanceOf(fromAddress);
ethHelper.transfer(
toAddress,
BigDecimal.valueOf(1),
Convert.Unit.ETHER
);
EthContract ethContract = MagicianWeb3.getEthBuilder().getEthContract(web3j, privateKey);
List result = ethContract.select(
contractAddress,
ethAbiCodec.getInputData(
"balanceOf",
new Address(toAddress)),
new TypeReference() {}
);
System.out.println(result.get(0).getValue());
ethContract.sendRawTransaction(
fromAddress,
contractAddress,
ethAbiCodec.getInputData(
"transfer",
new Address(toAddress),
new Uint256(new BigInteger("1000000000000000000"))
)
);
```