Coder Social home page Coder Social logo

quarkchain / quarkchain-web3.js Goto Github PK

View Code? Open in Web Editor NEW
23.0 11.0 15.0 332 KB

QuarkChain client library built around web3.js

Home Page: https://quarkchain.io

License: GNU Lesser General Public License v3.0

JavaScript 100.00%
quarkchain solidity contract ethereum js web3

quarkchain-web3.js's People

Contributors

jishankai avatar mask-pp avatar ninjaahhh avatar qcdll avatar qcgg avatar superman285 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

quarkchain-web3.js's Issues

deploy contract with quarkchain-web3.js in browser without metamask

I forked qcgg/quarkchain-web3-contract-example.js and used it in browser. With MetaMask it woked fine. However, for supporting more browsers and more friendly to noob, I need use it without MetaMask. If I don't have a server, what should I do?

Here is my code.

<html>
<head> 
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<script src="/javascripts/quarkchain-web3.js"></script> 
<-- [email protected]>
</head>
<body>
<script>
  //var Web3 = require('web3');
  web3 = new Web3();
  //web3.setProvider(new web3.providers.HttpProvider());
  web3.setProvider(new web3.providers.HttpProvider("http://jrpc.testnet.quarkchain.io:38391"));
  QuarkChain.injectWeb3(web3, "http://jrpc.testnet.quarkchain.io:38391");
  web3.qkc.setPrivateKey('0x27245C2FFBF81193F1C68FDD2FD87C962724488DC40510BBB526FF24401D5825');

  var abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_value","type":"uint256"}],"name":"burnFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"ownerAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"endDate","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"buyRate","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"},{"name":"tokenDecimals","type":"uint8"},{"name":"tokenOwnerAddress","type":"address"},{"name":"tokenBuyRate","type":"uint256"},{"name":"tokenEndDate","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"}];
  var bytecode = "0x60806040526002805460ff1916601217905534801561001d57600080fd5b50604051610af5380380610af58339810160409081528151602080840151838501516060860151608087015160a088015160c089015160025460ff16600a0a8802600381905533600090815260078952998a20559489018051979990989401969295919490939092610091928901906100eb565b5084516100a59060019060208801906100eb565b506002805460ff191660ff959095169490941790935560048054600160a060020a031916600160a060020a03939093169290921790915560055560065550610186915050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061012c57805160ff1916838001178555610159565b82800160010185558215610159579182015b8281111561015957825182559160200191906001019061013e565b50610165929150610169565b5090565b61018391905b80821115610165576000815560010161016f565b90565b610960806101956000396000f3006080604052600436106100da5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde0381146100df578063095ea7b31461016957806318160ddd146101a157806323b872dd146101c8578063313ce567146101f257806342966c681461021d57806370a082311461023557806379cc6790146102565780638f84aa091461027a57806395d89b41146102ab578063a9059cbb146102c0578063c24a0f8b146102e4578063cae9ca51146102f9578063dd62ed3e14610362578063fc37987b14610389575b600080fd5b3480156100eb57600080fd5b506100f461039e565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561012e578181015183820152602001610116565b50505050905090810190601f16801561015b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561017557600080fd5b5061018d600160a060020a036004351660243561042c565b604080519115158252519081900360200190f35b3480156101ad57600080fd5b506101b6610492565b60408051918252519081900360200190f35b3480156101d457600080fd5b5061018d600160a060020a0360043581169060243516604435610498565b3480156101fe57600080fd5b50610207610507565b6040805160ff9092168252519081900360200190f35b34801561022957600080fd5b5061018d600435610510565b34801561024157600080fd5b506101b6600160a060020a0360043516610588565b34801561026257600080fd5b5061018d600160a060020a036004351660243561059a565b34801561028657600080fd5b5061028f61066b565b60408051600160a060020a039092168252519081900360200190f35b3480156102b757600080fd5b506100f461067a565b3480156102cc57600080fd5b5061018d600160a060020a03600435166024356106d4565b3480156102f057600080fd5b506101b66106ea565b34801561030557600080fd5b50604080516020600460443581810135601f810184900484028501840190955284845261018d948235600160a060020a03169460248035953695946064949201919081908401838280828437509497506106f09650505050505050565b34801561036e57600080fd5b506101b6600160a060020a0360043581169060243516610809565b34801561039557600080fd5b506101b6610826565b6000805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156104245780601f106103f957610100808354040283529160200191610424565b820191906000526020600020905b81548152906001019060200180831161040757829003601f168201915b505050505081565b336000818152600860209081526040808320600160a060020a038716808552908352818420869055815186815291519394909390927f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925928290030190a350600192915050565b60035481565b600160a060020a03831660009081526008602090815260408083203384529091528120548211156104c857600080fd5b600160a060020a03841660009081526008602090815260408083203384529091529020805483900390556104fd84848461082c565b5060019392505050565b60025460ff1681565b3360009081526007602052604081205482111561052c57600080fd5b3360008181526007602090815260409182902080548690039055600380548690039055815185815291517fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca59281900390910190a2506001919050565b60076020526000908152604090205481565b600160a060020a0382166000908152600760205260408120548211156105bf57600080fd5b600160a060020a03831660009081526008602090815260408083203384529091529020548211156105ef57600080fd5b600160a060020a0383166000818152600760209081526040808320805487900390556008825280832033845282529182902080548690039055600380548690039055815185815291517fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca59281900390910190a250600192915050565b600454600160a060020a031681565b60018054604080516020600284861615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156104245780601f106103f957610100808354040283529160200191610424565b60006106e133848461082c565b50600192915050565b60065481565b6000836106fd818561042c565b15610801576040517f8f4ffcb10000000000000000000000000000000000000000000000000000000081523360048201818152602483018790523060448401819052608060648501908152875160848601528751600160a060020a03871695638f4ffcb195948b94938b939192909160a490910190602085019080838360005b8381101561079557818101518382015260200161077d565b50505050905090810190601f1680156107c25780820380516001836020036101000a031916815260200191505b5095505050505050600060405180830381600087803b1580156107e457600080fd5b505af11580156107f8573d6000803e3d6000fd5b50505050600191505b509392505050565b600860209081526000928352604080842090915290825290205481565b60055481565b6000600160a060020a038316151561084357600080fd5b600160a060020a03841660009081526007602052604090205482111561086857600080fd5b600160a060020a038316600090815260076020526040902054828101101561088f57600080fd5b50600160a060020a038083166000818152600760209081526040808320805495891680855282852080548981039091559486905281548801909155815187815291519390950194927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929181900390910190a3600160a060020a0380841660009081526007602052604080822054928716825290205401811461092e57fe5b505050505600a165627a7a723058202827fd9fcfaab34240c1a77ec0715a7c72dcf4a852b4a7f0f8ab1d91e2f46f000029";

  var contractClass = web3.qkc.contract(abi);
  var contractInstance = contractClass.new({data: bytecode, gasPrice: 1000000000, gas: 1000000}, function(err, contract) {
    if(!err) {
      // NOTE: The callback will fire twice!
      // Once the contract has the transactionId property set and once its deployed on an address.

      // e.g. check tx hash on the first call (transaction send)
      if(!contract.address) {
        console.log(contract.transactionId); // The id of the transaction, which deploys the contract
        // check address on the second call (contract deployed)
      } else {
        console.log(contract.address); // the contract address
        // Note that the returned "contractInstance" === "contract",
        // so the returned "contractInstance" object will also get the address set.       
      }
    } 
  });
</script>
</body>
</html>

I run the code in Safari and Chrome. All had the error as follows:
image
image

I would appreciate any suggestion!

Above all, when I used the sample code in server, I can only get the transactionId and no second callback for address but I can see the success of the deploy in testnet explorer, I need the contract address when I deploy it and tell the customers.
image
You can see I only get the txid back and then timeout. It's exactly same code besides I used require instead of import.

web3.qkc.getBalance() 和 sendTransaction() 两个函数无法调通

对应node节点版本:2.5.4
1、web3.qkc.getBalance()对应node节点中的eth_getBalance(),调用时node节点报错如下:

MASTER: File "/usr/local/lib/python3.6/dist-packages/jsonrpcserver/request.py", line 35, in handle_exceptions
MASTER: yield
MASTER: File "/usr/local/lib/python3.6/dist-packages/jsonrpcserver/async_request.py", line 21, in call
MASTER: result = await callable_(*(self.args or []), **(self.kwargs or {}))
MASTER: File "<decorator-gen-76>", line 2, in eth_getBalance
MASTER: File "/code/pyquarkchain/quarkchain/cluster/jsonrpc.py", line 351, in new_f
MASTER: res = await f(*args, **kwargs)
MASTER: File "/code/pyquarkchain/quarkchain/cluster/jsonrpc.py", line 1023, in eth_getBalance
MASTER: balance = account_branch_data.balance
MASTER: AttributeError: 'AccountBranchData' object has no attribute 'balance'

这个问题应该是node节点的问题。

我的调用代码如下:

web3.qkc.setPrivateKey('0x......')
var balance = web3.qkc.getBalance("0xB2ac5a...");
console.log(balance);

2、web3.qkc.sendTransaction()中缺少gasTokenIdtransferTokenId两个变量。我观察到这两个变量是固定的,自行填充后再次调用sendTransaction()函数,仍有问题,node节点报错如下:

SLAVE_S1: W0403 15:24:22.294277 shard_state.py:516] Failed to add transaction: <Transaction(673a)>: 'nonce' actual:1 target:0

我观察到node节点侧通过签名(v,r,s)解析出来的EvmTransaction.sender和我私钥对应的地址不匹配,应该是这个原因导致的。但不确定是web3还是node节点的问题。是否可以给出一个例子呢?下面是我调用的代码:

web3.qkc.setPrivateKey('0x......')

var data = {
    to:'0xb2ac5ac...',
    value: '0x4563918244f40000',
    fromFullShardId: '0x00055ca8',
    toFullShardId: '0x00055ca9',
    gas: '0x7530',
    gasPrice: '0x2540be400',
    gasTokenId: '0x8bb0',
    transferTokenId: '0x8bb0'
}

web3.qkc.sendTransaction(data, function(err, transactionId) {
  if (!err)
    console.log(transactionId);
});

SyntaxError: Unexpected identifier - While Running the node example of web3

➜ qkc_poc node compile-2.js
/Users/gaurav/Documents/295B_Code/qkc_poc/compile-2.js:1
(function (exports, require, module, __filename, __dirname) { import QuarkChain from 'quarkchain-web3';
^^^^^^^^^^

SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)

Upgrade underlying web3js to latest

current web3 dependency is ^0.20.3 (i.e. <0.21), while the latest has been stable after 1.0.0 (latest 1.2.1)

the latest version also has better support for pub/sub using subscribe endpoint instead of newFilter which is not supported yet in our JSONRPC

Create smart contract with quarkchain web3

I have built an app which aims to create an ICO smart contract with data included from form inputs. You can see the app here: https://jsfiddle.net/m81470rb/

We have not yet set up a server, but would like to test the frontend code. How should I implement it? I have looked at other examples, but cannot understand how the abi is sent off (and in fact what it does). If you could point me in a direction of what needs to be done for this contract to be created, I would be grateful. There is a js object which stores all of the form data, and which can be possibly accessed by web3.

guide to quarkchain-web3.js for the web

Hi,

I want to use the quarkchain-web3.js to create contracts for ICO projects through a web app. However, I run into a problem when installing it for use in a web app (clientside browser). Once the js is installed, how do I use it with a web app to send user inputs. That is how do I put it all together to be sent to cluster?

Thanks,
K

Add getBalances

Now, web3.qkc.getBalance only returns balance of QKC. Will we add getBalances to support native tokens as JRPC does?

web3.qkc.getTransactionReceipt.contractAddress is a qkc address

It's different in readme.

contractAddress : String - 20 Bytes - The contract address created without full shard id, if the transaction was a contract creation, otherwise null. To build a QKC contract address, append the toFullShardId used in the contract creation transaction. Normally you should use web3.qkc.contract(abi).new() which returns the QKC address in the callback.

Can't interact with Contract in browser

I deployed an ICO smart contract with abi that include:

{
    "constant": true,
    "inputs": [],
    "name": "totalSupply",
    "outputs": [
      {
        "name": "",
        "type": "uint256"
      }
    ],

In my HMTL file, I have the following javascript, I would expect this to return the totalSupply, but instead, I get "undefined".

ethAddr=QuarkChain.getEthAddressFromQkcAddress("0x13970840784104FC13664095aAb517f773dD47cd134140f7");
var MyContract = web3.qkc.contract(abi);
var myContractInstance = MyContract.at(contractAddr);
result = await myContractInstance.totalSupply();

Any ideas?

Add getTokenIdFromName

Shall we add a function like QuarkChain.getTokenIdFromName to make it easier when developing native token app?

gas or gasLimit in rawTx

The gas(gasLimit) param in rawTx is different when you use web3 or jrpc. It's 'gas' in quarkchain-web3 and 'gasLimit' in jrpc. Is it better that they are the same?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.