Payable method that don’t know at once how much amount it worth

Hi all! I recently wondered - how I can create and use payable contract method, that don’t know at once how much amount it worth. For example - random generated amount. I remember - I try run it with web3.js but I’ve got a error, do not remember details, but is happen. If you know - links, materials about that case or can explain - please help!

1 Like

For example:
Just imagine - we're has contract and payable method that generate random number - and its number of amount needs to transfer from callee wallet address (by web3.js) to the contract address in bnb/eth (and so forth) native currency.

Do you mean you want to create a mintable token, a contract that can mint new tokens if the old finishes or get to some certain amount?

Can you elaborate more so I can help?

Sorry, I am not sure what the final question is?

  • How to generate a random number in web3.js?
  • Or how to send a random number to a payable function?
  • Or how to receive a random number of eth in a function?
  • Or something else?

Yes, random value of amount eth/bnb/any_native_currency that should withdraw from user wallet to contract address. That random value generate payable method of contract, client don't know amount before sending transaction.

It's possible or I need make two transaction - first for getting amount from contract, second for sending this amount to contract through payable method

Be carefully, two scammers send me PM:
They nicknames is:
HebrewDEV and ferrariDEV

Thanks for the notice @adminoid. They have been suspended.


check about price oracle contract. It can be added to your contract.

It is possible to create a contract method that can be paid for, but the amount of the payment needs to be specified at the time the method is called. This is because transactions on the Ethereum network, are used to execute contract methods, include a "value" field that specifies the amount of Ether being sent along with the transaction. If you want to create a contract method that generates a random amount and requires payment of that amount, you can do so by having the method generate the random amount, store it in a contract state variable, and then have the method require the caller to send the correct amount of Ether as part of the transaction.

sample code:

pragma solidity ^0.7.0;

contract RandomPayment {
    uint public randomAmount;

    function generateRandomAmount() public {
        randomAmount = uint(keccak256(abi.encodePacked(now, msg.sender))) % 1000;

    function payRandomAmount() public payable {
        require(msg.value == randomAmount, "Incorrect payment amount");
        // Do something with the payment

To call these functions using web3.js, you can use the following code:

const Web3 = require('web3');
const web3 = new Web3(/* your provider */);

const contractAddress = /* your contract address */;
const contract = new web3.eth.Contract(/* your contract ABI */, contractAddress);

// Generate a random amount
await contract.methods.generateRandomAmount().send({ from: /* your address */ });

// Get the random amount generated by the contract
const randomAmount = await contract.methods.randomAmount().call();

// Make a payment of the random amount
await contract.methods.payRandomAmount().send({
    from: /* your address */,
    value: randomAmount,



Thank you very much!!!
I suspected it! But wasn't sure.
I was hoping there was an easier and more beautiful way.