Sunday, September 26, 2021

Ethereum Programming 12 upload image 1

//cmd
truffle test

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

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

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

    it('create images', async () => {
      result = await decentragram.uploadImage()
      let image = await decentragram.images(1)
      console.log(image)
    })
  })
})

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

pragma solidity ^0.5.0;

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

  mapping(uint => Image) public images;

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

  function uploadImage() public {

    images[1] = Image(1, 'abc123', 'hello', 0, address(0x0));
  }
}

reference:

No comments:

Post a Comment