0

Buy method in smart contract:

enter image description here

Gas fee > 10k enter image description here

Why so expensive?

EDIT

My test:

    return Contract.deployed()
      .then(instance => {
        meta = instance;
        return meta.mint(account_one, 1, 'tokenURI', { from: account_one });
      })
      .then(() => {
          return meta.allowBuy(1, '20000000000000000', {from: account_one})
      })
      .then(() => {
          return meta.setApprovalForAll(account_two, true, {from: account_one})
      })
      .then((result) => {
          return meta.ownerOf(1)
      })
      .then((result) => {
          console.log('ownerOf before', result)
      })
      .then(() => {
          return meta.buy(1, {from: account_two, value: '20000000000000000'})
      })
      .then((result) => {
          return meta.ownerOf(1)
      })
      .then((result) => {
          console.log('ownerOf after', result)
      })
  });

Can I enable buy method without approve?

0

1 Answer 1

1

This often happens when an error is thrown in the contract. Metamask will set the gasLimit to a very high value, showing ridiculous amounts of gas fees. Check the code for possible errors, perhaps it's one of the first two require statements.

7
  • I updated the question putting the test I did and the result. It is accusing the value of the NFT in WEI that I defined for purchase.
    – GIA
    Nov 5, 2021 at 23:14
  • 1
    Regarding the edit: when passing numbers, pass these as strings: meta.allowBuy(1, '20000000000000000', {from: account_one}) Also missing the return statement in that line.
    – phaze
    Nov 6, 2021 at 0:15
  • Ok, now I understand what I do wrong. I need approve transfer to buy method works. I will update test in question to see. My question is: Can I enable buy method without "approve"?
    – GIA
    Nov 6, 2021 at 15:19
  • 1
    It would be nice to have more context on what you're trying to do / have some contract code. I'm assuming this is some kind of marketplace. Then yes, you will have to make the user approve the marketplace contract to be able to transfer their NFT. If you didn't, anyone could just freely transfer your NFTs out of your wallet.
    – phaze
    Nov 6, 2021 at 16:10
  • Its a simple NFT mint/buy contract extending ERC721. My "block way" is the price. When I mint some TOKEN, I set the price 0. If price == 0, can't buy/transfer the token. Owner of token can 'allowBuy' defining the new price and now others address can buy the token. But ok, I will use the approve like a order system, maybe works! Thanks Ian you help alot!
    – GIA
    Nov 6, 2021 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.