Magician-ContractsTools is a powerful Java toolkit designed to streamline interactions with smart contracts. Whether you're querying data or executing write operations, this tool simplifies the process for developers working with blockchain networks like Ethereum (BSC, Polygon), Solana, and Tron.
Key Features
- Prebuilt Contract Templates: ERC20, ERC721, and ERC1155 templates for rapid deployment.
- Custom Function Support: Easily call non-standard contract functions.
- InputData Encoding/Decoding: Tools for ABI-compliant data handling.
- ETH Utilities: Query balances and transfer native tokens.
- Multi-Chain Compatibility: Supports ETH-based chains, Solana, and Tron.
Getting Started
Dependency Installation
Add the following to your pom.xml:
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician-ContractsTools</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency> Smart Contract Interactions
Querying Contracts
String contractAddress = "0x...";
EthContractUtil ethContractUtil = EthContractUtil.builder(web3j);
List<Type> result = ethContractUtil.select(
contractAddress,
EthAbiCodecTool.getInputData("balanceOf", new Address(toAddress)),
new TypeReference<Uint256>() {}
); Writing to Contracts
SendResultModel sendResultModel = ethContractUtil.sendRawTransaction(
senderAddress,
contractAddress,
privateKey,
new BigInteger("1200000"), // gasPrice
new BigInteger("800000"), // gasLimit
EthAbiCodecTool.getInputData("transfer", new Address(toAddress), new Uint256(amount))
); Prebuilt Template Examples
ERC20 Operations
Query:
BigInteger balance = erc20Contract.balanceOf("0x..."); Write:
SendResultModel result = erc20Contract.transfer(
recipientAddress,
amount,
callerAddress,
privateKey,
null, // Default gasPrice
null // Default gasLimit
); InputData Encoding/Decoding
// Encode
String inputData = EthAbiCodecTool.getInputData("transfer", new Address(toAddress), new Uint256(amount));
// Decode
List<Type> decoded = EthAbiCodecTool.decoderInputData(
inputData.substring(10),
new TypeReference<Address>() {},
new TypeReference<Uint256>() {}
); Native Token Utilities
EthHelper ethHelper = EthHelper.builder(web3j);
BigInteger balance = ethHelper.balanceOf(fromAddress);
TransactionReceipt receipt = ethHelper.transfer(
toAddress,
privateKey,
BigDecimal.valueOf(1),
Convert.Unit.ETHER
); ๐ Explore advanced blockchain tools for seamless development.
FAQ
1. What chains does Magician-ContractsTools support?
It currently supports ETH (including BSC, Polygon), Solana, and Tron, with plans to expand.
2. Can I use custom ABIs?
Yes! While templates are provided, custom contract functions are fully supported.
3. How are gas fees handled?
Default values are applied if gasPrice/gasLimit are omitted or set to null.
4. Is InputData decoding necessary?
Only if you need to parse encoded contract call data for debugging or analysis.
5. Where can I report issues?
Visit the official GitHub repository.