mirror of https://github.com/status-im/web3.js.git
changed contract API
This commit is contained in:
parent
36ca47f11c
commit
684c495981
|
@ -452,24 +452,42 @@ var addEventsToContract = function (contract, desc, address) {
|
|||
* outputs: [{name: 'd', type: 'string' }]
|
||||
* }]; // contract abi
|
||||
*
|
||||
* var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
|
||||
* var MyContract = web3.eth.contract(abi); // creation of contract prototype
|
||||
*
|
||||
* myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
|
||||
* myContract.call().myMethod('this is test string param for call'); // myMethod call (explicit)
|
||||
* myContract.transact().myMethod('this is test string param for transact'); // myMethod transact
|
||||
* var contractInstance = new MyContract('0x0123123121');
|
||||
*
|
||||
* @param address - address of the contract, which should be called
|
||||
* @param desc - abi json description of the contract, which is being created
|
||||
* contractInstance.myMethod('this is test string param for call'); // myMethod call (implicit, default)
|
||||
* contractInstance.call().myMethod('this is test string param for call'); // myMethod call (explicit)
|
||||
* contractInstance.transact().myMethod('this is test string param for transact'); // myMethod transact
|
||||
*
|
||||
* @param abi - abi json description of the contract, which is being created
|
||||
* @returns contract object
|
||||
*/
|
||||
var contract = function (abi) {
|
||||
|
||||
var contract = function (address, desc) {
|
||||
// return prototype
|
||||
if(Object.prototype.toString.call(abi) === '[object Array]' && arguments.length === 1) {
|
||||
return Contract.bind(this, abi);
|
||||
|
||||
// depreacted: auto initiate contract
|
||||
} else {
|
||||
|
||||
console.warn('Initiating a contract like this is deprecated please use var Contract = eth.contract(abi); new Contract(address); instead.');
|
||||
|
||||
return new Contract(arguments[1], arguments[0]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function Contract(abi, address) {
|
||||
console.log('address', address);
|
||||
console.log('abi', abi);
|
||||
|
||||
// workaround for invalid assumption that method.name is the full anonymous prototype of the method.
|
||||
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous
|
||||
// prototype, so we make it so as a workaround.
|
||||
// TODO: we may not want to modify input params, maybe use copy instead?
|
||||
desc.forEach(function (method) {
|
||||
abi.forEach(function (method) {
|
||||
if (method.name.indexOf('(') === -1) {
|
||||
var displayName = method.name;
|
||||
var typeName = method.inputs.map(function(i){return i.type; }).join();
|
||||
|
@ -479,12 +497,12 @@ var contract = function (address, desc) {
|
|||
|
||||
var result = {};
|
||||
addFunctionRelatedPropertiesToContract(result);
|
||||
addFunctionsToContract(result, desc, address);
|
||||
addEventRelatedPropertiesToContract(result, desc, address);
|
||||
addEventsToContract(result, desc, address);
|
||||
addFunctionsToContract(result, abi, address);
|
||||
addEventRelatedPropertiesToContract(result, abi, address);
|
||||
addEventsToContract(result, abi, address);
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = contract;
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -35,7 +35,7 @@
|
|||
]
|
||||
}];
|
||||
|
||||
var contract;
|
||||
var myContract;
|
||||
|
||||
function createExampleContract() {
|
||||
// hide create button
|
||||
|
@ -43,8 +43,10 @@
|
|||
document.getElementById('source').innerText = source;
|
||||
|
||||
// create contract
|
||||
var address = web3.eth.sendTransaction({data: web3.eth.compile.solidity(source)});
|
||||
contract = web3.eth.contract(address, desc);
|
||||
var address = web3.eth.sendTransaction({data: web3.eth.compile.solidity(source)}),
|
||||
Contract = web3.eth.contract(desc);
|
||||
|
||||
myContract = new Contract(address);
|
||||
document.getElementById('call').style.visibility = 'visible';
|
||||
}
|
||||
|
||||
|
@ -53,7 +55,7 @@
|
|||
var param = parseInt(document.getElementById('value').value);
|
||||
|
||||
// call the contract
|
||||
var res = contract.call().multiply(param);
|
||||
var res = myContract.call().multiply(param);
|
||||
document.getElementById('result').innerText = res.toString(10);
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
]
|
||||
}];
|
||||
|
||||
var contract;
|
||||
var myContract;
|
||||
|
||||
function createExampleContract() {
|
||||
// hide create button
|
||||
|
@ -43,8 +43,10 @@
|
|||
document.getElementById('source').innerText = source;
|
||||
|
||||
// create contract
|
||||
var address = web3.eth.sendTransaction({data: web3.eth.compile.solidity(source)});
|
||||
contract = web3.eth.contract(address, desc);
|
||||
var address = web3.eth.sendTransaction({data: web3.eth.compile.solidity(source)}),
|
||||
Contract = web3.eth.contract(desc);
|
||||
|
||||
myContract = new Contract(address);
|
||||
document.getElementById('call').style.visibility = 'visible';
|
||||
}
|
||||
|
||||
|
@ -54,7 +56,7 @@
|
|||
var param2 = parseInt(document.getElementById('value2').value);
|
||||
|
||||
// call the contract
|
||||
var res = contract.call().multiply([param, param2]);
|
||||
var res = myContract.call().multiply([param, param2]);
|
||||
document.getElementById('result').innerText = res.toString(10);
|
||||
}
|
||||
|
||||
|
|
|
@ -180,24 +180,42 @@ var addEventsToContract = function (contract, desc, address) {
|
|||
* outputs: [{name: 'd', type: 'string' }]
|
||||
* }]; // contract abi
|
||||
*
|
||||
* var myContract = web3.eth.contract('0x0123123121', abi); // creation of contract object
|
||||
* var MyContract = web3.eth.contract(abi); // creation of contract prototype
|
||||
*
|
||||
* myContract.myMethod('this is test string param for call'); // myMethod call (implicit, default)
|
||||
* myContract.call().myMethod('this is test string param for call'); // myMethod call (explicit)
|
||||
* myContract.transact().myMethod('this is test string param for transact'); // myMethod transact
|
||||
* var contractInstance = new MyContract('0x0123123121');
|
||||
*
|
||||
* @param address - address of the contract, which should be called
|
||||
* @param desc - abi json description of the contract, which is being created
|
||||
* contractInstance.myMethod('this is test string param for call'); // myMethod call (implicit, default)
|
||||
* contractInstance.call().myMethod('this is test string param for call'); // myMethod call (explicit)
|
||||
* contractInstance.transact().myMethod('this is test string param for transact'); // myMethod transact
|
||||
*
|
||||
* @param abi - abi json description of the contract, which is being created
|
||||
* @returns contract object
|
||||
*/
|
||||
var contract = function (abi) {
|
||||
|
||||
var contract = function (address, desc) {
|
||||
// return prototype
|
||||
if(Object.prototype.toString.call(abi) === '[object Array]' && arguments.length === 1) {
|
||||
return Contract.bind(this, abi);
|
||||
|
||||
// depreacted: auto initiate contract
|
||||
} else {
|
||||
|
||||
console.warn('Initiating a contract like this is deprecated please use var Contract = eth.contract(abi); new Contract(address); instead.');
|
||||
|
||||
return new Contract(arguments[1], arguments[0]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function Contract(abi, address) {
|
||||
console.log('address', address);
|
||||
console.log('abi', abi);
|
||||
|
||||
// workaround for invalid assumption that method.name is the full anonymous prototype of the method.
|
||||
// it's not. it's just the name. the rest of the code assumes it's actually the anonymous
|
||||
// prototype, so we make it so as a workaround.
|
||||
// TODO: we may not want to modify input params, maybe use copy instead?
|
||||
desc.forEach(function (method) {
|
||||
abi.forEach(function (method) {
|
||||
if (method.name.indexOf('(') === -1) {
|
||||
var displayName = method.name;
|
||||
var typeName = method.inputs.map(function(i){return i.type; }).join();
|
||||
|
@ -207,12 +225,12 @@ var contract = function (address, desc) {
|
|||
|
||||
var result = {};
|
||||
addFunctionRelatedPropertiesToContract(result);
|
||||
addFunctionsToContract(result, desc, address);
|
||||
addEventRelatedPropertiesToContract(result, desc, address);
|
||||
addEventsToContract(result, desc, address);
|
||||
addFunctionsToContract(result, abi, address);
|
||||
addEventRelatedPropertiesToContract(result, abi, address);
|
||||
addEventsToContract(result, abi, address);
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = contract;
|
||||
|
||||
|
|
Loading…
Reference in New Issue