// contract/ThisTest.sol
contract ThisTest {
function getContractAddress() constant returns (address){
return this;
}
function () constant returns (address){
return this;
}
}
// test/ThisTest.js
contract('ThisTest', function(accounts) {
var eth = web3.eth;
var minerAddress = eth.accounts[0];
var sellerAddress = eth.accounts[1];
var buyerAddress = eth.accounts[2];
var users = [minerAddress, sellerAddress, buyerAddress];
var getBalance = Promise.promisify(eth.getBalance);
var sendTransaction = Promise.promisify(eth.sendTransaction);
it(" 'this' should return contract address ", function(done) {
var thisTest = ThisTest.at(ThisTest.deployed_address);
sendTransaction({from:buyerAddress, to: thisTest.address, value: 1 * Math.pow(10, 18)}).then(function(test){
console.log("ThisTest contract address is " + thisTest.deployed_address);
console.log("ThisTest contract address is " + thisTest.address);
console.log("Solidity function() return address is " + test);
//assert.equal(thisTest.address, test, "contract address should be matched, but not matched!");
}).then(function(){
thisTest.getContractAddress.call().then(function(test2){
console.log("ThisTest contract address is " + thisTest.address);
console.log("getContractAddress return address is " + test2);
//assert.equal(thisTest.address, test2, "contract address should be matched, but not matched!");
done()
}).catch(done)
}).catch(done)
});
});
// then
$ truffle test test/ThisTest.js
// return
$ truffle test test/ThisTest.js
Using environment test.
Compiling contracts...
Contract: ThisTest
ThisTest contract address is 0x9de4202e79ac256507c23f25d5ec9098743297f9
ThisTest contract address is 0x9de4202e79ac256507c23f25d5ec9098743297f9
Solidity function() return address is 0x0f401f0f3e49cb7217aa4262121bd2024bc637b8353556c7c521eb2decf7bf87
ThisTest contract address is 0x9de4202e79ac256507c23f25d5ec9098743297f9
getContractAddress return address is 0x9de4202e79ac256507c23f25d5ec9098743297f9
✓ 'this' should return contract address
1 passing (7s)
// check what is "0x0f401f0f3e49cb7217aa4262121bd2024bc637b8353556c7c521eb2decf7bf87"
eth.getTransaction("0x0f401f0f3e49cb7217aa4262121bd2024bc637b8353556c7c521eb2decf7bf87")
{
blockHash: "0x73f76be29278fff5739af8f339b5182a7fba991d32526aab1904eb78b03546ff",
blockNumber: 6609,
from: "0x318e67c441a2c664600a1d16b98f89345b89c2f6",
gas: 90000,
gasPrice: 20000000000,
hash: "0x0f401f0f3e49cb7217aa4262121bd2024bc637b8353556c7c521eb2decf7bf87",
input: "0x",
nonce: 39,
to: "0x9de4202e79ac256507c23f25d5ec9098743297f9",
transactionIndex: 0,
value: 1000000000000000000
}
Per specification of web3 lib, sendTransaction returns transaction hash. If you want the return value from the function, you need to use call.
thanks, I have mistaken
If you want the return value from the function, you need to use call
If you use
.call(), the data will not be saved but you will get the return value. The typical case is to call the function with.call()the first time to see if the execution went good, and then call again the function without the.call().
Most helpful comment
Per specification of web3 lib, sendTransaction returns transaction hash. If you want the return value from the function, you need to use call.