DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

Author Dex_DQT

In the previous chapter we completed setting up the environment and implementing the functions for generating secret keys and obtaining wallet balances. In this chapter we will introduce some common Api interfaces and implement the transfer function.

Get Address Hrp

The code is as follows:

    // 获取当前链的地址前缀
    String hrp = await _web3.getAddressHrp();
    print("hrp: $hrp");

The result is shown below:
1644851968(1)

Get Gas Price

The code is as follows:

    // 获取当前gas的价格
    BigInt gasPrice = await _web3.gasPrice();
    print("gasPrice: ${AmountUtil.convertVonToLat(gasPrice)}");

The result is shown below:
1644852187(1)

Get PlatON Block Number

The code is as follows:

 // 获取当前块高
    BigInt blockNumber = await _web3.platonBlockNumber();

The result is shown below:

1644852458(1)

Get Block by Number

You can check the Andorid chapter II, which has the same block height: 6533257
The code is as follows:

  PlatonBlockInfo blockInfo = await _web3.getBlockByNumber(BigInt.from(6533257));

The result is shown below:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

As you can see the same results as in the Android one.

Get Block Transaction Count by Number

Code is as follows:

 BigInt txCount = await _web3.getBlockTransactionCountByNumber(BigInt.from(6533257));

Since it is an empty block, the number of transactions is 0,
As shown below:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

Get Block Transaction Count By Hash

The code is as follows:

  BigInt txCount = await _web3.getBlockTransactionCountByHash("0x0561ab627d3053c486a552e594f6b3f40f7acc2fd107866169feb34de346129b");

We already know that the transaction hash for block 6533257 is: 0x0561ab627d3053c486a552e594f6b3f40f7acc2fd107866169feb34de346129b
As shown below:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

Get Block Transaction Count By Hash

Code:

  BigInt txCount = await _web3.getTransactionCount("lat1tgu6pts6nhmneu5zhqly3rc83r6y6ecfmde03e");

I got the number of transactions with the address: lat1tgu6pts6nhmneu5zhqly3rc83r6y6ecfmde03e.
The result is shown below:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

The number of transactions is 22, and the structure of the blockchain is as follows:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

The number of transactions does not increase with the number of receiving records, and this account has 20 receiving records, so the value obtained by our interface is 22. (42 – 20)

Complete the first transfer

The secret ke of the wallet I used is :a4ac816da1ab40f805d026009247002f47c8c0a9af95b35ca9741c576466e1a8
And the wallet adress is: lat1tgu6pts6nhmneu5zhqly3rc83r6y6ecfmde03e.
We’ve already known that the balance of the wallet is: 224.79987 LAT.
Now we send 10 LATs to: lat1zrq89dhle45g78mm4j8aq3dq5m2shpu56ggc6e.

Code:

    //  加载秘钥
    Credentials credentials = Credentials.createPrivate(
        "a4ac816da1ab40f805d026009247002f47c8c0a9af95b35ca9741c576466e1a8");

    // 获取钱包的交易次数作为nonce
    BigInt nonce = await _web3.getTransactionCount(credentials.address);
    // 获取当前gasPrice
    BigInt gasPrice = await _web3.gasPrice();
    // 构建交易对象
    RawTransaction rawTransaction = RawTransaction(
        nonce,
        gasPrice,
        BigInt.from(21000),
        "lat1zrq89dhle45g78mm4j8aq3dq5m2shpu56ggc6e",
        BigInt.parse("10000000000000000000"),
        "");
    // 签名
    Uint8List signData = TransactionEncoder.signMessageWithChainId(
        rawTransaction, credentials, BigInt.from(210309));
    // 转换为16进制字符串
   String hexSignData = Numeric.bytesToHex(signData, withPrefix: true);

  // 发送交易数据
   String txHash = await _web3.sendRawTransaction(hexSignData);

The returned transaction hash is as follows:

1644855572(1)

0x7a3b3f842cb8796fdfdd09c0e174e7c762042f1d2554f8b73b6ba8c1908e18e7

The transaction details can be seen from the blockchain browser as follows:

DQtech Column | Learn from DEX about PlatON Application Development – Flutter (II)

Well, that’s it for this chapter.

In the next chapter, I will show how to build a master page.

The URL of github: https://github.com/DQTechnology/Platon_DevGuideProject

Publisher:PlatOnWorld-Kita,Please indicate the source for forwarding:https://platonworld.org/technology/dqtech-column-learn-from-dex-about-platon-application-development-flutte-ii/

Like (0)
Previous April 11, 2022 10:16
Next April 11, 2022 15:53

相关推荐

Leave a Reply

Please Login to Comment