Skip to content

McashWeb

Ngo Nghia edited this page Jul 10, 2019 · 3 revisions

McashWeb

McashWeb Object

const mcashWeb = new McashWeb({
    fullHost: 'https://mainnet.mcash.network',
    privateKey: 'your private key'
  }
)

address

Helper object that allows you to convert between hex / base58 and private key representations of a address. Note: If you wish to convert generic data to hexadecimal strings, please use the function McashWeb.toHex.

McashWeb.address.toHex("MJ3DhnJbVYiV8P3eUUSzTbkuatR56zYPw2")

result = "326F377BC42F76D7ED3ABB54A2EF6FF24FCF7F7CA3"

toMatoshi

Helper function that converts a value in Mcash to Matoshi. (1 Mcash = 100.000.000 Matoshi)

McashWeb.toMatoshi(12)

result = 1200000000

fromMatoshi

Helper function that converts a value in Matoshi to Mcash. (1 Mcash = 100.000.000 Matoshi)

McashWeb.fromMatoshi(12000000)

result = 0.12

isAddress

Helper function that checks if a given address is valid.

McashWeb.isAddress("MJ3DhnJbVYiV8P3eUUSzTbkuatR56zYPw2")

McashWeb.isAddress("326F377BC42F76D7ED3ABB54A2EF6FF24FCF7F7CA3")

return = true

createAccount

Helper function that generates a new private key + address combination. This account is not activated on the network.

McashWeb.createAccount();

McashWeb.mcash

Addresses & Accounts

getAccount

Parameters
Parameter Description Data Type
address Mcash address string
Example
mcashWeb.mcash.getAccount("M8TP6RutmttRY9BCUz7SydDZjhb7SWgSJy");
Output
{
  "address": "3206180c288e08db62aa1772ba30434517d8f2a7ec",
  "balance": 3969000000000,
  "create_time": 1561711818000,
  "account_resource": {
    "latest_bandwidth_consume_time": 1561521600000,
    "latest_free_bandwidth_consume_time": 1561521600000,
    "latest_energy_consume_time": 1561521600000
  }
}

getAccountResources

Get the account's bandwidth and energy resources.

Parameters
Parameter Description Data Type
address Mcash address string
Example
mcashWeb.mcash.getAccountResources("M8TP6RutmttRY9BCUz7SydDZjhb7SWgSJy");
Output
{
  "free_bandwidth_limit": 10000,
  "total_bandwidth_limit": 43200000000,
  "total_bandwidth_weight": 640693,
  "total_energy_limit": 50000000000,
  "total_energy_weight": 502471
}

getBalance

Get the account Mcash balance.

Parameters
Parameter Description Data Type
address Mcash address string
Example
mcashWeb.mcash.getBalance("MJ3DhnJbVYiV8P3eUUSzTbkuatR56zYPw2");

Transactions

getTransaction

Parameters
Parameter Description Data Type
Transaction ID Transaction ID string
Example
mcashWeb.mcash.getTransaction("87692949ef7902fa22e6752003edad1b148ee1fcef1ee43f87e572912ee46379");
Output
{
  "ret": [
    {
      "contract_result": "OK"
    }
  ],
  "signature": [
    "e5fce8e9a14045424dc17386e94b55700910f1c3e569ef0c690259cadd49e0d27deecf390f27b89d7f16c60106680bdb823ad5a78ba30d9691012d1ab8ecffb001"
  ],
  "tx_id": "87692949ef7902fa22e6752003edad1b148ee1fcef1ee43f87e572912ee46379",
  "raw_data": {
    "contract": [
      {
        "parameter": {
          "value": {
            "amount": 19900000000,
            "owner_address": "32b436c96c6de1f50a160ed307317c275424dbe4f2",
            "to_address": "32613f654ba1a3e43cd3120c344e32e68c67a77f27"
          },
          "type_url": "type.googleapis.com/protocol.TransferContract"
        },
        "type": "TransferContract"
      }
    ],
    "ref_block_bytes": "01aa",
    "ref_block_hash": "ea2cf51f1b6cba3a",
    "expiration": 1562703801000,
    "timestamp": 1562703743117
  },
  "raw_data_hex": "0a0201aa2208ea2cf51f1b6cba3a40a8d59ec3bd2d5a69080112650a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412340a1532b436c96c6de1f50a160ed307317c275424dbe4f2121532613f654ba1a3e43cd3120c344e32e68c67a77f271880ce87914a708d919bc3bd2d"
}

getTransactionFromBlock

Parameters
Parameter Description Data Type
Block height Block height integer
Example
mcashWeb.mcash.getTransactionFromBlock(380087);

sendRawTransaction

Parameters
Parameter Description Data Type
signedTransaction The signed transaction object JSON
Example
mcashWeb.mcash.sendRawTransaction(signedTransaction);

sign

Signs a provided transaction object.

Parameters
Parameter Description Data Type
transaction Transaction object JSON
privateKey Private Key associated with the transaction string
Example
mcashWeb.mcash.sign(transaction, privateKey);

Query network

getBlock

Get block by height and id

Parameters
Parameter Description Data Type
Block Height Block Height integer
Example
mcashWeb.mcash.getBlock(380087);
Output
{
  "block_header": {
    "raw_data": {
      "number": 380087,
      "witness_address": "32dc2f806d9f3ab5ff6f09915c2db276f31db42922",
      "version": 1,
      "parent_hash": "000000000005ccb63951a126e6d4d7f1300d2a0749f609a17cd0a3f332616a84",
      "timestamp": 1562663040000,
      "tx_trie_root": "60dd83acf38e8099f4ae661b51c089a461ac02e7c513815fdb2e85a286e6bc4c"
    },
    "witness_signature": "f069027e7e3ff7cfd531af081260b6c5f25b8d4d5ef47abcd8507904a4d3e9114abdfcfff9622f15754ce687473943d435e3b349098300a2efff41e0fb85f9fd01"
  },
  "transactions": [
    {
      "ret": [
        {
          "contract_result": "OK"
        }
      ],
      "signature": [
        "529d1932c62ba4f683efceda32096b35ad6cc7ace4ef1b584cc5f6423168ea810de60cd9da9b6b8c5e1ef9527187a058148645c86a2cbbac12c65008634446a100"
      ],
      "tx_id": "53cd966bd4e5ebb1d3edfef36796401d562a58d85eb0402c297b99ac48f8cb47",
      "raw_data": {
        "contract": [
          {
            "parameter": {
              "value": {
                "amount": 20000000000,
                "owner_address": "321036355254b644cf3ca1fc758b7b498113fcc6d4",
                "to_address": "32234c905817f8e137b0b6fab61c1999be574266a1"
              },
              "type_url": "type.googleapis.com/protocol.TransferContract"
            },
            "type": "TransferContract"
          }
        ],
        "ref_block_bytes": "ccb6",
        "ref_block_hash": "3951a126e6d4d7f1",
        "expiration": 1562663097000,
        "timestamp": 1562663037706
      },
      "raw_data_hex": "0a02ccb622083951a126e6d4d7f140a8a5eaafbd2d5a69080112650a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412340a15321036355254b644cf3ca1fc758b7b498113fcc6d4121532234c905817f8e137b0b6fab61c1999be574266a1188090dfc04a708ad6e6afbd2d"
    }
  ],
  "block_id": "000000000005ccb7f3533db6e861cb811420fabd710b12907d229272736e46c2"
}

getBlockByHash

Get block information on the Full Node, specified by block header hash number.

Parameters
Parameter Description Data Type
Block id Block id string
Example
mcashWeb.mcash.getBlockByHash("000000000000004473dacbb6a6b3b0cabad78c340f3fccb1991b7714bd83b1e0");

getBlockByNumber

Gets a block by height.

Parameters
Parameter Description Data Type
Block Number Block height integer
Example
mcashWeb.mcash.getBlockByNumber(68);
Output

getBlockRange

Gets a block by height in range.

Parameters
Parameter Description Data Type
Starting Block Block number of the beginning of the range integer
Ending Block Block number of the end of the range integer
Example
mcashWeb.mcash.getBlockRange(68, 70);

getCurrentBlock

Get current block on the Full Node.

Example
mcashWeb.mcash.getCurrentBlock();

Smart Contracts

getContract

Get the details of the contract at the specified address.

Parameters
Parameter Description Data Type
address Smart Contract Address string
Example
mcashWeb.mcash.getContract("MLpZUn1YBb2zNwRSiYXq5zpWdXEyyKNrji");
Output

McashWeb.transactionBuilder

sendMcash

Create an unsigned Mcash transfer transaction.

Parameters
Parameter Description Data Type
to Address to send Mcash to string
amount Amount of Mcash (in Matoshi) to send integer
from Optional address that is transferring the Tokens. If left blank, will use the address associated with the private key string
Example
mcashWeb.transactionBuilder.sendMcash(to, amount, from);

mcashWeb.transactionBuilder.sendMcash("MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz", 100, "M9Fbdp9xRb66fct5zkgjN5FjGJ1X8WV9JT");

sendToken

Create an unsigned Mcash transfer transaction.

Parameters
Parameter Description Data Type
to Address to send Token to string
amount Amount of Token to send integer
tokenId Token id integer
from Optional address that is transferring the Tokens string
Example
mcashWeb.transactionBuilder.sendToken(to, amount, tokenId, from);

mcashWeb.transactionBuilder.sendToken("MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz", 100, 1000001, "M9Fbdp9xRb66fct5zkgjN5FjGJ1X8WV9JT");

freezeBalance

Allows users to freeze their Mcash balance to grant ENERGY or BANDWIDTH to either themselves or other addresses. Creates an unsigned freeze Mcash transaction.

Parameters
Parameter Description Data Type
amount Amount of Mcash (in Matoshi) to freeze integer
duration Length in Days to freeze Mcash for. Minimum of 3 days integer
resource Resource that you're freezing Mcash in order to obtain. Must be either "BANDWIDTH" or "ENERGY" string
ownerAddress (optional) Address of the owner of the Mcash to be frozen (defaults to caller's default address) string
receiverAddress (optional) Address of other user receiving the resource string
Example
mcashWeb.transactionBuilder.freezeBalance(amount, duration, resource, ownerAddress, receiverAddress);

mcashWeb.transactionBuilder.freezeBalance(McashWeb.toMatoshi(100), 3, "ENERGY", "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz");
mcashWeb.transactionBuilder.freezeBalance(McashWeb.toMatoshi(100), 3, "ENERGY", "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz", "M9Fbdp9xRb66fct5zkgjN5FjGJ1X8WV9JT");

unfreezeBalance

Creates an unsigned unfreeze Mcash transaction.

Parameters
Parameter Description Data Type
resource Resource that you're freezing Mcash in order to obtain. Must be either "BANDWIDTH" or "ENERGY" string
ownerAddress (optional) Address of the owner of the Mcash to be frozen (defaults to caller's default address) string
receiverAddress (optional) Address of user in which the resource is being removed from, due to unfreeze string
Example
mcashWeb.transactionBuilder.unfreezeBalance(resource, ownerAddress, receiverAddress);

mcashWeb.transactionBuilder.unfreezeBalance("ENERGY", "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz");
mcashWeb.transactionBuilder.unfreezeBalance("ENERGY", "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz", "M9Fbdp9xRb66fct5zkgjN5FjGJ1X8WV9JT");

stake

Creates an unsigned stake Mcash transaction.

Parameters
Parameter Description Data Type
amount Amount of Mcash (in Matoshi) to stake integer
stakeDuration Length in Days to stake Mcash for. Always 3 days integer
ownerAddress (optional) Address of the owner of the Mcash to be staked (defaults to caller's default address) string
Example
mcashWeb.transactionBuilder.stake(amount, stakeDuration, ownerAddress);

mcashWeb.transactionBuilder.stake(McashWeb.toMatoshi(5000), 3, "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz");

withdrawBlockRewards

Creates an unsigned withdraw Mcash transaction.

Parameters
Parameter Description Data Type
ownerAddress (optional) Optional address to withdraw from string
Example
mcashWeb.transactionBuilder.stake(ownerAddress);

mcashWeb.transactionBuilder.stake("MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz");

applyForSR

Creates an unsigned create witness transaction.

Parameters
Parameter Description Data Type
witnessAddress Witness address string
ownerAddress Owner address string
url Url to witness string
Example
mcashWeb.transactionBuilder.applyForSR(witnessAddress, ownerAddress, url);

mcashWeb.transactionBuilder.applyForSR("M9Fbdp9xRb66fct5zkgjN5FjGJ1X8WV9JT", "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz", "https://mcash.network");

vote

Creates an unsigned vote transaction.

Parameters
Parameter Description Data Type
voteAddress Witness address string
ownerAddress(option) Owner address string
Example
mcashWeb.transactionBuilder.vote(witnessAddress, ownerAddress);

mcashWeb.transactionBuilder.vote("M9Fbdp9xRb66fct5zkgjN5FjGJ1X8WV9JT", "MF7uTJWM8HMWPTWhtoopQBjXR5NmoW45Gz");