Questions tagged [design-patterns]
A design pattern is a general reusable solution to a commonly occurring problem in software design. Use this tag for questions when you're having problems with the implementation of design-patterns. Please don't use this tag on questions about text pattern matching. When using this tag on implementation heavy questions - tag the code language the implementation is written in.
65
questions
5
votes
2
answers
65
views
+50
Can the `totalSupply` of a token be different than the sum of all account balances?
If I'm building a rebase token that has a difference of 1 wei sometimes (pre/post rebase) between the totalSupply and the sum of the balances of all holders, does that pose an attack risk within the ...
1
vote
1
answer
33
views
Reentrancy Guard and CEI
Could you please explain, is there a need to use a ReentrancyGuard modifier if the contract follows the CEI(Check Effect Interactions) pattern? If yes, could you bring an example, where the contract ...
0
votes
0
answers
12
views
Solidity design pattern drawbacks
I was ask by a friend to give feedback on his solidity project. I came over a general design pattern issue, which is not good from an object oriented point of view. I try to outline the projects ...
0
votes
1
answer
35
views
How do you use the Diamond Pattern for escrow upgrades?
I am creating a contract with the diamond pattern so I can evolve and add new features to my contract.
Contract facets:
Contract A: an ERC721 that takes care of minting its own NFTs
Contract B: ...
3
votes
0
answers
59
views
`delegatecall` in batch call stops working as expected when changing the code slightly
I have the following function for batching multiple calls, which returns the result (success or not) and the return data for each corresponding call.
function callBatch(bytes[] calldata calls) public ...
1
vote
1
answer
125
views
Is it possible to batch-call the function multiple times so it's executed asynchronously and make profit from that?
If an attacker batch-calls the vulnerable contract's function in a re-entrancy attack attempt, would the transaction with a CEI pattern violation be fully executed before the other transactions are ...
2
votes
2
answers
65
views
Solidity Proxy: why using initialiser instead of a constructor?
Why proxies can't read from the constructor?
0
votes
1
answer
62
views
What is the Web3 approach to storing secret tokens (for example, when need to store the local salt for commit/reveal pattern)?
What is the Web3 approach to storing secret tokens (for example, when need to store the local salt for commit/reveal pattern)?
I'm currently using local storage.
For example in Web2 a back-end layer ...
1
vote
0
answers
36
views
Complex factory optimization
After two and a half years of developing smart contracts on a non-EVM blockchain (Tezos), I started working on a few solidity projects, and I'm trying to understand some things.
I built a factory ...
2
votes
1
answer
686
views
How to make UUPS upgradeable contracts modular
I have the following contract:
import { UUPSUpgradeable } from "@openzeppelin-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import { OwnableUpgradeable } from "@openzeppelin-upgradeable/...
0
votes
1
answer
211
views
Cloning ERC20/ERC721
I'm using a Clone Factory pattern to create new instances cheaper. I realized that every field, preset or set in constructor, has default value. Let's assume that I have this contract:
contract ...
1
vote
1
answer
65
views
Is there a reason a function is made public and then called internally in Solidity?
I have come across a popular smart contract in the OpenZeppelin library that manages access control of a contract.
This is the library:
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/...
1
vote
0
answers
45
views
Storage design for a social network
I want to create a social network app using Solidity, but I am having a hard time thinking of the storage design. Using this link:
Are there well-solved and simple storage patterns for Solidity? as a ...
0
votes
1
answer
1k
views
Using ABI's in Proxy contract pattern
I am planning to implement a Proxy pattern for my smart-contracts. And I want to make sure I understood it's use-case right.
In short: User only needs to know about Proxy contract address, and by ...
1
vote
0
answers
104
views
Getting data from contract instance created via Factory pattern with Hardhat
I had two contracts AuctionFactory and Auction contracts.
contract AuctionFactory is IAuctionFactory {
using Counters for Counters.Counter;
Counters.Counter private auctionIds;
uint256 ...
1
vote
1
answer
316
views
Solidity private/internal function call from external function
what the purpose of that way to restrict access to function call?
function updateQuorumNumerator(uint256 newQuorumNumerator) external virtual onlyGovernance {
_updateQuorumNumerator(...
0
votes
1
answer
97
views
Is there a design pattern in order to protect private keys in a Multi SigWallet?
I've developed a MultiSig Wallet for my company. For timing reason i've used as "code base" the Ethereum Multi Sig Smart Contract by BitGo (here the link of the repository: https://github....
0
votes
0
answers
64
views
Does it cost more gas to call a function more than once in another function?
Does it cost more gas to call a function more than once in another function?
For example:
function getValue() public returns(uint){
return 5;
}
function getOtherValue() public returns(uint){
...
0
votes
1
answer
51
views
How to handle memberships of multiple users of a scheme
I am very, very new to blockchain development and even newer to Ethereum. Have done / am doing some online course though...
Anyway, how would one handle the situation where you might have a scheme ...
0
votes
1
answer
36
views
splitting contract in data and application contract
I made 2 contracts in 2 different solidity files. Lets for simplicity assume the contracts are called A and B. A contains data and some simple function calls, B contains more complex function calls. ...
0
votes
0
answers
58
views
Can I make a delegatecall to a Solidity contract that is compiled with a higher compiler version?
Lets say that I have the contracts A and B.
A needs to make a delegatecall to B.
A is (for some reason) compiled with a compiler version that is lower than B's compiler version.
Can I safely assume ...
6
votes
2
answers
399
views
Graph traversals on Ethereum, at scale
I read the Efficient Solidity storage pattern for a directional weighted graph thread, but that deals with storing graphs. I'm interested in techniques for how to traverse graphs in Solidity, at scale....
0
votes
2
answers
69
views
Ho can a server act for third party?
I will try to explain my idea.
Our server receive data to save from different servers, we call it S1, S2 and S3.
Our server uses a node script with web3.js to call function over a contract. Actually ...
0
votes
1
answer
62
views
Which Pattern to follow to create contract?
I have an ad campaign and I want to store data(like campId, number of impressions, start date, end date etc.) of each campaign on blockchain. Also, whenever I will pass campId, I should be able to ...
0
votes
1
answer
141
views
How do you pause a Proxy-Logic pattern?
Suppose we have a Proxy contract that forwards to call to a Logic contract by using delegatecall.
If we pause the Proxy, users will still be able to directly interact with the Logic contract. While ...
0
votes
2
answers
773
views
Split State Storage Contract
This is a follow-up question of Split Contract Design Pitfall
Background: I have split my logic into two contracts A and B. A keeps the state and not ETH. All state change functions are only allowed ...
2
votes
2
answers
3k
views
Reason for check of msg.value > 0
I came across published smart contracts (like this one from ConsensSys) that check whether msg.value is strictly larger than zero in their payable functions.
After wondering whether msg.value can be ...
0
votes
2
answers
225
views
Self Destruct Design pattern - Loss of funds issue
I was exploring on self destruct design pattern for a time bound smart contract. So my contract should not accept any further state change after a certain given time period.
While working on that I ...
2
votes
1
answer
769
views
What is the Withdrawal Pattern?
I am reading this article : https://medium.com/@jgm.orinoco/why-use-the-withdrawal-pattern-d5255921ca2a
It seems interesting. Could anyone explain the idea of the Withdrawal pattern in a simple way ?
2
votes
1
answer
2k
views
Design pattern Checks-Effects-Interactions Pattern
I was workig on design pattern Checks-Effects-Interactions Pattern and found the below link which has a similar question but in the direction which I am looking at ..
Checks-Effects-Interactions ...
2
votes
1
answer
4k
views
Issue in type conversion : Explicit type conversion not allowed from unit256 to bytes1
I am trying to convert the unit to string using the method. I have the "TypeError" in the line. Please check the screen shot below for the error message.
function uint2str(uint i) internal pure ...
0
votes
1
answer
139
views
Circular reference error in factory contract pattern
I've used the factory model before, but I've never encountered a circular reference error, and I'm having trouble seeing how this could be a circular reference when I'm creating a brand new ...
1
vote
1
answer
148
views
Solidity/Remix Error: The constructor should be payable if you send value enum change state
I'm using this (State Machine) design pattern in solidity and having an error when changing the state after executing the first function. Any hint? Thank you!
Error: revert The transaction has been ...
0
votes
1
answer
237
views
Design pattern for game development
I am developing a game which currently uses a GameFactory contract to create instances of a Game contract.
The Factory pattern would allow any number of players to play their own single player game ...
2
votes
2
answers
2k
views
What is the proper pattern to use a temporary variable and ensure it gets stored?
A contract has a state variable, which is complex. For the example, lets say it is a mapping of structs. Inside a method, I want to access that and store the changes. Like so:
contract FooManager {
...
0
votes
1
answer
98
views
Production Fork of Ethereum
everyone!
I have a project, where I surely need to implement transparency of all the actions doing in the network. Let's say I want to create public asset tracking. So everybody who posts an asset in ...
0
votes
1
answer
723
views
How can contracts share a whitelist?
I have a token, let's call it TokenA, that inherits from Open Zeppelin Whitelist.sol. TokenB and TokenC both inherit from TokenA and they each have some additional functions.
I want all three Tokens ...
1
vote
2
answers
264
views
How to identify submitted transaction to network?
I am developing an eCommerce application that has on-chain and off-chain components. The off-chain components focus on the compute and storage intensive operation while on-chain smartcontract ensure ...
2
votes
0
answers
232
views
What is a common pattern to work with a large number of complex Things: make a Thing a contract, or a struct in a mapping?
In order to build a somewhat complex Dapp, I need to manage "Contributions". To do this, I roughly see two patterns:
A Factory-pattern, in which each Contribution is it's own Contract/Instance.
A ...
1
vote
0
answers
347
views
Listen for Solidity Event With X Number of Confirmations
I presume Solidity events are fired the moment the block that includes its transaction is mined -- please correct me if I'm wrong.
I need to sync the transaction status locally but wish to only save ...
0
votes
1
answer
70
views
Why lot of Smart Contract implementations are using a single instance to hold all accounts?
I found a lot of example of tokens (like ERC20) using this pattern:
contract MyCoin {
mapping (address => uint) balanceOf;
...
}
Those contracts are deployed once, the "balanceOf" ...
3
votes
1
answer
432
views
Oraclize __callback function with unbounded gas usage
I am making a lottery contract and using Oraclize to select a winning number.
Here is what I have now:
The owner calls a finalize() function, which disallows new entries, and makes a call to ...
1
vote
2
answers
171
views
Withdrawal pattern in fallback function
I need to implement withdrawal pattern for my smart contract. I wanted to make it possible to interact with contract for the people, who use smartphones and tables, just by sending some amounts of ...
2
votes
0
answers
47
views
Where does Ethereum Dapps go in known design paterns
I am doing a Dapp for a school project and I am supposed to choose a design pattern to work with.
I want to know the possibilities for design patterns concerning the ethereum dapps, or if they have ...
4
votes
4
answers
1k
views
Should UIs handle 4Byte function selector collisions?
With 4 bytes collisions are possible.
But they are not very likely. Currently in the 6,846 entries in the 4Byte.directory there are no collisions.
Still they are possible. Now I wonder if UIs should ...
1
vote
1
answer
34
views
Are there any downsides to if...else logic versus rejecting in a function
Pretty much all examples of contract functions I see use the require(...) pattern. If the initial condition is not as expected, an error is thrown and the transaction is reverted.
I don't like this ...
1
vote
0
answers
43
views
Build blockchain application that has some off-chain functionalities
I am learning about blockchain and I have a general question. Let's say that I want to build my custom blockchain based on Ethereum. My implementation has many nodes and few of them are chosen ...
0
votes
2
answers
279
views
Web wallet best practices
I'm putting together an ether/erc20 token wallet in a chrome extension but would like to follow best practices with regards to private key security.
I'm using localstorage to handle login and its ...
0
votes
1
answer
103
views
Modeling language for Block-Chain-Oriented Systems
Any ideas about how do i use graphic models for representations. Should i follow UML diagrams style?
0
votes
1
answer
598
views
Solidity local validation call before sending state changing transaction
I have an use-case where the user should be able to change a contract's state only when the inputs pass a validation check. However the validations are expensive to perform and can be done locally ...