Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample code to deploy smart contract on QuarkChain testnet using quarkchain-web3.js
// Sample code to deploy smart contract on QuarkChain testnet using quarkchain-web3.js
// https://github.com/QuarkChain/quarkchain-web3.js
// Contract code is taken from https://testnet.quarkchain.io/contract
// The code can run in the browser console while on http://testnet.quarkchain.io/contract (not https)
// MetaMask is required to sign the transaction and you need testnet tokens in your account
var abi = [{"constant":true,"inputs":[],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];
var byteCode = "0x608060405234801561001057600080fd5b5060405161045938038061045983398101806040528101908080518201929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610089929190610090565b5050610135565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100d157805160ff19168380011785556100ff565b828001600101855582156100ff579182015b828111156100fe5782518255916020019190600101906100e3565b5b50905061010c9190610110565b5090565b61013291905b8082111561012e576000816000905550600101610116565b5090565b90565b610315806101446000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806342cbb15c1461005c578063a413686214610087578063cfae3217146100f0575b600080fd5b34801561006857600080fd5b50610071610180565b6040518082815260200191505060405180910390f35b34801561009357600080fd5b506100ee600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610188565b005b3480156100fc57600080fd5b506101056101a2565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561014557808201518184015260208101905061012a565b50505050905090810190601f1680156101725780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600043905090565b806001908051906020019061019e929190610244565b5050565b606060018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561023a5780601f1061020f5761010080835404028352916020019161023a565b820191906000526020600020905b81548152906001019060200180831161021d57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061028557805160ff19168380011785556102b3565b828001600101855582156102b3579182015b828111156102b2578251825591602001919060010190610297565b5b5090506102c091906102c4565b5090565b6102e691905b808211156102e25760008160009055506001016102ca565b5090565b905600a165627a7a72305820b65144ba1d967908bb1a2d47f6e1c39f81b666ce2776d5ee9791692038a1b0b30029";
// Use http://localhost:38391 to deploy contract on a local cluster
var testnetEndpoint = "http://jrpc.testnet.quarkchain.io:38391";
// web3 object comes from MetaMask
QuarkChain.injectWeb3(web3, testnetEndpoint);
var contractClass = web3.qkc.contract(abi);
var contractInstance = contractClass.new({data: byteCode, 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.
}
});
@lcinacio

This comment has been minimized.

Copy link

@lcinacio lcinacio commented Sep 13, 2018

It works when I run it in the Chrome console, but not in nodejs. See below how I modified your example:

// Sample code to deploy smart contract on QuarkChain testnet using quarkchain-web3.js
// https://github.com/QuarkChain/quarkchain-web3.js
// Contract code is taken from https://testnet.quarkchain.io/contract
// The code can run in the browser console while on the https://testnet.quarkchain.io/contract page
// MetaMask is required to sign the transaction and you need testnet tokens in your account

import QuarkChain from 'quarkchain-web3';
import Web3 from 'web3';
const web3 = new Web3();

var abi = [{"constant":true,"inputs":[],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];
var byteCode = "0x608060405234801561001057600080fd5b5060405161045938038061045983398101806040528101908080518201929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610089929190610090565b5050610135565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100d157805160ff19168380011785556100ff565b828001600101855582156100ff579182015b828111156100fe5782518255916020019190600101906100e3565b5b50905061010c9190610110565b5090565b61013291905b8082111561012e576000816000905550600101610116565b5090565b90565b610315806101446000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806342cbb15c1461005c578063a413686214610087578063cfae3217146100f0575b600080fd5b34801561006857600080fd5b50610071610180565b6040518082815260200191505060405180910390f35b34801561009357600080fd5b506100ee600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610188565b005b3480156100fc57600080fd5b506101056101a2565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561014557808201518184015260208101905061012a565b50505050905090810190601f1680156101725780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600043905090565b806001908051906020019061019e929190610244565b5050565b606060018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561023a5780601f1061020f5761010080835404028352916020019161023a565b820191906000526020600020905b81548152906001019060200180831161021d57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061028557805160ff19168380011785556102b3565b828001600101855582156102b3579182015b828111156102b2578251825591602001919060010190610297565b5b5090506102c091906102c4565b5090565b6102e691905b808211156102e25760008160009055506001016102ca565b5090565b905600a165627a7a72305820b65144ba1d967908bb1a2d47f6e1c39f81b666ce2776d5ee9791692038a1b0b30029";

var testnetEndpoint = "http://jrpc.testnet.quarkchain.io:38391";
const PRIVATEKEY = 'myprivatekey'

// web3 object comes from MetaMask
QuarkChain.injectWeb3(web3, testnetEndpoint);
web3.qkc.setPrivateKey(PRIVATEKEY);
var contractClass = web3.qkc.contract(abi);
var contractInstance = contractClass.new({data: byteCode, 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.

}
});

@ninjaahhh

This comment has been minimized.

Copy link

@ninjaahhh ninjaahhh commented Sep 14, 2018

@lcinacio thanks for flagging this out. this PR should fix it QuarkChain/quarkchain-web3.js#6

@syntrust

This comment has been minimized.

Copy link

@syntrust syntrust commented Nov 1, 2019

more attributes should be provided to the transaction:
var contractInstance = contractClass.new({data: byteCode, gas: 1000000, gasPrice: 1000000000,fromFullShardKey: "0x00010000", toFullShardKey: "0x00010000", networkId:"0xfc", transferTokenId: "0x8bb0",gasTokenId: "0x8bb0"}, ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment