[英]Accepting parameters in javascript to send to solidity smart contract
[英]Calling solidity smart contract with javascript to mint token does not seem to work
我有一個關於以太坊的新問題——剛剛開始並試圖了解以太坊開發環境。
我有一個非常簡單的 721 測試合約,我部署到 Ropsten,我可以使用 REMIX 來使用它,它可以用於鑄造和查看代幣余額(tokenCounter)等。
這是“合同”: https://ropsten.etherscan.io/address/0x97E0175415cB7D758cFB0ffc27Be727360664B90
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import "@0xcert/ethereum-erc721/src/contracts/tokens/nf-token-metadata.sol";
import "@0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";
contract Icollect is NFTokenMetadata, Ownable {
uint256 public tokenCounter;
constructor() NFTokenMetadata () {
nftName = "Icollect";
nftSymbol = "ICOL";
tokenCounter = 0;
}
function mint(string calldata _uri) external onlyOwner {
super._mint(msg.sender, tokenCounter);
super._setTokenUri(tokenCounter, _uri);
tokenCounter = tokenCounter + 1;
}
}
當我使用此測試 javascript 和安全帽進行本地測試時,該合約似乎適用於鑄造代幣
async function main() {
const contract_address = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
const Icollect = await ethers.getContractFactory("Icollect");
const icollect = await Icollect.attach(contract_address);
const mint_return = await icollect.mint("https://test.test");
console.log("mint returned: ", mint_return);
console.log("owner:", await icollect.owner());
console.log("owner:", await icollect.ownerOf(0));
console.log("symbol:", await icollect.symbol());
console.log("URI:", await icollect.tokenURI(0));
console.log("token counter:", await icollect.tokenCounter());
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
問題:當我嘗試從 web 頁面鑄造令牌時,我似乎無法讓“鑄造”交易工作(當我需要汽油時)。 但是,查看變量是有效的(例如合同的名稱和所有者)。 知道我在這里做錯了什么。
const transaction = contract.methods.mint(NFT_URI);
我正在構建 object,然后對其進行簽名然后發送。 交易似乎有效,但我沒有看到額外的令牌。
這是使用以下代碼的示例“薄荷”交易之一: https://ropsten.etherscan.io/tx/0x6f3fc389355ffedfe135ac049837ac2d1a6eb6aad1dd10b055addfa70814e0fd但使用 REMIX 查詢從不增加計數“tokenCounter”
document.getElementById('mintbutton').onclick = async function() {
let NFT_URI = document.getElementById("nft_uri").value;
let contract_address = document.getElementById("contract_address").value;
const contract = await new web3.eth.Contract(abi, contract_address);
let account_address = document.getElementById("account_address").value;
const account = web3.eth.accounts.privateKeyToAccount(privateKey).address;
const transaction = contract.methods.mint(NFT_URI);
let nonce_count = await web3.eth.getTransactionCount(account_address);
//build the transaction
const txObject = {
nonce: nonce_count,
to: account_address,
//value: web3.utils.toHex(21000),
//gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),
gas: await transaction.estimateGas({from: account}),
data: transaction.encodeABI()
};
const signed = await web3.eth.accounts.signTransaction(txObject, privateKey);
const return_from_send = await web3.eth.sendSignedTransaction(signed.rawTransaction);
return_string =
"blockHash: " + return_from_send.blockHash + "<br>" +
"blockNumber: <a href='https://ropsten.etherscan.io/block/" + return_from_send.blockNumber + "'>" + return_from_send.blockNumber + "</a><br>" +
"contractAddress: " + return_from_send.contractAddress + "<br>" +
"cumulativeGasUsed: " + return_from_send.cumulativeGasUsed + "<br>" +
"from: <a href='https://ropsten.etherscan.io/address/" + return_from_send.from + "'>" + return_from_send.from + "</a><br>" +
"gasUsed: " + return_from_send.gasUsed + "<br>" +
"status: " + return_from_send.status + "<br>" +
"to: <a href='https://ropsten.etherscan.io/address/" + return_from_send.to + "'>" + return_from_send.to + "</a><br>" +
"transactionHash: <a href='https://ropsten.etherscan.io/tx/" + return_from_send.transactionHash + "'>" + return_from_send.transactionHash + "</a><br>" +
"transactionIndex: " + return_from_send.transactionIndex + "<br>" +
"type: " + return_from_send.type + "<br>";
$('#mint_return').html(return_string);
var x = document.getElementById("showDIV3");
x.style.display = "block";
}
帕特里克 - 這是 console.log https://imgur.com/XBQTAxT的鏈接
解決方法:我在交易 object 中輸入了錯誤的地址。 我有帳戶地址而不是合同地址。 下面更正。
//build the transaction
const txObject = {
nonce: nonce_count,
to: contract_address, //NOT account_address,
//value: web3.utils.toHex(21000),
//gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),
gas: await transaction.estimateGas({from: account}),
data: transaction.encodeABI()
};
解決方法:我在交易 object 中輸入了錯誤的地址。 我有帳戶地址而不是合同地址。 下面更正。
//build the transaction
const txObject = {
nonce: nonce_count,
to: contract_address, //NOT account_address,
//value: web3.utils.toHex(21000),
//gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10','gwei')),
gas: await transaction.estimateGas({from: account}),
data: transaction.encodeABI()
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.