New answers tagged

0

You should first cast the contract to a certain type before accessing its methods. In this case, you need to query the balanceOf an ERC721, so we'll use the IERC721 interface: require(IERC721(0x..).balanceOf(msg.sender) >= 1, "..."); P.S. To access IERC721 you can import the OZ implementation, which you're probably already doing.


1

In addition to Ismael's answer, you actually CAN delete from array with shrinking. If order does not matter in your case, you may move element from last position to the one you want to delete, and then simply do .pop() on the array. This essentially picks last item from the array and shrinks it by one. See example based on Ismael's: // Assume we have data: [...


0

To answer your URI question - if you want to use IPFS then you should install IPFS CLI to create a local node and upload your file to get a hash generated which you can use to create your URI. https://docs.ipfs.io/install/command-line/ If you want an example on how to mint an NFT using ERC721, IPFS and some basic toolchain try out this example from Patrick ...


0

I had a folder named smart_contracts. Inside of it I created a folder named hadhat_typescript_sample and communicated with the latter via smart_contracts/ I fixed that error by opening hardhat_typescript_sample in VsCode. Hopefully, it will help you out:)


0

I asked basically the same question on StackOverflow but for python, below is my solidity adaptation using this answer: // SPDX-License-Identifier: GPL-3.0 contract TwosComplement { function twos_comp_to_sign_mag(int8 value) external pure returns(int8) { int8 mask = 2**7 - 1; // 0111_1111 if (value < 0) { value = ...


0

If your project file structure is something like: Project client smart_contracts contracts *contract_A.sol you'll experience this error especially assuming you're in VSCode cd'ed into the sub directory. You need to open the 'smart_contracts' directory as the root when you execute commands such as npx hardhat test etc. When 'smart_contracts' is the root ...


1

Assuming the ethers version you're using is the older version, the error arises because ethers.js makes a distinction between constant calls and non constant(state writing) calls using call and send. When trying to call a constant method, call is used and when calling a non constant method, send is used. Because your contract changes the state in boolArray....


0

I did a brownie pm install for both the packages and also added following in the settings.json for Solidity { "workbench.colorTheme": "Default Dark+", "editor.formatOnSave": true, "python.formatting.provider": "black", "solidity.compileUsingRemoteVersion": "0.6.0", "editor.minimap....


0

I'm getting same error. Did you fix it?


1

It's the same thing, except that the casting part is done implicitly in the second example you provided.


0

Your code already overrides the supportsInterface() method well and will cause all the extended contract's methods to be called. This method is also used by the OpenZeppelin Contracts Wizard. If you get more interfaces, just add them in the override() tuple. The order of the interfaces might be important as they get called from the last to the first. ...


1

The downside to using lower solidity versions is that your contract possibly gets exposed to already fixed bugs. Other than using the very latest version (recommended), you could mitigate this by using some static analyser tools(mythril - https://github.com/b-mueller/mythril, slither - https://github.com/crytic/slither), OR you can try to get a developer to ...


0

Even though the code might compile, you can run into trouble with bugs in the future. There are many static analyser tools you can use to check for any problems with it (mythril - https://github.com/b-mueller/mythril, slither - https://github.com/crytic/slither), or you can try to get a developer to review your contract (ATTE - https://www.atteweb3.com/)


2

The short answer is no, it's not possible for a contract on network A to inspect a contract on network B. For a little more insight, blockchains cannot access external information at all because it is vital that the state of the contract can be reconstructed by any any node at any point in the future ... a property that cannot be maintained when there are ...


0

When you call the second function with this it will trigger an internal transaction, therefore msg.sender is the contract itself, which is different from the owner. You can call the function without this. which will not trigger an internal transaction and just "jump" to the function.


0

Figured out this set an array of size 0. To store variables in the struct the size needs to be equal to what is stored.


1

I agree with the above answer. Also, there are events that you can emit in your functions. Those events are saved on the blockchain and then you can use those events to check the transactions in your dapp, if you are creating a dapp.


3

First, EVM/Solidity doesn’t have the concept of a transaction hash. That’s the blockchain/record part. In your case, there’s no need for you to check if the transfer went through. The transfer REVERTS if failed. And any revert bubbles up so the function itself reverts and the user can then see this in their transaction hash.


1

What you are going to want to do is implement the ERC-20 token as an ERC-777 token which is a backward-compatible version of the ERC-20 standard with a few extra features. One of these new features is the ability to notify recipients of their balance change events. With this trigger, inside the NFT contract, which is also of type ERC-777 Recipient, the ...


0

Solidity has an SMTChecker which makes using assert very cool because it can prove that your invariants are true: Solidity implements a formal verification approach based on SMT (Satisfiability Modulo Theories) and Horn solving. The SMTChecker module automatically tries to prove that the code satisfies the specification given by require and assert ...


0

You should use assert to help you find invariants which have been violated. You use assert to help you catch when the impossible happens. Solidity also has an SMTChecker which can prove that your invariants are true: Solidity implements a formal verification approach based on SMT (Satisfiability Modulo Theories) and Horn solving. The SMTChecker module ...


0

This might be silly, but have you compiled your solidity file? https://trufflesuite.com/docs/truffle/getting-started/compiling-contracts.html


1

Here's a good article: https://codeforgeek.com/assert-vs-require-in-solidity/ Philosophically, they are intended to mean two very different things. require is "user should have done this before calling the function", whereas assert means "something fundamentally is wrong". Pragmatically, it depends on what your intention is. If the goal ...


1

Its easy. Instead of hard coding the amount, you just put a variable in it. Take a look at code below which takes an address and amount from user, and then sends the amount to the given address: function payFromContract (address payable _recipient, uint _amount) external { _recipient.transfer(_amount); }


0

_ownedTokens is a mapping that point to a uint256[1]. This is an array with the fixed length of 1. Therefore this array doesn't support push as the length cabnot be adjusted. If you want to be able to add/remove items, then you should declare it as a dynamic array uint256[].


1

Here's an ERC 1155 implementation of EIP 2981 royalties--implemented by some exchanges, but not yet universal. Royalties aren't built into a contract's transfer functions, which would interfere with gifting and transfers between an owner's wallets (see explanation from the EIP 2981 abstract below). ...The royalty payment must be voluntary, as transfer ...


1

This repo has an ERC 1155 implementation with EIP 2981 royalties; for an ERC 721, you'd just need to import IERC2981 and then add the royaltyInfo function and supportsInterface override to the token contract.


0

According to the Solidity docs you cannot return a mapping: Mappings can only have a data location of storage and thus are allowed for state variables, as storage reference types in functions, or as parameters for library functions. They cannot be used as parameters or return parameters of contract functions that are publicly visible. These restrictions are ...


1

It very much depends on how your contract is used. E.g. if you have a custom interface and expect that all interactions are done via that interface it is feasible to use the solution proposed by p0pps. When comparing your two approaches then approach 2 is cheaper in any case. If we look at setting the whitelist, then storing an array on the blockchain will ...


0

Your issue is not related to the view function. You assign blockNumber at constructor time and then at some later point call viewFunction, therefore block.number will always be greater (or if done in the deployment block equal) to blockNumber. So if you subtract blockNumber - block.number and type it as a uint then this will cause an underflow error. All ...


0

This depends what you require and what you want to ensure. abi.encode will apply ABI encoding rules. Therefore all elementary types are padded to 32 bytes and dynamic arrays include their length. Therefore it is possible to also decode this data again (with abi.decode) when the type are known. abi.encodePacked will only use the only use the minimal required ...


2

I am assuming you ask how to simulate a transaction off-chain. For this you have a couple tools. If you want the all in one hosted solution I would suggest you to look into https://tenderly.co/. If you have some programming knowledge you can also use Ganache to spin up a local for of a network and simulate your transaction. This will then allow you to ...


0

The gasUsed value that Truffle shows you comes from the transaction receipt returned by the Ethereum client (see eth_getTransactionReceipt in json-rpc). In case of running tests locally with Truffle, the client used is Ganache. The client calculates the gas by executing the transaction on its EVM implementation. In case of Ganache the EVM used is EthereumJS. ...


0

You will need to accept Wrapped Ether. (Wrapped Ether is a version of Ethereum pegged onto Polygon or any other chain) You will implement this just like accepting any other token on Solidity, see this: deposite and withdraw erc20 token using smart contract


0

This is an implementation example from @RickPark answer, using each steps in its response as comments. function hardcodedExample() public pure returns(bool) { uint256[4] memory first = [uint256(2), 4, 6, 8]; uint256[4] memory second = [uint256(4), 2, 5, 7]; // Simply get (start from) the first number from the input array for (uint256 ii = 0;...


1

As blocks get mined the block.number will increase. The stored block number will be smaller than the current. Subtracting the two reverts because the result underflows (you can think of it like negative values). In solidity 0.8+ underflows are reverted by default. The error has nothing to do with visibility otherwise the compiler would have already warned ...


0

If you want to make payment in Ether in polygon, you need to use pegged Ether in polygon chain, which is ERC20 token. Ask user to approve 0.1 pegged ETHER for your contract and make transfer from mg.sender by 0.1 ETHER.


1

I managed to implement both floor and ceil as follows. The ABDKMath64x64.toInt function essentially floors the signed fixed point number to a signed integer, hence the following would hold true: toInt(-18444899399302180000) == -1 i.e. floor(-0.9999) == -1 toInt(0) == 0 i.e. floor(0) == 0 toInt(18444899399302180000) == 0 i.e. floor(0.9999) == 0 toInt(...


0

Are you creating both token A and token B, just token B, or neither? It sounds to me like you're creating both of them? Correct me if I'm wrong here, but to sum up it sounds like you're creating contracts for ERC20 tokenA and ERC20 tokenB, and whenever an amount of tokenA is transferred, you want the same amount of tokenB to be transferred to the same ...


0

In solidity inheritance works slightly different than other languages. Let's suppose you have a Base contract and a Derived contract. contract Base { uint256 public x; function foo(uint256 a) public { x = 1000 + a; } function bar() public view returns (uint256) { return x; } } contract Derived is Base { function bar(...


1

Here is a mock marketplace contract (please excuse errors): pragma solidity ^0.8.0; import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol"; import {Vault} from "./Vault.sol"; contract Sale { // -------- ADDRESS -------- address public vault; // -------- UINT -------- uint public saleNonce; // --...


1

Usually the recommended approach is to emit an event with the contract address. Then you can read the event from the transaction receipt and get the address. event TokenCreated(address indexed a); function _createFNftToken( string calldata _name, string calldata _symbol, uint256 _amount, address _to ) private returns(address){ ...


0

const busdAbi = [ "event Approval(address indexed owner, address indexed spender, uint256 value)", "function approve(address spender, uint256 amount) external returns (bool)" ]; // retrieve the BUSD contract busdToken = await ethers.getContractAt(busdAbi, BUSD_ADDRESS?BUSD_ADDRESS:'', deployer)


0

There should be no difference as totalSupply simply returns the length of the enumerated tokens array, which is decremented on burn and incremented on mint.


0

You could use web3.eth.abi.encodeParameters to encode parameters from the js side. const result = web3.eth.abi.encodeParameters( ['string','uint256','bytes32'], ["stingData", 1234, "0x65225648"], ) console.log(result) Which returns ...


0

The function should be returning the struct offerModel and not a string. Try this function searchOffers(string memory _itemName) external view returns (offerModel memory) { for (uint256 i = 0; i < numberOfOffer; i++) { if (keccak256(offers[i].itemName) == keccak256(_itemName)) { return offers[i]; } ...


0

_tokenID is just a parameter of the function ownerOf(). You can pass anything in this function as a parameter. //Calling ownerOf() function with a parameter 1 to find the owner of token with ID = 1 return ownerOf(1); then during the execution you can replace all the instances of _tokenID with 1


0

This is bad contract design in my opinion. You can just store the mapping in Bank.sol and have the same functionality. You are also using double the gas required. Is it because BankMediator has 0 eth thus not able to pay any gas fee? How can I make wallet pay gas fee even for withdrawal? No. All gas is paid for by the user. Contracts do not pay gas as ...


1

The way how to do this depends on the contract that you want to extract the variable from. If the contract normally provides a function to query this variable you just need to know the correct function id to query it. If this is not the case then you can use eth_getStorageAt to query the storage at a specific slot for that contract, but for this you will ...


0

Connecting to the mainnet fixed the issue for me, seems the error is from mumbai cause the code works fine on both local and mainnet rpc. I know this isnt an optimal solution especially if you just wanna run some test but this is what worked for me and thought I should share.


Top 50 recent answers are included