Let's say a wallet which has 3 different ERC20 tokens(token A, token B, token C) and that wallet approved my contract that my contract can transfer any of three tokens by transferFrom function of the token but now I've an issue is that if my contract wants to call transferFrom function of token B then my contract needs to create the instance of token B but how could my contract create the instance of token B because my contract doesn't have the address of token B.So I want to ask that is there any way in solidity to get the address of particular contract?
1 Answers
0
votes
Well, I used the below piece of code and worked fine for me.
window.onload = function() {
if (typeof BrowserSolc == 'undefined') {
console.log("You have to load browser-solc.js in the page. We recommend using a <script> tag.");
throw new Error();
}
const source = "pragma solidity ^0.4.0; contract Mortal { /* Define variable owner of the type address */ address owner; /* This function is executed at initialization and sets the owner of the contract */ function Mortal() { owner = msg.sender; } /* Function to recover the funds on the contract */ function kill() { if (msg.sender == owner) selfdestruct(owner); }}contract Greeter is Mortal { /* Define variable greeting of the type string */ string greeting; /* This runs when the contract is executed */ function Greeter(string _greeting) public { greeting = _greeting; } /* Main function */ function greet() constant returns (string) { return greeting; }}";
BrowserSolc.loadVersion("soljson-v0.4.24+commit.e67f0147.js", function(compiler) {
optimize = 1;
var result = compiler.compile(source, optimize);
// Bytecode
var bytecode = result.contracts[':Greeter'].bytecode;
// ABI
var abi = result.contracts[':Greeter'].interface;
var MyContract = web3.eth.contract(JSON.parse(abi));
var myContractInstance = MyContract.new({
from: web3.eth.accounts[0],
data: "0x" + bytecode,
gas: '4700000'
}, function(err, contract) {
if (err) {
console.log(err)
return
}
if (contract.address) {
console.log("MyContract deployed at address :" + contract.address)
} else {
console.log("MyContract is waiting to be mined at transaction hash:" + contract.transactionHash);
}
})
});
}