Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
dapp 開発における big number の落とし穴 / Pitfalls of the ...
Search
blue planet
November 10, 2018
Technology
0
120
dapp 開発における big number の落とし穴 / Pitfalls of the big number in dapp development
イーサリアムと web3.js で dapp 開発する際の落とし穴と対応策
blue planet
November 10, 2018
Tweet
Share
More Decks by blue planet
See All by blue planet
join.me使用方法
blueplanet
0
130
rails cn
blueplanet
0
210
Sublime Text 2 プラグイン開発 - ゼロからPackageControlへ登録まで
blueplanet
5
2.1k
Other Decks in Technology
See All in Technology
Snowflake×dbtを用いたテレシーのデータ基盤のこれまでとこれから
sagara
0
120
2025/09/16 仕様駆動開発とAI-DLCが導くAI駆動開発の新フェーズ
masahiro_okamura
0
130
AWSを利用する上で知っておきたい名前解決のはなし(10分版)
nagisa53
10
3.2k
【NoMapsTECH 2025】AI Edge Computing Workshop
akit37
0
230
開発者を支える Internal Developer Portal のイマとコレカラ / To-day and To-morrow of Internal Developer Portals: Supporting Developers
aoto
PRO
1
480
AI時代を生き抜くエンジニアキャリアの築き方 (AI-Native 時代、エンジニアという道は 「最大の挑戦の場」となる) / Building an Engineering Career to Thrive in the Age of AI (In the AI-Native Era, the Path of Engineering Becomes the Ultimate Arena of Challenge)
jeongjaesoon
0
250
まずはマネコンでちゃちゃっと作ってから、それをCDKにしてみよか。
yamada_r
2
120
EncryptedSharedPreferences が deprecated になっちゃった!どうしよう! / Oh no! EncryptedSharedPreferences has been deprecated! What should I do?
yanzm
0
490
5年目から始める Vue3 サイト改善 #frontendo
tacck
PRO
3
230
Automating Web Accessibility Testing with AI Agents
maminami373
0
1.3k
Android Audio: Beyond Winning On It
atsushieno
0
2.4k
初めてAWSを使うときのセキュリティ覚書〜初心者支部編〜
cmusudakeisuke
1
280
Featured
See All Featured
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
113
20k
Agile that works and the tools we love
rasmusluckow
330
21k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
For a Future-Friendly Web
brad_frost
180
9.9k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Writing Fast Ruby
sferik
628
62k
Typedesign – Prime Four
hannesfritz
42
2.8k
The Art of Programming - Codeland 2020
erikaheidi
56
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Transcript
DAPP 開発における BIG NUMBER の落とし⽳穴 Hi-CON 2018
⽬目次 0. ⾃自⼰己紹介 1. イーサリアムの数値 2. BigNumber ライブラリ 3. まとめ
⾃自⼰己紹介 0
4 { name: “⿂振江”, kana: “ギョシンコウ”, role: “CTO”, company: “chainbow.io”,
}
5
イーサリアムの数値 1
7 Solidity のデータ型には、⼩数型はないですよね const transaction = … const testCoin =
new web3.eth.Contract(abi, tokenAddress) const decimals = await testCoin.methods.decimals().call() const tokenValue = transaction.value / 10 ** tokenInfo.decimals console.log(tokenValue)
8 ERC20 の decimals は普通に 18 ですよね @bob: 10 TTC
送⾦金金してみたぜ! @alice: 10 TTC 来てます! @bob: 0.01 TTC 送⾦金金してみたぜ! @alice: 0.01 TTC 来てます!
9 ERC20 の decimals は普通に 18 ですよね @bob: 10 TTC
送⾦金金してみたぜ! @alice: 10 TTC 来てます! @bob: 0.01 TTC 送⾦金金してみたぜ! @alice: 1.0000000000000001e-20 TTC 来てます!
BigNumber ライブラリ 2
11 web3.utils.fromWei 使いたい Ver 0.20.7 var fromWei = function(number, unit)
{ var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit)); return isBigNumber(number) ? returnValue : returnValue.toString(10); }; https://github.com/ethereum/web3.js/blob/develop/lib/utils/utils.js#L329-L333
12 web3.utils.fromWei 使いたい Ver 0.20.7 "dependencies": { "bignumber.js": "github:frozeman/bignumber.js-nolookahead", "crypto-js":
"^3.1.4", "utf8": "^2.1.1", "xhr2-cookies": "^1.1.0", "xmlhttprequest": "*" },
13 web3.utils.fromWei 使いたい Ver 1.0.0-beta.36 var fromWei = function(number, unit)
{ unit = getUnitValue(unit); if(!utils.isBN(number) && !_.isString(number)) { throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.'); } return utils.isBN(number) ? ethjsUnit.fromWei(number, unit) : ethjsUnit.fromWei(number, unit).toString(10); }; https://github.com/ethereum/web3.js/blob/1.0/packages/web3-utils/src/index.js#L231-L239
14 web3.utils.fromWei 使いたい Ver 1.0.0-beta.36 "dependencies": { "bn.js": "4.11.6", "eth-lib":
"0.1.27", "ethjs-unit": "0.1.6", "number-to-bn": "1.7.0", "randomhex": "0.1.5", "underscore": "1.8.3", "utf8": "2.1.1" }
15 web3.utils.fromWei 使いたい Ver 1.0.0-beta.36 function fromWei(weiInput, unit, optionsInput) {
var wei = numberToBN(weiInput); // eslint-disable-line var negative = wei.lt(zero); // eslint-disable-line const base = getValueOfUnit(unit); const baseLength = unitMap[unit].length - 1 || 1; const options = optionsInput || {}; if (negative) { wei = wei.mul(negative1); } var fraction = wei.mod(base).toString(10); // eslint-disable-line while (fraction.length < baseLength) { fraction = `0${fraction}`; } if (!options.pad) { fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1]; // 省略 return value; } https://github.com/ethjs/ethjs-unit/blob/master/src/index.js#L83-L117
16 web3.utils.fromWei 使いたい Ver 1.0.0-beta.36 import Big from 'bignumber.js'; function
BigNumber (number, base) { return new Big(number, base); } function toDecimals (number, decimals) { return BigNumber(number).times(BigNumber(10).pow(decimals)); } function fromDecimals (number, decimals) { return BigNumber(number).div(BigNumber(10).pow(decimals)); }
まとめ 3
数字ではなく、資産になるので、慎重にやりましょう
ご清聴ありがとうございました!