# 链上交互

DxChain3.0兼容以太坊EVM，兼容以太坊的所有的RPC-API和相关SDK。同时定制DPoS-RPC-API模块，进行与DPoS（节点、委托投票）相关的操作。

* [RPC List](https://eth.wiki/json-rpc/api)
* web3系列
* ethers.js
* ......

## RPC

* [Ethereum RPC List](https://eth.wiki/json-rpc/api)

例如：

```shell
curl -s -H 'content-type:application/json' -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":67}' https://testnet-http.dxchain.com

// result: {"jsonrpc":"2.0","id":67,"result":"0xd5ba"}
```

* [DPoS RPC List](/dxchain-wiki/zhong-wen-she-qu/kai-fa-zhe-wen-dang/kai-fa-zhe/lian-shang-jiao-hu/dpos-api.md)

例如：

```shell
curl -s -H 'content-type:application/json' -d '{"jsonrpc":"2.0","method":"dpos_getCurrentEpochValidators","params":[],"id":67}' https://testnet-http.dxchain.com

// result : {"jsonrpc":"2.0","id":67,"result":["0xd7a53c68ad615906484a17fed3ebe298ad389fec","0x49d29e0a3d0ae38ed0ad4732e08327a39bbbfa03","0x8ff49bc9fbda10dbe327ec06306761183cf7ef11","0x061c4df9c6a879976054219aa50ffc8001543573","0x8e66c772a03b954a7d589b2ebc13a73818b52cbc","0x88e4af4cda236d2fa322d014e31c7c717856a17e","0x08c5ac6769b036567ba7259313fa2eb084f83218","0xe560a742cafff57e4da0f37760fadc555b216fa0","0xc737aaaa44399a32ba7e18222ae38a9609085142","0x839966b579ddcac9780314c4d8a17e5d845a632c","0xdf6a8f4604765679590a5657ffade32979d8a89d","0xcf4540fcd7aa16d70007d4fedc1806846d961ce6","0x66dc74cbc72580f79bd558a98935c38ce304e92f","0x43d3436a1381405e868a9a2ce534a6d8160b9b07","0xe5a85f72cb33ebce190fcca63cb8dd963bba37a3","0x008ad4a13f28d2c9ab418de05ee021354be9ad97","0x159abbf49d2ccbac440900038c3028f01d4b8ed6","0x577aaf7227a269f39cbdc2ab9545b7d2f17cab4e","0x1a42a60ac116ec8288b0b75a673d778437f77bb8","0x678abf6ced79a791e28fd6200b2fd781227f05b4","0xa9d3a05f3568e04c65b368693439cd8c0586a51c"]}
```

## SDK

例如 [Web3.js](https://web3js.readthedocs.io/en/v1.7.3/)：

### 创建账户

```javascript
const Web3Accounts = require('web3-eth-accounts')

const generateAccount = async () => {
    let newAccount =  new Web3Accounts().create()
    console.log(newAccount);
}

generateAccount()

// result
{
  address: '0x62381730D25394Aa543eDBd980d5547B038eb575',
  privateKey: 'address private key',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt]
}
```

### 最新区块

```javascript
const Web3 = require('web3');
const web3 = new Web3("https://testnet-rpc.dxchain.com");

const queryLatestBlock = async() => {
    let rst = await web3.eth.getBlock("latest");
    console.log(rst);
}

queryLatestBlock()

// result
{
  baseFeePerGas: '0x0',
  difficulty: '2',
  extraData: '0xd883010200846765746888676f312e31332e34856c696e7578000000000000006d7140e46507f6257e0d2273fc7b5070eb8f4b7cee9cc6496b484f227fda1fa367e991e80f71c2c9af73a1454b925f6c6b1bc60f0c9a0c830d61f13cd956a0b801',
  gasLimit: 40000000,
  gasUsed: 0,
  hash: '0x52b94e1cb072604680c478db7c56f3a0de4aee1c8d7af00eaaf83ed654a26812',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  miner: '0x159abbF49D2cCBac440900038c3028F01D4b8Ed6',
  mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
  nonce: '0x0000000000000000',
  number: 55275,
  parentHash: '0x60b6e6cdf9c9f267e8331299c6dee9bae5957865cd750e5a994a1714b5d54559',
  receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
  size: 610,
  stateRoot: '0x6f8b755b458db243ff85baa3538eda611aa59fbcb91b84a05d74f5adba6bcf70',
  timestamp: 1649921944,
  totalDifficulty: '110463',
  transactions: [],
  transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421',
  uncles: []
}
```

### 发起交易

```javascript
const Web3 = require('web3');
const web3 = new Web3("https://testnet-rpc.dxchain.com");


const sendValue = async () => {
    // unlock account
    web3.eth.accounts.wallet.add("private key")

    let rst = await web3.eth.sendTransaction({
        from: "0x1c0e8eaf42ec8d4010e960313248d2af95be7d34",
        to: "0x0000000000000000000000000000000000000000",
        value: web3.utils.toWei("100",'ether'),
        gas: 21000
    })
    console.log(rst);
}

sendValue()

// result
{
  blockHash: '0x003cf6889f2a8f4b14f925efb1c7cc341e508902fb442f672ac17f7419428632',
  blockNumber: 55575,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  effectiveGasPrice: '0x3b9aca00',
  from: '0x1c0e8eaf42ec8d4010e960313248d2af95be7d34',
  gasUsed: 21000,
  logs: [],
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  status: true,
  to: '0x0000000000000000000000000000000000000000',
  transactionHash: '0x21d2e4ebf08f3c25b94d912c05828db3d01aee998e71ce4d1df649006ca9db3e',
  transactionIndex: 0,
  type: '0x0'
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dxchain.gitbook.io/dxchain-wiki/zhong-wen-she-qu/kai-fa-zhe-wen-dang/kai-fa-zhe/lian-shang-jiao-hu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
