Solidity
如何訪問由另一個智能合約建構子創建的令牌實例
我正在使用一個智能合約,它在其建構子中創建一個令牌實例:
contract Exemple{ Token public myToken function Exemple(){ myToken=Token(this); } }
我如何從合約外部訪問這個令牌實例(使用 truffle 控制台)?例如發出如下命令:
myToken.approve(address,value, {from: other_account})
希望我的問題有意義。這裡只是初學者…
您可以獲得如下合約實例:
使用 Web3:
// ------- Get Web3 instance -------------------------------------------------------------- var web3Client = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // ------- Get ABI of the contract --------------------------------------------------------- // #################### REPLACE it with your ABI ############################# var myContractABI = <contract ABI>; // ------- Get address of the contract ----------------------------------------------------- // #################### REPLACE it with your Address ############################# var myContractAddress = '<address of the contract from which it is deployed>'; // ------- Create contract client ---------------------------------------------------------- var myContract = web3Client.eth.contract(myContractABI); // ------- Create contract instance -------------------------------------------------------- var myContractInstance = myContract.at(myContractAddress); // ------- Invoke any method of the instance ------------------------------------------------ myContractInstance.myContractMethod();
從松露控制台:
myContract.deployed().then(function(instance) { myContractInstance = instance; }) myContractInstance.myMethod()