Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BlockchainTokyo23.pdf

biga816
November 29, 2019

 BlockchainTokyo23.pdf

biga816

November 29, 2019
Tweet

More Decks by biga816

Other Decks in Technology

Transcript

  1. τϥϯβΫγϣϯཤྺͷ؅ཧ લఏ # αʔόʔαΠυͰτϥϯβΫγϣϯΛൃߦ͢Δ # ύϒϦοΫνΣʔϯ ͭΒΈͱରԠ # ϢʔβʔʹτϥϯβΫγϣϯ͕֬ఆͨ͠ͷ͔Ͳ͏͔௨஌͢ΔͨΊʹτ ϥϯβΫγϣϯͷཤྺͱεςʔλεΛ%#ʹอ࣋͢Δඞཁ͕͋ΔͨΊɺ

    ఆظతʹτϥϯβΫγϣϯΛ؂ࢹ͢Δඞཁ͕͋Δɻ # ޮ཰Λߟ͑ͯશτϥϯβΫγϣϯͷεςʔλεΛ؂ࢹ͸ͤͣΠϕϯτ ͷ؂ࢹΛߦ͏͜ͱ͕ଟ͍͕ɺXFCKTͷXFCTPDLFUͷίωΫγϣϯ͸ ͨ·ʹ੾ΕΔ͠ɺର৅Πϕϯτ͕૿͑ΔͱΧΦεʹͳΓ͕ͪɻ
  2. τϥϯβΫγϣϯཤྺͷ؅ཧ export class HogeService { : connect(): void { const

    provider = new Web3.providers.WebsocketProvider(GETH.HOST); const reconnect = () => { const interval = this.isReconnection ? 1000 : 0; setTimeout(() => { this.isReconnection = true; this.connect(); this.observer.next(false); }, interval); }; provider.on("error", () => reconnect()); provider.on("end", () => reconnect()); provider.on("connect", () => { this.isReconnection = false; this.observer.next(true); }); this.web3 = new Web3(provider); } on(): Observable<any> { const observable = new Observable((observer: Subscriber<any>) => { this.observer = observer; return; }); return observable; } : } export class Hoge { : async run(): Promise<void> { this.hogeService.on().subscribe(async (isConnected: boolean) => { if (isConnected) { this.watchEvents(); this.fetchPastBlocks(); } }); this.web3Service.connect(); } : }
  3. (BT-JNJU લఏ # αʔόʔαΠυͰτϥϯβΫγϣϯΛൃߦ͢Δ # ύϒϦοΫνΣʔϯ ͭΒΈͱରԠ # 8FCKTͷFTUJNBUF(BTΛ༻͍ͯHBT-JNJUΛઃఆ͍͕ͨ͠ɺ࣮ࡍͷফ අHBTΛԼճͬͨΓɺ#MPDLͷHBT-JNJUΛ௒͑ͯΤϥʔΛు͍ͨΓͯ͠

    ҆ఆ͠ͳ͍͜ͱ͕ͨ·ʹ͋Δɻ # ݁ہ࣮ࡍͷফඅHBTʹগ͠ΛόοϑΝΛ࣋ͨͨ͠ݻఆ஋ͱ͢Δͷ͕҆ ఆͦ͠͏ɻͨͩ౎౓ফඅHBTΛࢉग़͢Δͷ͕໘౗ɻ
  4. /PODF໰୊ export class SampleService { : call(): void { const

    provider = new Web3.providers.WebsocketProvider(GETH.HOST); : let nonce = await this.web3Service.getPendingTxCount(ADDRESS); const getNonce = () => { const rv = nonce; nonce += 1; return rv; }; await Promise.all([ this.web3Service.send( contract, ‘setFoo’, { foo: 'bar' }, getNonce() ), this.web3Service.send( contract, 'setHage’, { hage: true }, getNonce() ), ]); } : }
  5. ίϯτϥΫτංେԽ໰୊ pragma solidity ^0.5.0; : contract IERC20 { function balanceOf(address

    address) public returns (uint256); } contract HogeContract { function balanceOf(address token, address holder) external { return IERC20(token).balanceOf(holder); } : }
  6. ಈ࡞֬ೝਏ͗͢໰୊ const FooMarket = artifacts.require('FooMarket'); const HogeToken = artifacts.require('HogeToken'); :

    contract('FooMarket', function ([owner, alice]) { beforeEach(async function () { this.HodeToken = await HogeToken.new(); this.FooMarket = await FooMarket.new(this.HodeToken.address, { from: owner }); await this.FooMarket.setWhitelisted(owner, true, { from: owner }); await this.FooMarket.setWhitelisted(alice, true, { from: owner }); }); describe('transfer hoge token', function () { it('token balance should be changed', async function () { await this.HogeToken.mint(alice, 100, { from: owner }); assert(await this.FooMarket.transfer(alice, bob, 100, { from: owner })); const aliceBalance = await this.HogeToken.balanceOf(alice); const bobBalance = await this.HogeToken.balanceOf(bob); assert.equal(aliceBalance, 0); assert.equal(bobBalance, 100); }); }); : });
  7. 4PMJEJUZͷ͕ͤ͘ڧ͗͢໰୊ contract Hoge { uint256[] private targetValues; mapping(uint256 => uint256)

    private targetValuesIndex; mapping(uint256 => bool) private valueStatus; function add(uint256 _targetId) public { require(!exists(_targetId)); targetValuesIndex[_targetId] = targetValues.push(_targetId) - 1; valueStatus[_targetId] = true; } function remove(uint256 _targetId) public { require(exists(_targetId)); uint256 targetIndex = targetValuesIndex[_targetId]; uint256 lastIndex = targetValues.length - 1; uint256 lastTargetId = targetValues[lastIndex]; valueStatus[_targetId] = false; targetValues[targetIndex] = lastTargetId; targetValues.length--; targetValuesIndex[lastTargetId] = targetIndex; targetValuesIndex[_targetId] = 0; } function exists(uint256 _targetId) public view returns (bool) { return valueStatus[_targetId]; } }