Sunday, September 26, 2021

Ethereum Programming 13 upload image 2

//cmd -console.log(result)
PS C:\Users\zchen\Programming\decentragram-starter-code> truffle test
Using network 'development'.


Compiling your contracts...
===========================
> Compiling .\src\contracts\Decentragram.sol
> Compiling .\src\contracts\Decentragram.sol
> Compiling .\src\contracts\Migrations.sol
> Artifacts written to C:\Users\zchen\AppData\Local\Temp\test--14908-izXoOAEen8OE
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



  Contract: Decentragram
    deployment
      √ deploys successfully
      √ has a name (190ms)
    images
{
  tx: '0xdeaed902492c5399b6b4dd719a5de01351f560ec03c5776dd269e6754f8de3b4',
  receipt: {
    transactionHash: '0xdeaed902492c5399b6b4dd719a5de01351f560ec03c5776dd269e6754f8de3b4',
    transactionIndex: 0,
    blockHash: '0xad50fed8fbaf6b2b7cbe3d7533837f290a671ace2211c34f7378c9fb13389425',
    blockNumber: 45,
    from: '0x2ebfb7af01da3f8e73d2938a928289f2ef633c4e',
    to: '0x6b70d1407fcffa192f9b18658abe485f4db6dac2',
    gasUsed: 134507,
    cumulativeGasUsed: 134507,
    contractAddress: null,
    logs: [ [Object] ],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001000000000000000000000000000000000008800000000000000000000000000000040000',
    rawLogs: [ [Object] ]
  },
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0xdeaed902492c5399b6b4dd719a5de01351f560ec03c5776dd269e6754f8de3b4',
      blockHash: '0xad50fed8fbaf6b2b7cbe3d7533837f290a671ace2211c34f7378c9fb13389425',
      blockNumber: 45,
      address: '0x6B70d1407fcfFa192F9b18658ABE485f4Db6Dac2',
      type: 'mined',
      id: 'log_2d9f031a',
      event: 'ImageCreated',
      args: [Result]
    }
  ]
}
      √ create images


  3 passing (782ms)
  
  ----------------------------
//cmd - console.log(result.logs[0].args)
  PS C:\Users\zchen\Programming\decentragram-starter-code> truffle test
Using network 'development'.


Compiling your contracts...
===========================
> Compiling .\src\contracts\Decentragram.sol
> Compiling .\src\contracts\Decentragram.sol
> Compiling .\src\contracts\Migrations.sol
> Artifacts written to C:\Users\zchen\AppData\Local\Temp\test--14816-pzQlEL64rrKB
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



  Contract: Decentragram
    deployment
      √ deploys successfully
      √ has a name (203ms)
    images
Result {
  '0': BN {
    negative: 0,
    words: [ 1, <1 empty item> ],
    length: 1,
    red: null
  },
  '1': 'abc123',
  '2': 'Image description',
  '3': BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  },
  '4': '0x2EbFB7Af01Da3F8E73D2938A928289F2ef633c4E',
  __length__: 5,
  id: BN {
    negative: 0,
    words: [ 1, <1 empty item> ],
    length: 1,
    red: null
  },
  hash: 'abc123',
  description: 'Image description',
  tipAmount: BN {
    negative: 0,
    words: [ 0, <1 empty item> ],
    length: 1,
    red: null
  },
  author: '0x2EbFB7Af01Da3F8E73D2938A928289F2ef633c4E'
}
      √ create images


  3 passing (757ms)

---------------------------
//test/test.js

const { assert } = require('chai')

const Decentragram = artifacts.require('./Decentragram.sol')

require('chai')
  .use(require('chai-as-promised'))
  .should()

contract('Decentragram', ([deployer, author, tipper]) => {
  let decentragram

  before(async () => {
    decentragram = await Decentragram.deployed()
  })

  describe('deployment', async () => {
    it('deploys successfully', async () => {
      const address = await decentragram.address
      assert.notEqual(address, 0x0)
      assert.notEqual(address, '')
      assert.notEqual(address, null)
      assert.notEqual(address, undefined)
    })

    it('has a name', async () => {
      const name = await decentragram.name()
      assert.equal(name, 'Decentragram')
    })
  })

  describe('images', async () => {
    let result, imageCount
    const hash = 'abc123'

    before(async () => {
      result = await decentragram.uploadImage(hash, 'Image description', { from: author })
      imageCount = await decentragram.imageCount()
    })

    it('create images', async () => {
      //console.log(result)
      //console.log(result.logs[0].args)
      const event = result.logs[0].args
      assert.equal(event.id.toNumber(), imageCount.toNumber(), 'id is correct')
      assert.equal(event.hash, hash, 'Hash is correct')
      assert.equal(event.description, 'Image description', 'description is correct')
      assert.equal(event.tipAmount, '0', 'tip amount is correct')
      assert.equal(event.author, author, 'author is correct')
    })
  })
})

------------------------
//src/contracts/Decentragram.sol

pragma solidity ^0.5.0;

contract Decentragram {
  // Code goes here...
  string public name = "Decentragram";

  uint public imageCount = 0;
  mapping(uint => Image) public images;

  struct Image{
    uint id;
    string hash;
    string description;
    uint tipAmount;
    address payable author;
  }

  event ImageCreated(
    uint id,
    string hash,
    string description,
    uint tipAmount,
    address payable author
  );

  function uploadImage(string memory _imgHash, string memory _description) public {
    imageCount++;

    images[imageCount] = Image(imageCount, _imgHash, _description, 0, msg.sender);

    emit ImageCreated(imageCount, _imgHash, _description, 0, msg.sender);
  }
}

reference:

No comments:

Post a Comment