數字貨幣交易所/合約跟單/秒合約/永續合約/量化合約/合約量化系統開發詳細策略及原始碼

開發MrsFu123發表於2023-04-23

  web3.js是一個JavaScript API庫。要讓DApp在以太坊上執行,我們可以使用web3.js庫提供的web3物件。web3.js透過RPC呼叫與本地節點通訊,它可以與任何公開RPC層的以太坊節點一起使用。web3包含eth物件-web3.eth(用於與以太坊區塊鏈互動)和shh物件-web3.shh(用於與Whisper互動)


  As a new combination of technologies,blockchain integrates P2P networks,consensus algorithms,asymmetric encryption,smart contracts and other new technologies.It is a traceable blockchain data structure built through transparent and trusted rules in the peer-to-peer network(also known as distributed network,peer-to-peer)environment,with distributed peer-to-peer,chained data blocks,forgery and tamper prevention,traceability Typical characteristics of transparency,trustworthiness,and high reliability


  <dependency>


  <groupId>io.github.conflux-chain</groupId>


  <artifactId>conflux.web3j</artifactId>


  <version>1.1.0</version>


  </dependency>


  import conflux.web3j.Account;


  import conflux.web3j.Cfx;


  import conflux.web3j.AccountManager;


  import conflux.web3j.contract.ContractCall;


  import conflux.web3j.contract.abi.DecodeUtil;


  import conflux.web3j.types.Address;


  import org.web3j.abi.FunctionReturnDecoder;


  import org.web3j.abi.TypeReference;


  import org.web3j.abi.datatypes.*;


  import org.web3j.abi.datatypes.generated.Uint256;


  import org.web3j.crypto.Bip39Wallet;


  import org.web3j.crypto.Credentials;


  import org.web3j.crypto.WalletUtils;


  import org.web3j.abi.Utils;


  import java.io.File;


  import java.math.BigInteger;


  import java.util.Arrays;


  import java.util.List;


  import java.util.Optional;


  //API地址


  Cfx cfx=Cfx.create(");


  //與合約建立連線


  ContractCall contract=new ContractCall(cfx,new Address("cfxtest:acdhpa29xz0095w6rxbhv36yhjeub58vzp64s0swpk"));


  {


  //單引數返回


  String balance=contract.call("balanceOf",new Address("cfxtest:aasg762e552jwmp3pk73ve9j9tszezz9xu5abab3xb").getABIAddress()).sendAndGet();


  BigInteger outBalance=DecodeUtil.decode(balance,Uint256.class);


  System.out.println("contract minter:"+outBalance);


  }


  {


  //動態引數返回


  String test=contract.call("tokens",new Uint256(0),new Uint256(11)).sendAndGet();


  List<Type>values=FunctionReturnDecoder.decode(test,Utils.convert(Arrays.asList(new TypeReference<Uint256>(){},new TypeReference<DynamicArray<Uint256>>(){})));


  System.out.println("tokens 0:"+(values.get(0)).getValue());


  List<Uint256>list=(List<Uint256>)values.get(1).getValue();


  for(Uint256 v:list){


  System.out.println("tokens array:"+v.getValue());


  }


  }


  {


  //多返回引數


  String test=contract.call("Test").sendAndGet();


  List<Type>values=FunctionReturnDecoder.decode(test,Utils.convert(Arrays.asList(new TypeReference<Utf8String>(){},new TypeReference<Utf8String>(){})));


  System.out.println((values.get(0)).getValue());


  System.out.println((values.get(1)).getValue());


  }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70016819/viewspace-2947753/,如需轉載,請註明出處,否則將追究法律責任。

相關文章