Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我没有看到我的智能合约进入区块链?_Javascript_Blockchain_Ethereum_Smartcontracts - Fatal编程技术网

Javascript 为什么我没有看到我的智能合约进入区块链?

Javascript 为什么我没有看到我的智能合约进入区块链?,javascript,blockchain,ethereum,smartcontracts,Javascript,Blockchain,Ethereum,Smartcontracts,我已经使用脚本标记链接了web3和metamask API,而且我的控制台中似乎没有收到任何类型的错误,所以为什么我不能在etherscan.io上找到我的智能合约 我的JS是这样的: var dataHandling = async function customResponse () { const provider = await detectEthereumProvider(); if (provider) {

我已经使用脚本标记链接了web3和metamask API,而且我的控制台中似乎没有收到任何类型的错误,所以为什么我不能在etherscan.io上找到我的智能合约

我的JS是这样的:

var dataHandling = async function customResponse () {
            const provider = await detectEthereumProvider();
            if (provider) {
                if (provider !== window.ethereum) {
                console.error('Do you have multiple wallets installed?');
                }
            console.log('Access the decentralized web!');
            } else {
                console.log('Please install MetaMask!');
            }
        }
        dataHandling();

        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            web3 = new Web3($INFURA_LINK);
        }
        const SCabi = $ABI
        const SCaddress = $address

async function connect(){
            //Will Start the metamask extension
            const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
            const account = accounts[0];
            console.log(ethereum.selectedAddress)
            var dat = {
                fname: document.getElementById('name').value,
                cert: document.getElementById('cert').value
            }

var SC = new web3.eth.Contract(SCabi, SCaddress)
            SC.methods.setMessage(JSON.stringify(dat)).call(function (err, res) {
                if (err) {
                    console.log("An error occured", err)
                    
                }else{
                    console.log(SC.methods.getMessage())
                    return
                }
            })

contract Message {
    string myMessage;

    function setMessage(string x) public {
        myMessage = x;
    }

    function getMessage() public view returns (string) {
        return myMessage;
    }
}
我的智能合约如下:

var dataHandling = async function customResponse () {
            const provider = await detectEthereumProvider();
            if (provider) {
                if (provider !== window.ethereum) {
                console.error('Do you have multiple wallets installed?');
                }
            console.log('Access the decentralized web!');
            } else {
                console.log('Please install MetaMask!');
            }
        }
        dataHandling();

        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            web3 = new Web3($INFURA_LINK);
        }
        const SCabi = $ABI
        const SCaddress = $address

async function connect(){
            //Will Start the metamask extension
            const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
            const account = accounts[0];
            console.log(ethereum.selectedAddress)
            var dat = {
                fname: document.getElementById('name').value,
                cert: document.getElementById('cert').value
            }

var SC = new web3.eth.Contract(SCabi, SCaddress)
            SC.methods.setMessage(JSON.stringify(dat)).call(function (err, res) {
                if (err) {
                    console.log("An error occured", err)
                    
                }else{
                    console.log(SC.methods.getMessage())
                    return
                }
            })

contract Message {
    string myMessage;

    function setMessage(string x) public {
        myMessage = x;
    }

    function getMessage() public view returns (string) {
        return myMessage;
    }
}

new web3.eth.Contract
未部署该合约。如果您提供了一个特定的地址,那么它会说“我想与这个已经部署在这个地址的ABI进行交互”。要部署它,您需要使用该方法。您不能选择部署到的地址,当从
deploy
返回的
Promise
解析时,它将返回给您

顺便说一句:我假设
$
值来自PHP或其他什么?如果您还没有尝试部署,那么在尝试部署之前检查它们是否正确可能是值得的


编辑:假设您的合同已部署,问题在于
setMessage
是一种修改区块链状态的方法,因此您需要使用交易(用少量ETH/gas支付该变更)

使用Metamask/Web3实现这一点的方法在API方面有点笨拙:

//(从之前开始)
让SC=newweb3.eth.Contract(SCabi,SCaddress);
//首先我们得到调用“data”,它对调用的参数进行编码
让data=SC.methods.setMessage.getData(JSON.stringify(dat));
//然后我们准备事务的参数
常量参数={
data:data,//来自上一行
//值:“0x0”,//可选,仅当您还想支付合同时
from:account,//用户的地址
收件人:SCaddress//合同地址
};
//然后我们开始实际的交易
以太坊请求({
方法:“eth_sendTransaction”,
params:[params],
}).then(txHash=>console.log(“事务哈希”,txHash));

我以前已经部署过该契约,但是当我尝试调用它的函数
setMessage(x)
时,我没有得到任何结果。另外,$值不是来自PHP,我只是让它们隐藏地址、API和infuralink@ComplexEeno好吧,也许我知道原因了。
getMessage
对您有用吗
setMessage
是一种改变区块链状态的方法,因此您需要为其付费,并且需要进行交易。看见我将用一个例子来编辑答案。这就是全部代码吗?您是否调用了
connect()
方法?