Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Pruebe una función onlyOwner en Javascript

Tengo esta situación en mi contrato inteligente:

 address[] public allowedUsers; function allowUser(address _newUser) public onlyOwner { allowedUser.push(_newUser); }

Estoy usando truffle y su conjunto de pruebas y luego escribí este caso, que falla tal vez porque no estoy usando el único método de propietario de la manera correcta:

 const MyContract = artifacts.require("../contracts/MyContract.sol"); contract("MyContract", accounts => { it("should deploy the contract and allow the user", async () => { const contract = await MyContract.deployed(); const account = accounts[0]; const owner = await contract.owner.call() await contract.allowUser(account).call({ from: owner }); const allowedUser = contract.allowedUser.call(0); assert.equal(whitelistedUser, account, 'new user is not allowed'); }) });

¿Alguien me puede ayudar? ¡Gracias a todos!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Suponiendo que haya establecido correctamente el propietario en el contrato, escriba un getter para el propietario en el contrato:

 function getContractOwner() public view returns (address) { return owner; }

en prueba.js

 contract("MyContract", accounts => { let _contract = null let currentOwner=null before(async () => { _contract = await MyContract.deployed(); currentOwner = await _contract.getContractOwner() }) it("should deploy the contract and allow the user", async () => { const account = accounts[0]; await contract.allowUser(account, {from: currentOwner}); // I assume this is retrieving data from a mapping const allowedUser = _contract.allowedUser.call(0); assert.equal(whitelistedUser, account, 'new user is not allowed'); }) });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!