Magician-Scanning是一個(gè)用Java開發(fā)的掃描區(qū)塊鏈的工具包,當(dāng)我們?cè)诔绦蛑行枰恍┕δ軙r(shí),它可以派上用場(chǎng),比如說(shuō)。
- 當(dāng)一個(gè)地址收到ETH時(shí),程序中的一個(gè)方法會(huì)被自動(dòng)觸發(fā),這個(gè)交易會(huì)被傳入該方法。
- 當(dāng)一個(gè)合約的某個(gè)功能被調(diào)用時(shí)(比如
ERC20
轉(zhuǎn)賬),它會(huì)自動(dòng)觸發(fā)程序中的一個(gè)方法,并將這個(gè)交易傳遞給這個(gè)方法。它甚至可以只在代幣被轉(zhuǎn)移到指定地址時(shí)被觸發(fā)。- 當(dāng)程序需要保留一個(gè)區(qū)塊高度開始以來(lái)的所有交易記錄時(shí),也可以使用這個(gè)工具包。
一、導(dǎo)入依賴
<dependency>
<groupId>com.github.yuyenews</groupId>
<artifactId>Magician-Scanning</artifactId>
<version>1.0.6</version>
</dependency>
<!-- This is the logging package, you must have it or the console will not see anything, any logging package that can bridge with slf4j is supported -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.12</version>
</dependency>
二、創(chuàng)建一個(gè)監(jiān)聽器
監(jiān)聽器可以創(chuàng)建多個(gè),根據(jù)你的需求分別設(shè)置監(jiān)聽條件
ETH(BSC, POYGAN 等)監(jiān)聽器
/**
* 創(chuàng)建一個(gè)類,實(shí)現(xiàn) EthMonitorEvent接口 即可
*/
public class EventDemo implements EthMonitorEvent {
/**
* 篩選條件,如果遇到了符合條件的交易,會(huì)自動(dòng)觸發(fā) call方法
* 這些條件都是 并且的關(guān)系,必須要同時(shí)滿足才行
* 如果不想根據(jù)某個(gè)條件篩選,直接不給那個(gè)條件設(shè)置值就好了
* 這個(gè)方法如果不實(shí)現(xiàn),或者返回null, 那么就代表監(jiān)聽任意交易
*/
@Override
public EthMonitorFilter ethMonitorFilter() {
return EthMonitorFilter.builder()
.setFromAddress("0x131231249813d334C58f2757037F68E2963C4crc") // 篩選 fromAddress 發(fā)送的交易
.setToAddress("0x552115849813d334C58f2757037F68E2963C4c5e") // 篩選 toAddress 或 合約地址 收到的交易
.setMinValue(BigInteger.valueOf(1)) // 篩選發(fā)送的主鏈幣數(shù)量 >= minValue 的交易
.setMaxValue(BigInteger.valueOf(10)) // 篩選發(fā)送的主鏈幣數(shù)量 <= maxValue 的交易
.setInputDataFilter( // 根據(jù)inputData篩選
InputDataFilter.builder()
.setFunctionCode(ERC20.TRANSFER.getFunctionCode()) // 函數(shù)簽名(被調(diào)用的合約內(nèi)的某方法), 支持任意函數(shù),這里的枚舉只是一部分標(biāo)準(zhǔn)的合約函數(shù)
.setTypeReferences( // 此方法的參數(shù)列表(僅類型)
new TypeReference<Address>(){},
new TypeReference<Uint256>(){}
)
.setValue("0x552115849813d334C58f2757037F68E2963C4c5e", null)// 篩選第幾個(gè)參數(shù) = 什么值
);
}
/**
* 如果遇到了符合上面條件的交易,就會(huì)觸發(fā)這個(gè)方法
* transactionModel.getEthTransactionModel() 是一個(gè)交易對(duì)象,內(nèi)部包含hash,value,from,to 等 所有的數(shù)據(jù)
*/
@Override
public void call(TransactionModel transactionModel) {
String template = "EventOne 掃描到了, hash:{0}, from:{1}, to: {2}, input: {3}";
template = template.replace("{0}", transactionModel.getEthTransactionModel().getBlockHash());
template = template.replace("{1}", transactionModel.getEthTransactionModel().getFrom());
template = template.replace("{2}", transactionModel.getEthTransactionModel().getTo());
template = template.replace("{3}", transactionModel.getEthTransactionModel().getInput());
System.out.println(template);
}
}
InputDataFilter 詳解
如果你想監(jiān)控,某合約內(nèi)的某函數(shù) 被調(diào)用的交易
public EthMonitorFilter ethMonitorFilter() {
return EthMonitorFilter.builder()
.setToAddress("0x552115849813d334C58f2757037F68E2963C4c5e") // 合約地址
.setInputDataFilter( // 根據(jù)inputData篩選
InputDataFilter.builder()
.setFunctionCode("0xadasasdf") // 被調(diào)用的函數(shù)編碼(inputData前十位)
);
}
如果 有一個(gè)合約[0x552115849813d334C58f2757037F68E2963C4c5e],
里面有一個(gè)函數(shù)是 transferFrom(address from, address to, uint256 amount)
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-798773.html
你想 實(shí)現(xiàn)一個(gè)監(jiān)控:如果有人用這個(gè)合約里的這個(gè)函數(shù),將代幣轉(zhuǎn)給[0x552115849813d334C58f2757037F68E2963C4c5e]時(shí)
,就觸發(fā) Monitor
事件,那么你可以這樣寫文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-798773.html
public EthMonitorFilter ethMonitorFilter() {
return EthMonitorFilter.builder()
.setToAddress("0x552115849813d334C58f2757037F68E2963C4c5e") // 合約地址
.setInputDataFilter( // 根據(jù)inputData篩選
InputDataFilter.builder()
.setFunctionCode(ERC20.TRANSFER_FROM.getFunctionCode()) // 被調(diào)用的函數(shù)編碼(inputData前十位)
.setTypeReferences( // 此方法的參數(shù)列表(僅類型)
new TypeReference<Address>(){}, // 第一個(gè)參數(shù)的類型
new TypeReference<Address>(){}, // 第二個(gè)參數(shù)的類型
new TypeReference<Uint256>(){} // 第三個(gè)參數(shù)的類型
)
.setValue(null, "0x552115849813d334C58f2757037F68E2963C4c5e", null)// 篩選第二個(gè)參數(shù)(to) = 0x552115849813d334C58f2757037F68E2963C4c5e
);
}
三、開啟一個(gè)掃塊任務(wù)
// 初始化線程池,核心線程數(shù)必須 >= 掃塊的任務(wù)數(shù)量 + 重試策略的數(shù)量
EventThreadPool.init(1);
// 開啟一個(gè)掃塊任務(wù),如果你想掃描多個(gè)鏈,那么直接拷貝這段代碼,并修改配置即可
MagicianBlockchainScan.create()
.setRpcUrl(
EthRpcInit.create()
.addRpcUrl("https://data-seed-prebsc-1-s1.binance.org:8545")
) // 節(jié)點(diǎn)的RPC地址
.setScanPeriod(5000) // 間隔多久,掃描下一個(gè)區(qū)塊
.setBeginBlockNumber(BigInteger.valueOf(24318610)) // 從哪個(gè)塊高開始掃描
.addEthMonitorEvent(new EventOne()) // 添加 監(jiān)聽事件
.addEthMonitorEvent(new EventTwo()) // 添加 監(jiān)聽事件
.addEthMonitorEvent(new EventThree()) // 添加 監(jiān)聽事件
.start();
到了這里,關(guān)于Java掃描區(qū)塊鏈的工具包|Java掃塊|監(jiān)聽token轉(zhuǎn)賬的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!