Solidity
在 ethers.js 中使用參數呼叫 Solidity 函式
我部署了一個具有此功能的智能合約:
function createNewMigration(string memory title, IERC20 oldTokenAddress, IERC20 newTokenAddress, uint256 decimalsOldToken, uint256 decimalsNewToken, uint256 divider, uint256 tokensForDistribution) public payable { require(msg.value == normalMigrationFee, "You need to pay the fee to start the migration!"); address newMigration = address(new MigrationHelper(msg.sender, title, oldTokenAddress, newTokenAddress, decimalsOldToken, decimalsNewToken, divider, tokensForDistribution)); migrations.push(newMigration); }
現在我嘗試使用 ethers.js 來呼叫它:
const [title, setTitle] = React.useState(""); const [oldTokenAddress, setOldTokenAddress] = React.useState(""); const [oldTokenDecimals, setOldTokenDecimals] = React.useState(""); const [newTokenAddress, setNewTokenAddress] = React.useState(""); const [newTokenDecimals, setNewTokenDecimals] = React.useState(""); const [divider, setDivider] = React.useState(""); const [tokenLogo, setTokenLogo] = React.useState(""); const [tokensForDistribution, setTokensForDistribution] = React.useState("");
….
const migration1 = async () => { try { const { ethereum } = window; if (ethereum) { const provider = new ethers.providers.Web3Provider(ethereum); const signer = provider.getSigner(); const Factory = new ethers.Contract(CONTRACT_ADDRESS, contractAbi, signer); const waveTxn = await Factory.createNewMigration(title, ethers.utils.getAddress(oldTokenAddress), ethers.utils.getAddress(newTokenAddress), oldTokenDecimals, newTokenDecimals, divider, tokensForDistribution, {gasLimit:300000}); console.log("Mining...", waveTxn.hash); await waveTxn.wait(); console.log("Mined -- ", waveTxn.hash); } else { console.log("Ethereum object doesn't exist!"); } } catch (error) { console.log(error) } }
這些變數使用以下形式獲取它們的值:
<form> <input type="text" id="fname" name="firstname" placeholder="Project Name" value={title} onChange={e => setTitle(e.target.value)}/> <br /> <input type="text" id="lname" name="lastname" placeholder="Old Token Address" value={oldTokenAddress} onChange={e => setOldTokenAddress(e.target.value)}/> <br/> <input type="text" id="lname" name="lastname" placeholder="Old Token Decimals" value={oldTokenDecimals} onChange={e => setOldTokenDecimals(e.target.value)}/> <br/> <input type="text" id="lname" name="lastname" placeholder="New Token Address" value={newTokenAddress} onChange={e => setNewTokenAddress(e.target.value)} /> <br/> <input type="text" id="lname" name="lastname" placeholder="New Token Decimals" value={newTokenDecimals} onChange={e => setNewTokenDecimals(e.target.value)}/> <br/> <input type="text" id="lname" name="lastname" placeholder="Divider" value={divider} onChange={e => setDivider(e.target.value)}/> <br/> <input type="text" id="lname" name="lastname" placeholder="Amount of new tokens for distribution)" value={tokensForDistribution} onChange={e => setTokensForDistribution(e.target.value)}/> <br/> <input type="text" id="lname" name="lastname" placeholder="Logo Link (eg: www.tokenwebsite.com/tokenlogo.png)" value={tokenLogo} onChange={e => setTokenLogo(e.target.value)}/> <br/> <button onClick={migration1}>Create</button> </form>
當我點擊創建按鈕時,只會刷新頁面。我也試過
你怎麼看?你在哪裡看到問題?
點擊按鈕時頁面的刷新可能是由於未阻止送出事件方法發生。
防止刷新的範例
onClick={(event) => Migration1(e)} function Migration1(e){ e.preventDefault() // <---- this prevent the form from being submited and run your code instead }
你稱之為乙太的部分不是。它實際上是反應。
在你的情況下
use onClick={(e) => Migration1(e)}
並將其放入 migration1 函式中
e.preventDefault()