2

First of all, sorry about the code written in Portuguese.

I have the following contract:

    contract GerenciadorBoletos {
    
         struct Boleto {
             uint codigoBarra;
             uint codigoBarraDigitavel;
             uint cpfOuCnpjBeneficiario;
             uint cpfOuCnpjPagador;
             uint valorOriginal;
             uint dataVencimento;
         }
    
         mapping(uint => Boleto) public registroBoletos;
    
         function inserirBoleto(
             uint codigoBarra,
             uint codigoBarraDigitavel,
             uint cpfOuCnpjBeneficiario,
             uint cpfOuCnpjPagador,
             uint valorOriginal,
             uint dataVencimento
         ) {
             Boleto memory b = Boleto(
                 codigoBarra,
                 codigoBarraDigitavel,
                 cpfOuCnpjBeneficiario,
                 cpfOuCnpjPagador,
                 valorOriginal,
                 dataVencimento
             );
    
             /* I need to do some validations before store it, but there is no code yet */
    
             registroBoletos[b.codigoBarra] = b;
         }
     }

Then I'm running some mocha test suites with web3.js

    // generates a new registry
    function gerarBoletoValido() {
        return {
            codigoBarra: BigNumber.random(44).toString().slice(2),
            codigoBarraDigitavel: BigNumber.random(47).toString().slice(2),
            cpfOuCnpjBeneficiario: BigNumber.random(14).toString().slice(2),
            cpfOuCnpjPagador: BigNumber.random(11).toString().slice(2),
            valorOriginal: parseInt(Math.random() * 1000),
            dataVencimento: Date.now() + 3*24*3600,
            dataLimitePagamento: Date.now() + 7*24*3600,
            metodoCalculo: MetodoCalculo.INTERNO,
            multa: parseInt(Math.random() * 100),
            juros: parseInt(Math.random() * 100) % 11,
            desconto: parseInt(Math.random() * 100) % 11,
        }
    }

    var boletoValido = gerarBoletoValido();
    it('Deve inserir com sucesso um boleto com os dados válidos', function(done){
         gerenciadorBoleto.inserirBoleto.sendTransaction(
         boletoValido.codigoBarra,
         boletoValido.codigoBarraDigitavel,
         boletoValido.cpfOuCnpjBeneficiario,
         boletoValido.cpfOuCnpjPagador,
         boletoValido.valorOriginal,
         boletoValido.dataVencimento,
         {
             from: web3.eth.accounts[0],
             gas: 3000000,
         },
         function(e, result) {
             expect(e).to.not.exist;
             expect(result).to.exist;
             result.should.be.above(0);
             expect(String(gerenciadorBoleto.registroBoletos(boletoValido.codigoBarra)[0])).to.be.equal(String(boletoValido.codigoBarra));
             done();
         });
    });

The problem is that the last assertion shows that nothing is being inserted:

     Uncaught AssertionError: expected '0' to equal '41227991517789294285860497224073582695415194'
          + expected - actual
    
          -0
          +41227991517789294285860497224073582695415194

What am I missing?

5
  • Just a quick guess. But sendTransaction returns the transaction hash. That's what the '4122...' number is you're getting back
    – dbryson
    May 5, 2016 at 17:47
  • Nop. 4122.. is the BigNumber generated to be inserted. I'm getting back 0 instead. May 5, 2016 at 19:09
  • I suspect that the problem you are encountering is to do with timing. I've just tested 2 mapped structs with an address array - push not working (and previously Getting 'invalid opcode' error) and get an ERROR: invalid jump destination (PUSH1) message with invalid results if I try to retrieve the results immediately after the data-inserting transaction is sent. Try waiting for a extra minute and retest. May 6, 2016 at 9:03
  • If you're using web3.js, and not truffle, then the callback is run when the transaction has been inserted into the blockchain - not when it's actually been executed and included in a block. May 6, 2016 at 10:44
  • I removed the gas parameter and it begun to work. I don't understand why though. May 6, 2016 at 15:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.