首页 > 解决方案 > 如何从 react.js 向 Solidity 智能合约发送多个函数参数?

问题描述

我正在尝试调用一个接收多个参数(8 个参数)的可靠合约函数。这是我的合约功能。

function generatePet(
                           uint256 _petPrice, 
                           string memory contentHash,
                           string memory _name,
                           string memory _akcPopularity,
                           string memory _height,
                           string memory _weight,
                           string memory _lifeExpectancy,
                           string memory _group
                           ) public returns(bool) {
         
         require(msg.sender != address(0), "Please! Check back! Registeration should not be from zero address");
         require(msg.sender == owner, "Only contract owner can generate more pets");
         
         tokenId++;
         require(tokenId <= 16, "More than 16 pets is not allowed");
         
         priceMapping[tokenId] = _petPrice;
         tokenIdToOffchainContentHash[tokenId] = contentHash;
         _mint(owner, tokenId);
         emit PetGenerated(owner, tokenId, _petPrice, contentHash, _name, _akcPopularity, _height, _weight, _lifeExpectancy, _group);
         return true;
       }

我现在正在尝试使用@drizzle/react-plugin: "^1.5.3" @drizzle/store": "^1.5.3"并做出反应。我正在尝试从反应 UI 调用此函数。调用generatePet的代码功能是这样的,


const initialValues = {
    petPrice: '',
    contentHash: '',
    petName: '',
    akcPopularity: '',
    height: '',
    weight: '',
    lifeExpectancy: '',
    group: ''
  }


const [values, setValues] = useState(initialValues);
const drizzleData = useContext(DrizzleContext.Context);

const sendData = (petPrice, contentHash, petName, akcPopularity, height, weight, lifeExpectancy, group) => {
      const { drizzle, drizzleState } = drizzleData;
      const contract = drizzle.contracts.PetShop;
  
      // let drizzle know we want to call the `set` method with `value`
      const stackId = contract.methods["generatePet"].cacheSend(petPrice, contentHash, petName, akcPopularity, height, weight, lifeExpectancy, group, {
        from: drizzleState.accounts[0]
      });
  
      console.log('value', petPrice, contentHash, petName, akcPopularity, height, weight, lifeExpectancy, group)
      console.log('stackId', stackId)
      console.log('drzzleState', drizzleState)
    };

const handleInputChange = (e) => {
      const { name, value } = e.target;

      setValues({
        ...values,
        [name]: value,
      });
    };
<form onSubmit={sendData}>
        <br />
        <label htmlFor='petPrice' >Pet Price </label><br/>
        <input type='text' value={values.petPrice} onChange={handleInputChange} name='petPrice'  id='petPrice' maxLength="50" /><br/>
        <label htmlFor='ipfsHash' >IPFS Content Hash </label><br/>
        <input type='text' value={values.contentHash} onChange={handleInputChange} name='contentHash'  id='contentHash' maxLength="50" /><br/>
        <label htmlFor='petName' >Pet Name </label><br/>
        <input type='text' value={values.petName} onChange={handleInputChange} name='petName'  id='petName' maxLength="50" /><br/>
        <label htmlFor='akcPopularity' >AKC Popularity </label><br/>
        <input type='text' value={values.akcPopularity} onChange={handleInputChange} name='akcPopularity'  id='akcPopularity' maxLength="50" /><br/>
        <label htmlFor='height' >Pet Height </label><br/>
        <input type='text' value={values.height} onChange={handleInputChange} name='height'  id='height' maxLength="50" /><br/>
        <label htmlFor='weight' >Pet Weight </label><br/>
        <input type='text' value={values.weight} onChange={handleInputChange} name='weight'  id='weight' maxLength="50" /><br/>
        <label htmlFor='lifeExpectancy' >Life Expectancy </label><br/>
        <input type='text' value={values.lifeExpectancy} onChange={handleInputChange} name='lifeExpectancy'  id='lifeExpectancy' maxLength="50" /><br/>
        <label htmlFor='group' >Group </label><br/>
        <input type='text' value={values.group} onChange={handleInputChange} name='group'  id='group' maxLength="50" /><br/>
        <input type="submit" value="Set Data" onSubmit={formHandleSubmit}/>
     </form>

问题

当我试图调用这个函数时,什么也没有发生,没有元掩码打开,我正在使用 ganache,我错过了什么?提交有什么问题吗?

请帮帮我

标签: reactjsethereumsoliditydrizzle

解决方案


推荐阅读