1

I'm trying to create a TicketPurchaser application. Here is the whole code for the smart contract, although I think the problem I'm asking about is a type error, most of the code is not important.

pragma solidity ^0.4.0;



/** @title TicketPurchaser
    @dev Let's the user buy ticket for event, which can be validated by gatekeeper at the event.
    @author imestin
 */
contract TicketPurchaser {
    mapping (bytes6 => address) tickets;                        //One address can buy multiple tickets, so the ticket is the key. 
    mapping (bytes6 => bool) expired;                           //True if ticket is already claimed 
    uint TicketPrice;                                           //Ticket price in wei

    //User can buy a ticket using ether
    function BuyTicket() public payable returns (string) {
        //If this condition is not true, program will exit.
        require(msg.value >= TicketPrice);
        if (msg.value > TicketPrice) {
            msg.sender.transfer(msg.value-TicketPrice);
        }

        SaveCode(GenerateSixDigitCode());
    }

    function GenerateSixDigitCode() private view returns (bytes6) {
//        uint8[6] memory AlphaIndex;                     //We add up 2 hex digit, than multiply it by 1.125 (32 to 36 conversion)
//        string memory AlphaNumerics = new string(36);
//        AlphaNumerics = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//        bytes6 SixDigit;

        //This is not really good, "seed" will be sha256 hash, and we want. 6-digit.
//        bytes32 seed = (keccak256(abi.encodePacked(
//            block.timestamp + block.difficulty +
//            ((uint256(keccak256(abi.encodePacked(block.coinbase)))) / (now)) +
//            block.gaslimit + 
//            ((uint256(keccak256(abi.encodePacked(msg.sender)))) / (now)) +
//            block.number
//        )));

        //This is the string that we are getting the index for: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
//        AlphaIndex[0] = ToAlphaIndex(seed[0], seed[1]);
//        AlphaIndex[1] = ToAlphaIndex(seed[0], seed[1]);
//        AlphaIndex[2] = ToAlphaIndex(seed[0], seed[1]);
//        AlphaIndex[3] = ToAlphaIndex(seed[0], seed[1]);
//        AlphaIndex[4] = ToAlphaIndex(seed[0], seed[1]);
//        AlphaIndex[5] = ToAlphaIndex(seed[0], seed[1]);

        //Let's suppose AlphaIndex worked. 
  /*      SixDigit[0] = AlphaNumerics[AlphaIndex[0]];
        SixDigit[1] = AlphaNumerics[AlphaIndex[1]];
        SixDigit[2] = AlphaNumerics[AlphaIndex[2]];
        SixDigit[3] = AlphaNumerics[AlphaIndex[3]];
        SixDigit[4] = AlphaNumerics[AlphaIndex[4]];
        SixDigit[5] = AlphaNumerics[AlphaIndex[5]];*/

        SixDigit = "X4C9BA";
        return SixDigit;
    }

    //The returning value 
    function ToAlphaIndex(bytes1 x, bytes1 y) private pure returns (uint8) {
        return uint8((uint16(x) + uint16(y)) * 9 / 8);
    }

    //we could simple continou coding this and simulating the 6-digit-generation. This way we could have a working contract
    function SaveCode(bytes6 SixDigit) private {
        // will alter tickets mapping
        // should check if 6-digit-code is alpha-numeric, return true or ERR
        //we don't care about this right now
        tickets[SixDigit] = msg.sender;
    }

    //Gatekeeper can validate the ticket of the user
    //This will alter state, because it is invalidating a ticket.
    function RedeemTicket(bytes6 SixDigit) public payable returns (bool) {
        //User will present 6-digit-code (probalby written down in piece of paper.)

        //Gatekeeper prompts the code. It doesn't really matter who owns the code (you can buy tiket for another person). 
        //The thing that matters, that tickets[code] > 0
        require(tickets[SixDigit] > 0);


        //If tickets[code] > 0, we still need to check that the ticket hasn't been already claimed.
        //If this is true, we set expired[code] = true
        if ( !expired[SixDigit] ) {
            expired[SixDigit] = true;
            return true;
            //Green light "Success!"
        } else {
            return false;                   
            //Already claimed!
        }
    }
}

I'm testing it in Remix. It's obvious that Six-Digit-generation is not working. What I would like to solve right now, is that I'm sending in a fix string ("X4C9BA"), and it will save it, and I will be able to redeem the ticket. (calling RedeemTicket will invalidate the ticket.) I think saving the code works, I checked with "Debug" button, this is in the tickets mapping:

tickets: mapping(bytes6 => address)

     5834433942410000000000000000000000000000000000000000000000000000: 0xCA35B7D915458EF540ADE6068DFE2F44E8FA733C

I assume this is the hex representation of "X4C9BA", but I don't truly understand to be honest, what is 5834433942410000000000000000000000000000000000000000000000000000.

But When I send in "X4C9BA" to RedeemTicket, it gives error.

transact to TicketPurchaser.RedeemTicket errored: Error encoding arguments: Error: invalid bytes6 value (arg="", coderType="bytes6", value="X4C9BA", version=4.0.44)

How to fixthis type error? Thank you for help!

1

You need the hexadecimal representation of the 6-byte value that you want to pass.

First off, it should start with 0x (although Remix might be able to manage without it).

But more importantly, it needs to consist of 12 hexadecimal characters, since each character represents 4 bits, while each byte stores 8 bits (so each byte is represented by 2 characters).

Assuming what you want is the string "X4C9BA", you should find the ASCII code of each character in that string, then convert this code into hexadecimal base, then replace each character with the corresponding result.

Here is a tool which does it all for you, and it seems that you should use 0x885267576665.

| improve this answer | |
  • thank you! it worked! It requires leading '0x' – Imestin Mar 22 at 10:31
  • 1
    @Imestin: You're welcome, I added the outcome in that tool. – goodvibration Mar 22 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.