1. Magician-Scanning區(qū)塊鏈開發(fā)工具包

        聯(lián)合創(chuàng)作 · 2023-09-18 15:02

        Magician-Scanning 是一個用 Java 開發(fā)的掃描區(qū)塊鏈的工具包,當(dāng)我們在程序中需要一些功能時,它可以派上用場,比如說。

        • 當(dāng)一個地址收到ETH時,程序中的一個方法會被自動觸發(fā),這個交易會被傳入該方法。

        • 當(dāng)一個合約的某個功能被調(diào)用時(比如ERC20轉(zhuǎn)賬),它會自動觸發(fā)程序中的一個方法,并將這個交易傳遞給這個方法。它甚至可以只在代幣被轉(zhuǎn)移到指定地址時被觸發(fā)。

        • 當(dāng)程序需要保留一個區(qū)塊高度開始以來的所有交易記錄時,也可以使用這個工具包。

        它計劃支持三種鏈,ETH(BSC,POLYGON等),SOL和TRON

        初始化配置

        導(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>

        掃塊,監(jiān)聽交易事件

        創(chuàng)建一個監(jiān)聽器

        監(jiān)聽器 可以創(chuàng)建多個,根據(jù)你的需求 分別設(shè)置監(jiān)聽條件

        ETH(BSC, POYGAN 等)監(jiān)聽器

        /**
         * 創(chuàng)建一個類,實現(xiàn) EthMonitorEvent接口 即可
         */
        public class EventDemo implements EthMonitorEvent {
        
            /**
             * 篩選條件,如果遇到了符合條件的交易,會自動觸發(fā) call方法
             * 這些條件都是 并且的關(guān)系,必須要同時滿足才行
             * 如果不想根據(jù)某個條件篩選,直接不給那個條件設(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)// 篩選第幾個參數(shù) = 什么值
                        );
            }
        
            /**
             * 如果遇到了符合上面條件的交易,就會觸發(fā)這個方法
             * transactionModel.getEthTransactionModel() 是一個交易對象,內(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前十位)
                        );
        }

        如果 有一個合約[0x552115849813d334C58f2757037F68E2963C4c5e], 里面有一個函數(shù)是 transferFrom(address from, address to, uint256 amount)

        你想 實現(xiàn)一個監(jiān)控:如果有人用這個合約里的這個函數(shù),將代幣轉(zhuǎn)給[0x552115849813d334C58f2757037F68E2963C4c5e]時,就觸發(fā) Monitor事件,那么你可以這樣寫

        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>(){}, // 第一個參數(shù)的類型
                                                new TypeReference<Address>(){}, // 第二個參數(shù)的類型
                                                new TypeReference<Uint256>(){} // 第三個參數(shù)的類型
                                        )
                                        .setValue(null, "0x552115849813d334C58f2757037F68E2963C4c5e", null)// 篩選第二個參數(shù)(to) = 0x552115849813d334C58f2757037F68E2963C4c5e
                        );
        }

        SOL, TRON 鏈的掃塊正在開發(fā)中......

        開發(fā)中......
        

        開啟一個掃塊任務(wù)

        // 初始化線程池,核心線程數(shù)必須 >= 掃塊的任務(wù)數(shù)量 + 重試策略的數(shù)量
        EventThreadPool.init(1);
        
        // 開啟一個掃塊任務(wù),如果你想掃描多個鏈,那么直接拷貝這段代碼,并修改配置即可
        MagicianBlockchainScan.create()
                .setRpcUrl(
                      EthRpcInit.create()
                                .addRpcUrl("https://data-seed-prebsc-1-s1.binance.org:8545")
                                .addRpcUrl("https://data-seed-prebsc-2-s1.binance.org:8545")
                                .addRpcUrl("https://data-seed-prebsc-1-s2.binance.org:8545")
                ) // 節(jié)點的RPC地址
                .setScanPeriod(5000) // 每輪掃描的間隔
                .setBeginBlockNumber(BigInteger.valueOf(24318610)) // 從哪個塊高開始掃描
                .addEthMonitorEvent(new EventOne()) // 添加 監(jiān)聽事件
                .addEthMonitorEvent(new EventTwo()) // 添加 監(jiān)聽事件
                .addEthMonitorEvent(new EventThree()) // 添加 監(jiān)聽事件
                .start();
        
        // TODO 暫時不支持SOL和TRON, 正在開發(fā)中......

        文檔地址

        https://magician-io.com/chain

        瀏覽 24
        點贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        編輯 分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        編輯 分享
        舉報
          
          

            1. 三上悠亚ssni452被迫在线 | 中文字幕手机在线观看 | 军婚吸乳1v1 | 伊人激情婷婷 | 官场巨肉黄暴辣高h文 |