Skip to content

mechanizm/zilliqa

Repository files navigation

Zilliqa - Zilliqa Blockchain Ruby Library

Table of Contents

Requirement

  • Ruby(2.5.3)

Installation

Add this line to your application's Gemfile:

gem 'zilliqa'

And then execute:

$ bundle

Or install it yourself as:

$ gem install zilliqa

Zilliqa KeyTool

Generate A new address

private_key = Zilliqa::Crypto::KeyTool.generate_private_key
public_key = Zilliqa::Crypto::KeyTool.get_public_key_from_private_key(private_key)
address = Zilliqa::Crypto::KeyTool.get_address_from_private_key(private_key)

Validate an address

address = '2624B9EA4B1CD740630F6BF2FEA82AAC0067070B'
Zilliqa::Util::Validator.address?(address)

Validate checksum address

checksum_address = '0x4BAF5faDA8e5Db92C3d3242618c5B47133AE003C'
Zilliqa::Util::Validator.checksum_address?(checksum_address)

Transaction

provider = Zilliqa::Jsonrpc::Provider.new('https://dev-api.zilliqa.com')
to_addr = 'zil1lpw9fc8p4tse55r85fa37gscnkxf6xq5ahe8uj'
pub_key = '032cfec301c57acc2a4b18f47247687a1ec51e61336a7d5936e455b7dab3ae712e'
testnet = 21_823_489

tx_params = {
  version: testnet,
  amount: '0',
  to_addr: to_addr,
  gas_price: '1000',
  gas_limit: 1,
  sender_pub_key: pub_key
}

wallet = Zilliqa::Account::Wallet.new(provider)
transaction = Zilliqa::Account::Transaction.new(tx_params, provider)

wallet.add_by_private_key(private_key)

tx = wallet.sign(transaction)
tx.submit!

Wallet

provider = Zilliqa::Jsonrpc::Provider.new('https://dev-api.zilliqa.com')
wallet = Zilliqa::Account::Wallet.new(provider)
wallet.add_by_private_key(private_key)
wallet.transfer('zil1lpw9fc8p4tse55r85fa37gscnkxf6xq5ahe8uj', 10 ** 12)
Successfull output
{
"ContractAddress"=>"1e366b36e5a17dec83c46f19d8d6b43434bd1dbb",
 "Info"=>"Contract Creation txn, sent to shard",
 "TranID"=>"411c1108800ac85118fcd9a44568d208276dcbdd5287c99119c69167912f344a"
}

Smart Contract

Create Smart contract

private_key = "e19d05c5452598..."
provider = Zilliqa::Jsonrpc::Provider.new('https://dev-api.zilliqa.com')
wallet = Zilliqa::Account::Wallet.new(provider)
address = wallet.add_by_private_key(private_key)

factory = Zilliqa::Contract::ContractFactory.new(provider, wallet)

contract = factory.new_contract(TEST_CONTRACT, [
  {
    vname: 'owner',
    type: 'ByStr20',
    value: '0x124567890124567890124567890124567890',
  },
],
ABI,
)

Deploy contract

gas_limit = TEST_CONTRACT.bytes.size + ABI.to_s.bytes.size
gas_price = 10 ** 12 # 1 zil
testnet_ver = 21_823_489
pub_key = '032cfec301...'

deploy_params = Zilliqa::Contract::DeployParams.new(nil, testnet_ver, nil, gas_price, gas_limit, pub_key)
tx, deployed = contract.deploy(deploy_params)

assert tx.confirmed?
assert deployed.deployed?
assert_equal Zilliqa::Contract::CONTRACT_STATUSES[:deployed], deployed.status

assert /[A-F0-9]+/ =~ contract.address

# call a deployed contract
call_tx = deployed.call(
      'setHello',
      [
        { vname: 'msg', type: 'String', value: 'Hello World!' },
      ],
      {
        version: Zilliqa::Util.pack(8, 8),
        amount: 0,
        gasPrice: 1000,
        gasLimit: 1000
      })


receipt = call_tx.receipt

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published