Programming Languages Caver Supports

Hi all,

This is the question I received from many folks: what programming languages does Caver supports? Here’s my answer: Javascript and Java.

caver-js

caver-js is Javascript implementation of Caver. For those of you who are accustomed to web3.js can quickly catch up how caver-js works since web3.js and caver-js share the same code base and syntax structure. Here’s a short code snippet demonstrating how to encode, sign, and send a transaction given an account.

const Caver = require('caver-js');
const caver = new Caver('https://api.baobab.klaytn.net:8651/');

const wallet = caver.klay.accounts.wallet;
const sender = wallet.add('0xYOUR_PRIVATE_KEY');

let tx = {
    type: 'VALUE_TRANSFER',
    from: sender.address,
    to: '0xRECIPIENT_ADDRESS',
    value: caver.utils.toPeb('1', 'KLAY'),
    gas: 300000
};

(async () => {
    // sign first and send later
    let signedTX = await caver.klay.accounts.signTransaction(tx, sender.privateKey);
    await caver.klay.sendSignedTransaction(signedTX.rawTransaction)
        .on('transactionHash', function(txhash) {
            console.log('hash first', txhash);
        })
        .on('receipt', function(receipt) {
            console.log('receipt later', receipt);
        })
        .on('error', function(err) {
            console.error('not good');
        });

    // alternatively, sign and send with one function call
    caver.klay.sendTransaction(tx).then((receipt) => {
        console.log(receipt);
    });
})();

You can find more about caver-js from here.

caver-java

caver-java is Java implementation of Caver. You can use this SDK for your Java projects including Android (dedicated build is available for Android). Here’s a simple value transfer example (written as a JUnit test case) using caver-java:

package com.klaytn.example.caver;

/* omitted import statements for brevity */

public class ValueTransferExample {
    private static final BigInteger GAS_LIMIT = BigInteger.valueOf(3000000L);
    private static final BigInteger GAS_PRICE = BigInteger.valueOf(25000000000L);
    private static final DefaultBlockParameter BLK_PARAM = DefaultBlockParameterName.LATEST;
    private static final int CHAIN_ID = ChainId.BAOBAB_TESTNET;

    private static Caver caver;

    private static KlayCredentials sender;

    @BeforeClass
    public static void setup() {
        caver = Caver.build("https://api.baobab.klaytn.net:8651/");
        sender = KlayCredentials.create("0xYOUR_PRIVATE_KEY");
    }

    private KlayTransactionReceipt.TransactionReceipt transfer(KlayCredentials from, KlayCredentials to, BigInteger value) throws IOException {
        BigInteger nonce = caver.klay().getTransactionCount(from.getAddress(), BLK_PARAM).send().getValue();
        TxTypeValueTransfer tx = TxTypeValueTransfer.createTransaction(
                nonce,
                GAS_PRICE,
                GAS_LIMIT,
                to.getAddress(),
                value,
                from.getAddress()
        );

        TransactionManager manager = new TransactionManager.Builder(caver, from)
                .setTransactionReceiptProcessor(new PollingTransactionReceiptProcessor(caver, 1000, 10))
                .setChaindId(CHAIN_ID)
                .build();

        return manager.executeTransaction(tx);
    }

    @Test
    public void VTTest() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
        BigInteger privKey = Keys.createEcKeyPair().getPrivateKey();
        // newly created recipient credential
        KlayCredentials recipient = KlayCredentials.create(privKey.toString(16));

        KlayTransactionReceipt.TransactionReceipt receipt;

        receipt = transfer(sender, recipient, Convert.toPeb("0.5", Convert.Unit.KLAY).toBigInteger());
        Assert.assertNotNull(receipt);
    }
}

You can find more about caver-java from here.

Other Languages

There are plans to implement Caver in different languages. However, we are always expecting to see community involvement in this area. If you have built an awesome SDK in languages we currently do not support (e.g., Python, Go, Ruby, you name it), please let us know or share it on here!

2개의 좋아요