How to call rebase function on smart contract automatically every 24 hours on Binance smart chain

Hi,any one suggest me a doc or github source to call rebase function on smart contract automatically for every 24 hours in Binance smart chain.

Smart contracts can not execute functions automatically, so maybe you can write a script to do this.

Hi,thanks@skyge i already created using javascript and i got transaction hash as undefined,i already posted it too on forum,here is the link.and didn't got any solution that's why i moved to smart contract for invoking function automatically.

Is there any source file available for calling rebase function automatically through javascript for Binance smart chain smart contract.

@abcoathup Can he run a defender to do it?
Maybe you can have a look at this article: Running a keeper from Keep3r Network on Defender, but I haven’t tried it yet, I think I should have a try this weekend.

2 Likes

Thanks @Skyge i will check it out.

1 Like

Hi @Prabhakaran,

OpenZeppelin Defender supports Ethereum mainnet and testnets, as well as xDAI sidechain and Sokol testnet. Binance smart chain isn’t currently supported.

Though please let us know if this is of interest, as it is good to know what demand there is for supporting various chains with Defender.


HI @Skyge,

Defender is free to sign up to use on testnets.

https://defender.openzeppelin.com

For Defender Admin you could start with the following guide: Manage an ERC20 token using Defender Admin and Gnosis Safe

2 Likes

Thanks a lot, I will have a try later.

1 Like

A little pity, I forget you are on the BSC-Chain.

1 Like

Hi @Skyge,

OpenZeppelin Defender Autotasks is great for automating this type of task. It is much better than having to manage your own infrastructure along with gas and nonce management yourself.

1 Like

Thanks @abcoathup i am trying to invoke rebase function every 24 hours automatically and important thing is my token and contract are deployed on binance smart chain ,for ethereum openzep contain defender which you suggest.
my doubt is in binance how they are invoking rebase function automatically ?

1 Like

Hi @Prabhakaran,

For your contract on the Binance smart chain, you will need to write a script which creates a transaction to call the function. You will then need to either write a program or use some scheduler to call your script.

You should think about, availability of your infrastructure, security of private keys (as assume you need some funds for the transaction), along with any gas and nonce management that you need to do.

@abcoathup here is my script file that invoke rebase function automatically,this code works fine in rinkeby and i changed it according to binance smart chain and tried it, it doesn’t works and it produce transaction hash as undefined…

const Tx   =  require('ethereumjs-tx').Transaction
const { Contract } = require('ethers')
const Web3 = require('web3')
const web3 = new Web3('https://bsc-dataseed.binance.org/') //your infura id

const account1 = '0xEB50a80F7DE37AF8669b0C4973B2A33E8502c5a7' // Your account address 1
var contractAddress = "0xFFa3D019C2D46973B42358fC6490608584783034"; //Your implementation address
const abiArray =[
	{
		"anonymous": false,
		"inputs": [
			{
				"indexed": true,
				"internalType": "address",
				"name": "previousOwner",
				"type": "address"
			},
			{
				"indexed": true,
				"internalType": "address",
				"name": "newOwner",
				"type": "address"
			}
		],
		"name": "OwnershipTransferred",
		"type": "event"
	},
	{
		"anonymous": false,
		"inputs": [
			{
				"indexed": true,
				"internalType": "address",
				"name": "destination",
				"type": "address"
			},
			{
				"indexed": false,
				"internalType": "uint256",
				"name": "index",
				"type": "uint256"
			},
			{
				"indexed": false,
				"internalType": "bytes",
				"name": "data",
				"type": "bytes"
			}
		],
		"name": "TransactionFailed",
		"type": "event"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "destination",
				"type": "address"
			},
			{
				"internalType": "bytes",
				"name": "data",
				"type": "bytes"
			}
		],
		"name": "addTransaction",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "eBNBpolicyref",
		"outputs": [
			{
				"internalType": "contract eBNBPolicy",
				"name": "",
				"type": "address"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "eBNBpolicyref_",
				"type": "address"
			}
		],
		"name": "initialize",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "owner",
		"outputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "rebase",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "index",
				"type": "uint256"
			}
		],
		"name": "removeTransaction",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "renounceOwnership",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "index",
				"type": "uint256"
			},
			{
				"internalType": "bool",
				"name": "enabled",
				"type": "bool"
			}
		],
		"name": "setTransactionEnabled",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"name": "transactions",
		"outputs": [
			{
				"internalType": "bool",
				"name": "enabled",
				"type": "bool"
			},
			{
				"internalType": "address",
				"name": "destination",
				"type": "address"
			},
			{
				"internalType": "bytes",
				"name": "data",
				"type": "bytes"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "transactionsSize",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "newOwner",
				"type": "address"
			}
		],
		"name": "transferOwnership",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	}
]//abi for contract

const privateKey1 =  Buffer.from(process.env.PRIVATE_KEY_1,'hex')
//const privateKey1 = process.env.PRIVATE_KEY_1;
//new Buffer.from(privateKey1.substring(2,66), 'hex')
console.log("Private Key " + privateKey1);
//const privateKey2 = Buffer.from(process.env.PRIVATE_KEY_2,'hex')
const contract = new web3.eth.Contract(abiArray,contractAddress)
const Rebase = contract.methods.rebase().encodeABI()
web3.eth.getBalance(account1 ,(err,bal)=>{console.log(web3.utils.fromWei(bal,"ether"))})//we print the balance of our account
//this function calls by 1 min interval
function rebase(){
	
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
console.log('Current Timestamp of rebase execution => :', dateTime)	
web3.eth.getTransactionCount(account1, (err,txCount) => {
  console.log("transaction so far",txCount)
    const txObject = {      
    nonce:    web3.utils.toHex(txCount),
    to:       contractAddress,
    gasLimit: web3.utils.toHex(800000),
    gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
    data :Rebase,
	chainId: 56
    
  }
  console.log('Step 1 ');
  const tx =new Tx(txObject);
  //const tx =new Tx(txObject, {chain: 'rinkeby'})
  console.log('Step 2 ');
  tx.sign(privateKey1)
  const serializedTx = tx.serialize()
    console.log('Step 3 ');
  const raw = '0x' + serializedTx.toString('hex')
  web3.eth.sendSignedTransaction(raw, (err, txHash) => {
    console.log('txHash:', txHash)
  })
 
console.log('Completed current execution ');
console.log("Tx Hash "+txObject)
})
web3.eth.getBalance(account1 ,(err,bal)=>{console.log(web3.utils.fromWei(bal,"ether"))})//Here we get the balance of our account after rebase
  console.log('Step 10 ');
}


setInterval(rebase,60000)  //rebase 1min 

herewith i’m attaching the screenshot of output i got when i run this script .

1 Like

Hi @Prabhakaran,

If it is working on Rinkeby, I suggest you try the Binance community: https://community.binance.org/category/44/en-binance-smart-chain

1 Like

Hi @abcoathup @Prabhakaran
I’m trying to implement the same. I’m able to sign the transaction with Metamask from frontend but I also need to send a transaction with nodejs. I didnt get exactly how to do that.
Is web3 the right thing to use in binance or is there a different thing to use.
I found these related
https://docs.binance.org/smart-chain/wallet/wallet_api.html#using-the-provider

1 Like

Hi @godfather,

You likely want to write a nodejs script to send a transaction that you will want to be able to trigger. OpenZeppelin Defender does all this for you on Ethereum mainnet and xDAI.

I assume that Binance smart chain you can just use web3 but you may want to ask in the Binance community: https://community.binance.org/category/44/en-binance-smart-chain

1 Like

@Prabhakaran I have the same problem, but who calls the rebase function for the first time?

1 Like

Hi @micheal, Still we are working on that rebase issue, I am not sure about it for the first time we only call the rebase.

2 Likes

Hello, I also need to know how to call claim function on binance smart contract automatically, I don't know how to change the posted script mentioned here to claim function and where do I put my private key in the script? Please any help appreciated!

OpenZeppelin Defender supports Binance Smart Chain now. Autotasks are well suited to call a function on a schedule.

Please check out defender.openzeppelin.com and if you have further questions post in #support:defender.