Skip to content

Instantly share code, notes, and snippets.

@BellaBe
Last active July 31, 2023 18:31
Show Gist options
  • Save BellaBe/55861311d3d6a9346bb6b0c87fface8a to your computer and use it in GitHub Desktop.
Save BellaBe/55861311d3d6a9346bb6b0c87fface8a to your computer and use it in GitHub Desktop.
How to interact with Pancakeswap Prediction Smart Contract
const { default: axios } = require("axios");
const Web3 = require("web3");
const provider = "https://bsc-dataseed1.binance.org:443";
const web3 = new Web3(provider);
const contractAddress = "0x18B2A687610328590Bc8F2e5fEdDe3b582A49cdA";
const execute = async() => {
const response = await axios.get(`https://api.bscscan.com/api?module=contract&action=getabi&address=${contractAddress}&apikey=YOUR_API_KEY`)
const contract = new web3.eth.Contract(JSON.parse(response.data.result), contractAddress);
const epoch = await contract.methods.currentEpoch().call(); // read function
const roundData = await contract.methods.rounds(epoch).call(); // read function
const gasPrice = await web3.eth.getGasPrice(); // blockchain utility function
const betBullFunctionABI = contract.methods.betBull(epoch).encodeABI(); // prepare write function
const betBearFunctionABI = contract.methods.betBear(epoch).encodeABI(); // prepare write function
const custodialAddress = "your_address";
const custodialAddressKey = "your_address_key";
const account = web3.eth.accounts.privateKeyToAccount(custodialAddressKey); // used to sign transaction
const nonce = await web3.eth.getTransactionCount(custodialAddress); // get your account nonce
const transaction = {
nonce,
from: custodialAddress,
to: contractAddress,
gasPrice,
gas: 5000000,
data: betBullFunctionABI,
value: "12008140256521106"
} // compose transaction object
const signedTransaction = await account.signTransaction(transaction); // sign transaction
const sentTransaction = await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction); // send transaction to blockchain
console.log(sentTransaction)
const isClaimable = await contract.methods.claimable(epoch, custodialAddress).call(); // read function
if(isClaimable){ // recompose transaction to claim wins
const claimFunctionABI = contract.methods.claim([epoch]).encodeABI(); // prepare write function
const nonce = await web3.eth.getTransactionCount(custodialAddress); // get your account nonce
const claimTransaction = {
nonce,
from: custodialAddress,
to: bnbPredictionContractAddress,
gasPrice,
gas: 5000000,
data: claimFunctionABI,
}; // compose transaction object
const signedClaimTransaction = await account.signTransaction(claimTransaction); // sign transaction
const sentClaimTransaction = await web3.eth.sendSignedTransaction(signedClaimTransaction.rawTransaction); // send transaction to blockchain
console.log(sentClaimTransaction)
}
}
execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment