AV

Smart Contracts: Don’t Reinvent the Wheel

SafeMathIn most modern programming languages, safety in arithmetic operations is accounted for, so little thought goes into their implementation. However, in Solidity, overflows and underflows present a security risk.SafeMath is a library that ensures safe arithmetic operations by reverting the transaction if the bounds of an integer data type are exceeded.SafeMath example.The using statement indicates to the compiler that the contract is using functions defined in SafeMath for uint operations. Instead of using arithmetic operators (+, -, *, /, %), use the functions add(), sub(), mul(), div(), and mod(). Source:

Ethereum DApps: How to Listen for Events

Using the instance we created in step 2, we can subscribe to listen out for any events that it emits.Figure 4: Subscribe to ‘MyEvent’As stated earlier, the event we want to listen for is called MyEvent, and figure 4 shows us how. Our myContract instance has an accessor called events from which we can target MyEvent. This is an asynchronous function that returns a promise, and it’s called every time MyEvent is emitted.Line 3 defines what happens once data has been returned from the promise. Here we’re simply logging the