0

I've successfully deployed my contract to Rinkeby. Testing on Remix everything worked fine. I'm now writing some tests in Javascript but I don't know how to advance time to test functions in a contract already deployed on Rinkeby. Using the openzeppelin testhelper {time} did not work. Is there a way to advance time in my tests or is that only possible on a Rinkeby fork using ganache-cli?

//--------------------configuring web3 for rinkeby---------------------------------------------------------------------------
const HDWalletProvider = require('@truffle/hdwallet-provider');

// Read in mnemonic from file
const fs = require('fs');
const path = require('path');
const secretPath = path.resolve(__dirname, '..', '.secret');
const mnemonic = fs.readFileSync(secretPath, 'utf8').toString().trim();


const Web3 = require("web3");

const provider = new HDWalletProvider(mnemonic, `wss://rinkeby.infura.io/ws/v3/${infuraID}` )

const web3 = new Web3(provider);

//------------------- requiring some testing Libraries---------------------------------------------------------------------------
require('@openzeppelin/test-helpers/configure')({
  provider: () => new HDWalletProvider(mnemonic, `wss://rinkeby.infura.io/ws/v3/${infuraID}`),
});


const { expectRevert, time } = require('@openzeppelin/test-helpers');
const assert = require('assert');


const compiledContractJson = require('../build/contracts/Tradingbot.json');

process.env.UV_THREADPOOL_SIZE=10;


let accounts;
let contractInstance;

instance before each it-statement
beforeEach(async () => {
  accounts = await web3.eth.getAccounts();

  const deploymentKey = Object.keys(compiledContractJson.networks)[0];

  contractInstance = await new web3.eth.Contract(compiledContractJson.abi, compiledContractJson.networks[deploymentKey].address);
});


describe('Testing Tradingbot_v2', () => {
  it('Contract available on Rinkeby', () => {
    console.log(contractInstance.options.address);
    console.log(accounts);

    assert.ok(contractInstance.options.address);
  });

  it('Buys an initial asset if pool exists', async () => {

    await contractInstance.methods._reset().send({
      from: accounts[0]
    });

    await contractInstance.methods.initialize(100, 90).send({
      from: accounts[0]
    });


    const min = await contractInstance.methods.minAmount().call();
    console.log("Minimum Amount: ", min);
    assert.equal(100, min);

    await contractInstance.methods.contribute().send({
      from: accounts[0],
      value: '1000'
    });

    let balance = await contractInstance.methods.getTokenBalance('0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa').call();
    balance = parseFloat(balance);
    console.log("Balance after first contribution: ", balance);
    assert(balance > 0);

    await contractInstance.methods.contribute().send({
      from: accounts[1],
      value: '1000'
    });

    const oldBalance = balance;
    balance = await contractInstance.methods.getTokenBalance('0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa').call();
    console.log("Balance after second contribution: ", balance);
    assert(balance > oldBalance);

    console.log("Timestamp before: ", await time.latest());
    await time.increase(time.duration.seconds(50));
    console.log("Timestamp after: ", await time.latest());

    const investAmount = parseInt(balance * 0.5);
    await contractInstance.methods.initialAssetBuy('0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', investAmount).send({from: accounts[0]});
    balance = await contractInstance.methods.getTokenBalance('0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984').call();
    console.log("Balance of UNI: ", balance);


  });

This is the error I recieve:

 1 passing (58s)
  1 failing

  1) Testing Tradingbot_v2
       Buys an initial asset if pool exists:
     Error: Provider does not have a request or send method to use.
      at RequestManager.send (node_modules\web3-core-requestmanager\lib\index.js:164:15)
      at sendRequest (node_modules\web3-core-method\lib\index.js:623:42)
      at Eth.send [as getBlock] (node_modules\web3-core-method\lib\index.js:655:13)
      at Object.latest (node_modules\@openzeppelin\test-helpers\src\time.js:36:32)
      at Context.<anonymous> (test\Tradingbot.test.js:216:50)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)



warning: LF will be replaced by CRLF in build/contracts/IWETH9.json.
The file will have its original line endings in your working directory
User (main *) cryptotradingbot_v1
$
1
  • Rinkeby is a public blockchain you can't advance the time. If you were using ganache or forking rinkeby into a test blockchain you will be able to do it.
    – Ismael
    Jan 21, 2022 at 0:41

1 Answer 1

0

Thank you @Ismael for the clarification on Rinkeby. I just was not sure if there were any features available on the testnet. I finally solved my problem by using the following function in my node.js testfile to pause the code execution.

function sleep(ms) { return new Promise((resolve) => { setTimeout(resolve, ms); }); }

Your Answer

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

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