axis-web3

0.3.3 • Public • Published

console web3.js

This is the Axis compatible JavaScript API which base on ethereum/web3.js version with v0.20.6 latest maintenance release .It is used by gero client as embed js that can interract with the Axis blockchain. It alse can be as a reference for dApps to interract with the Axis blockchain.

Differences from web3.js

1. value of unit

The minimum unit of Axis is ta, and one axis is equivalent to 10 to the 9th power ta.

 
var unitMap = {
    'noether':      '0',
    'ta':           '1',
    'kta':          '1000',
    'Kta':          '1000',
    'babbage':      '1000',
    'femtoether':   '1000',
    'mta':          '1000000',
    'Mta':          '1000000',
    'lovelace':     '1000000',
    'picoether':    '1000000',
    'gta':          '1000000000',
    'Gta':          '1000000000',
    'shannon':      '1000000000',
    'nanoaxis':     '1000000000',
    'nano':         '1000000000',
    'szabo':        '1000000000000',
    'microaxis':    '1000000000000',
    'micro':        '1000000000000',
    'finney':       '1000000000000000',
    'milliaxis':    '1000000000000000',
    'milli':        '1000000000000000',
    'axis':         '1000000000000000000',
    'AXIS':         '1000000000000000000',
    'kaxis':        '1000000000000000000000',
    'gaxis':        '1000000000000000000000',
    'maxis':        '1000000000000000000000000',
    'gaxis':        '1000000000000000000000000000',
};
 
/**
 * Takes a number of Ta and converts it to any other axis unit.
 *
 *
 * @method fromTa
 * @param {Number|String} number can be a number, number string or a HEX of a decimal
 * @param {String} unit the unit to convert to, default ether
 * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
 */
var fromTa = function (number, unit{
    var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit));
 
    return isBigNumber(number) ? returnValue : returnValue.toString(10);
};
 
/**
 * Takes a number of a unit and converts it to ta.
 * @method toTa
 * @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal
 * @param {String} unit the unit to convert from, default ether
 * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number
 */
var toTa = function (number, unit{
    var returnValue = toBigNumber(number).times(getValueOfUnit(unit));
 
    return isBigNumber(number) ? returnValue : returnValue.toString(10);
};
 

2. address

All the address is showed as base58 string

 
   /**
    * Convert a byte array to a base58 string
    *
    * @method bytesToBase58
    * @param {Array} bytes
    * @return {String} the base58 string
    */
   var bytesToBase58 = function (bytes{
       return base58.encode(bytes);
   };
   
   /**
    * Convert a base58 string to a byte array
    *
    * @method base58ToBytes
    * @param {String} the base58 string
    * @return {Array} bytes
    */
   var base58ToBytes = function (bs58{
       return base58.decode(bs58);
   }
 
 
    /**
    * Checks if the given string is strictly an address
    *
    * @method isStrictAddress
    * @param {String} address the given base58 adress
    * @return {Boolean}
    */
   var isStrictAddress = function (address{
   
       if (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{84,103}$/i.test(address)) {
           bytes = base58ToBytes(address)
           if (bytes.length !== 128) {
               return false;
           }
           return true;
       } else {
           return false;
       }
   };
 

3.contract data

create or call smart contract,the address type params must convert to once address

 
SolidityCoder.prototype.opParams = function (types, params,rand,axis,dy{
    var solidityTypes = this.getSolidityTypes(types);
    var addressParams = solidityTypes.map(function (solidityTypeindex) {
        if (solidityType instanceof SolidityTypeAddress){
            return  solidityType.address(params[index], types[index],false)
        }else{
            return
        }
    })[0]||'';
    if (addressParams){
        if (utils.isArray(addressParams)){
        }else{
            addressParams = [addressParams];
        }
    }else {
        addressParams = [];
    }
 
    var convertResult =  axis.convertAddressParams(rand,addressParams,dy);
    rand = convertResult.rand;
    if (addressParams.length >0 ){
        var addrMap = convertResult.addr;
         var convertParams =  solidityTypes.map(function (solidityTypeindex) {
            return  solidityType.convertAddress(params[index], types[index],addrMap)
        });
         params = convertParams;
    }
    var result ={};
    result.params = params;
    result.rand = rand;
    return result;
 
};
 

the create smart contract data:

 
    var rand ="0x";
    var argsResult = opArgs(this.abi,args,rand,this.axis,false);
    args = argsResult.params;
    rand = argsResult.rand;
    var bytes = encodeConstructorParams(this.abi, args);
    options.data += bytes;
    var prefix = encodeConstructorPrefix(this.abi,args,rand);
    options.data = prefix +options.data.substr(2);
        

the call smart contract data:

 
    var rand = utils.bytesToHex(utils.base58ToBytes(this._address).slice(0,16));
    var convertResult = coder.opParams(this._inputTypes,args,rand,this._axis,dy);
    args = convertResult.params;
    options.data = coder.addressPrefix(this._inputTypes,args,rand) + this.signature()+ coder.encodeParams(this._inputTypes, args);
 

5. sendTransaction params

The method of SendTransaction Json params add cy、dy、catg,tkt key. The dy only be used when call contract that means whether to regenerate a one-time address. The cy is the currency unit of the transaction, default is axis ,the catg is the Ticket category and the tkt is the Ticket Id.

   {
    from: axis.accounts[0],
    to: axis.accounts[1],
    value: 1000,
    gas: 30000,
    gasPrice: 5,
    data: [],
    cy: 'axis',
    dy: false,
    catg: '',
    tkt: ''
   }

6. transaction info

The showed transaction info add Zero knowledge proof

 
/**
 * Formats the output of a transaction to its proper values
 *
 * @method outputTransactionFormatter
 * @param {Object} tx
 * @returns {Object}
*/
var outputTransactionFormatter = function (tx){
    if(tx.blockNumber !== null)
        tx.blockNumber = utils.toDecimal(tx.blockNumber);
    if(tx.transactionIndex !== null)
        tx.transactionIndex = utils.toDecimal(tx.transactionIndex);
    tx.nonce = utils.toDecimal(tx.nonce);
    tx.gas = utils.toDecimal(tx.gas);
    tx.gasPrice = utils.toBigNumber(tx.gasPrice);
    tx.value = utils.toBigNumber(tx.value);
    if (tx.stx.Desc_O){
        tx.stx.Desc_O_Ins=tx.stx.Desc_O.Ins;
        var O_Outs=[];
        tx.stx.Desc_O.Outs.forEach(function(out){
            var out_O ={};
            if (utils.toBigNumber(out.Addr)!=0) {
                out_O.Addr = out.Addr;
            }
            if (out.Asset.Tkn){
                out_O.Currency = utils.bytesToString(utils.hexToBytes(utils.fromDecimal(utils.toBigNumber(out.Asset.Tkn.Currency))));
                out_O.Value = utils.toBigNumber(out.Asset.Tkn.Value);
            }
            if (out.Asset.Tkt){
                out_O.Category = utils.bytesToString(utils.hexToBytes(utils.fromDecimal(utils.toBigNumber(out.Asset.Tkt.Category))));
                out_O.TktId = out.Asset.Tkt.Value;
            }
            out_O.Memo = out.Memo;
            O_Outs.push(out_O);
        });
        tx.stx.Desc_O_Outs = O_Outs;
        delete  tx.stx.Desc_O;
    }
    if (tx.stx.Desc_Z){
        tx.stx.Desc_Z_Ins=tx.stx.Desc_Z.Ins;
        tx.stx.Desc_Z_Outs = tx.stx.Desc_Z.Outs;
        delete  tx.stx.Desc_Z;
    }
    return tx;
};
 

6. add isMineOAddr

Add a method in axis.js ,determine if it is your own one-time address

 var isMineOAddr = new Method({
        name: 'isMineOAddr',
        call: 'axis_isMineOAddr',
        params: 1,
        inputFormatter: [formatters.inputAddressFormatter]
    });

7. add convertAddressParams

Add a method in axis.js ,convert the address to once address with the given rand;

 
    var convertAddressParams = new Method({
        name: 'convertAddressParams',
        call: 'axis_convertAddressParams',
        params: 3,
    });
    

7. add getFullAddress

Add a method in axis.js ,get the full address with the vm short address;

 
    var getFullAddress = new Method({
        name: 'getFullAddress',
        call: 'axis_getFullAddress',
        params: 1,
    });
    

Building (gulp)

npm run-script build

License

LGPL-3.0+ © 2015 Contributors

Readme

Keywords

Package Sidebar

Install

npm i axis-web3

Weekly Downloads

2

Version

0.3.3

License

LGPL-3.0

Unpacked Size

336 kB

Total Files

77

Last publish

Collaborators

  • lqhlqy