Comments (4)
from contracts.
Here's an example implementation of using an array of bytes [[0x54, 0x4B, 0x4E], [0x42, 0x4E, 0x54], [0x5A, 0x52, 0x58]]
instead of parsing a string TKN.BNT.ZRX
.
I also tested the gas cost of each approach:
String parsing cost (adding 3 tokens):
Total: 618 000 gas
Execution: 427 000 gas
Byte array cost (adding 3 tokens):
Total : 560 000 gas
Execution: 385 000 gas
Byte array example:
pragma solidity ^0.4.24;
contract Labels {
struct Token {
string label;
uint8 decimals;
}
mapping (address => Token) public labels;
function setLabels(address[] _tokens, bytes32[] _labels, uint8[] _decimals) public {
for (uint i = 0; i < _labels.length; i++) {
labels[_tokens[i]].label = bytes32ToString(_labels[i]);
labels[_tokens[i]].decimals = _decimals[i];
}
}
function bytes32ToString(bytes32 _bytes) public pure returns (string) {
bytes memory bytesString = new bytes(32);
uint charCount = 0;
for (uint i = 0; i < 32; i++) {
byte char = byte(bytes32(uint(_bytes) * 2 ** (8 * i)));
if (char != 0) {
bytesString[charCount] = char;
charCount++;
}
}
bytes memory bytesStringTrimmed = new bytes(charCount);
for (i = 0; i < charCount; i++) {
bytesStringTrimmed[i] = bytesString[i];
}
return string(bytesStringTrimmed);
}
}
from contracts.
Where did you get this from, I am not sure that I am on the same page as you here. I think I prefer the current implementation. Would be easier for the person interacting with the contract from a programming language ...
from contracts.
I wrote it as an example. Well it's cheaper in terms of gas and I would argue also more intuitive because other parameters are also arrays.
Also when interacting from a programming langue it's as easy as []byte("MyLabel")
to covert string to byte array
from contracts.
Related Issues (20)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contracts.