Jvav 调用 Conflux 智能合约

时间:2025-03-07 07:06:58

1. 加入 Conflux 库

        <dependency>
            <groupId>-chain</groupId>
            <artifactId>conflux.web3j</artifactId>
            <version>1.1.0</version>
        </dependency>
import conflux.;
import conflux.;
import conflux.;
import conflux.;
import conflux.;
import conflux.;

import org.;
import org.;
import org..*;
import org..Uint256;
import org..Bip39Wallet;
import org.;
import org.;
import org.;

import ;
import ;
import ;
import ;
import ;

1. 调用 view 、pure 标记函数

        // API地址
        Cfx cfx = ("/v2", 3, 1000);
        // 与合约建立连接
        ContractCall contract = new ContractCall(cfx, new Address("cfxtest:acdhpa29xz0095w6rxbhv36yhjeub58vzp64s0swpk"));

        {
            // 单参数返回
            String balance = ("balanceOf", new Address("cfxtest:aasg762e552jwmp3pk73ve9j9tszezz9xu5abab3xb").getABIAddress()).sendAndGet();
            BigInteger outBalance = (balance, );
            ("contract minter: " + outBalance);
        }


        {
            // 动态参数返回
            String test = ("tokens", new Uint256(0), new Uint256(11)).sendAndGet();
            List<Type> values = (test, ((new TypeReference<Uint256>() {}, new TypeReference<DynamicArray<Uint256>>() {})));
            ("tokens 0 : " + ((0)).getValue());
            List<Uint256> list = (List<Uint256>)(1).getValue();
            for( Uint256 v : list) {
                ("tokens array : " + ());
            }
        }

        {
            // 多返回参数
            String test = ("Test").sendAndGet();
            List<Type> values = (test, ((new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {} )));
            (((0)).getValue());
            (((1)).getValue());
        }

2. 调用普通公开逻辑函数

        // 调用者私钥
        String privateKey = "0x私钥";
        // 合约地址
        Address contractAddress = new Address("cfxtest:accb2fg8f6n9c92hszej0x1u88mprtbdhpp69v74a9");
        // 调用者地址
        Address recipient = new Address("cfxtest:aatc1wrjhetj6bnggdkhwd6hhybecrmpj228968j27");
        // API地址
        Cfx cfx = ("/v2", 3, 1000);

        // 创建调用账户
        Account account = (cfx, privateKey);
        // 设置调用设置
         opt = new ();
        // 调用测试链
        (1);
        
        // 发起合约
        try
        {
            // 发起合约
            String txHash = (opt, contractAddress, "DT_GenerateNFT", ());

            // 返回64位开头0x8的哈希交易地址, 具体内容可以在 / 输入返回哈希搜到
            ("tx hash: " + txHash);
        }
        catch (Exception e)
        {
            // 调用失败
            (());
        }

3. 获取交易信息返回值 - 函数返回较慢

    // 订单转换
    private static Call convert(Transaction tx) {

        Call call = new Call();
        (());
        (());
        if (().isPresent()) {
            (().get());
        }
        (());
        (());
        (());
        (());
        (());
        return call;
    }

    // 获取交易信息返回值 - 函数返回较慢
    private static String getTXHashReturnValue(Cfx cfx, String txHash) throws Exception {

        Transaction tx = (txHash).sendAndGet().get();
        Receipt receipt = (txHash);

        Call call = (tx);
        BigInteger execEpoch = ().subtract();
        StringResponse response = (StringResponse)(call, new Epoch[] { (execEpoch) }).sendWithRetry();
        if (() != null)
        {
            throw new Exception((().getData()));
        }
        return ();
    }

4. 新建地址

        Cfx cfx = ("/v2", 3, 1000);
        BigInteger epoch = ().sendAndGet();
        ("Current epoch: " + epoch);
        AccountManager accountManager = new AccountManager(1);

        String path = "d:\\2";
        Bip39Wallet wallet = WalletUtils.generateBip39Wallet("1234567678",new File(path));
        ("助"+());
        // 使用密码和助记词让账户解锁
        Credentials credentials = WalletUtils.loadBip39Credentials("", ());
        // 获取账户地址
        String address1 = ();
        (address1);
        // 获取公钥
        String publicKey = ().getPublicKey().toString(16);
        ("公"+publicKey);
        // 获取私钥
        String privateKey = ().getPrivateKey().toString(16);
        ("私"+privateKey);
        // 获取地址
        Optional<Address> c = (privateKey,"12345678");
        (().getAddress());