Skip to content

Instantly share code, notes, and snippets.

@0xPratik
Created April 29, 2022 16:19
Show Gist options
  • Save 0xPratik/b65c56fd72de032ff074926acc630230 to your computer and use it in GitHub Desktop.
Save 0xPratik/b65c56fd72de032ff074926acc630230 to your computer and use it in GitHub Desktop.
const web3 = new Web3("https://bsc-dataseed.binance.org/");
const contract_address = "0x07dE034A0Fc0DA7a0bf703F6DcA7025bcD61BA3e";
const BUSD_token_contract_address = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
// BurgerSwap Contract
const contract = new web3.eth.Contract(contractABI, contract_address);
//BUSD Token Contract
const tokencontract = new web3.eth.Contract(
BUSD_token_abi,
BUSD_token_contract_address
);
// WithDraw Function
const WithDrawBUSD = async (amount) => {
try {
const amt = amount;
const networkId = await web3.eth.net.getId();
const wallet = web3.eth.accounts.privateKeyToAccount(
process.env.OPEN_POOL_PRVTKEY
);
const withdrawAmount = web3.utils.toWei(amt.toString(), "ether");
const tx = contract.methods.withdraw(6, withdrawAmount);
const gas = await tx.estimateGas({ from: wallet.address });
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(wallet.address);
const signedTx = await web3.eth.accounts.signTransaction(
{
to: contract_address,
data,
gas,
gasPrice,
nonce,
chainId: networkId,
},
wallet.privateKey
);
const recipient = await web3.eth.sendSignedTransaction(
signedTx.rawTransaction
);
if (
recipient.transactionHash !== undefined ||
recipient.transactionHash !== null ||
recipient.transactionHash !== ""
) {
const saveTx = new PoolTxs({
signature: recipient.transactionHash,
TxType: "WithDraw",
amount: amount,
});
const res = await saveTx.save();
console.log("RES POOL", res);
}
return recipient.transactionHash;
} catch (error) {
console.log("DEPOSIT ERROR", error);
return error;
}
};
// Deposit
const sendBUSD = async (userId, password, amt) => {
try {
const { prvtKey, pubKey } = await getPrivateKeyById(userId, password);
let tokencontract = new web3.eth.Contract(
BUSD_token_abi,
BUSD_token_contract_address
);
const networkId = await web3.eth.net.getId();
const toAddress = "0x6212d570F0bE4A1F052e28B1D564723f00B44c19";
const tx = tokencontract.methods.transfer(
toAddress,
web3.utils.toWei(amt.toString(), "ether")
);
const gas = await tx.estimateGas({ from: pubKey });
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(pubKey);
const signedTx = await web3.eth.accounts.signTransaction(
{
to: BUSD_token_contract_address,
data,
gas,
gasPrice,
nonce,
chainId: networkId,
},
prvtKey
);
const recipient = await web3.eth.sendSignedTransaction(
signedTx.rawTransaction
);
const hash = recipient.transactionHash;
console.log("HASH", hash);
const updateTx = await User.findOneAndUpdate(
{ _id: userId },
{
$push: {
transactions: {
tx_signature: recipient.transactionHash,
amount: amt,
type: "DEPOSITED",
},
},
}
);
console.log(updateTx);
return recipient.transactionHash;
} catch (error) {
console.log("SEND BUSD TO POOL", error);
return { error: error };
}
};
// Staking
exports.StakeBUSD = async (req, res) => {
try {
console.log("STAKING STARTeD");
const balances = await getBalanceById(req.params.id);
if (balances === null) {
return res.status(400).send({ data: "User Does not Exists" });
}
const data = await User.findById(req.params.id, "pubKey");
const stakeAmount = req.body.amount;
if (balances.bnb < 0.0003) {
console.log("TRANSFER GAS STARTED");
const res = await transferGasBNB(data.pubKey, req.params.id);
console.log("TRANSFERING GAS", res);
}
console.log("STAKING HERE");
console.log("BLA", balances);
if (balances.busd >= 2 && balances.bnb !== 0 && stakeAmount >= 2) {
console.log("sending BUSD started");
const sent = await sendBUSD(
req.params.id,
req.body.password,
stakeAmount
);
if (sent === undefined) {
return res.status(400).send("Error Sending BUSD To Pool");
}
console.log("sending to Pool started");
const depositResult = await DepositToBurgerSwap(stakeAmount);
if (depositResult === undefined) {
return res.status(400).send("Error Depositing BUSD To BurgerSwap");
}
console.log("BURGER SWAP DEPOSIT RESULT", depositResult);
const txDetails = await web3.eth.getTransaction(depositResult);
const BlockDetails = await web3.eth.getBlock(txDetails.blockNumber);
const timestamp = BlockDetails.timestamp;
console.log("TIMESTAMP", timestamp);
if (depositResult !== null) {
console.log("UDPATE Started");
const updateTx = await User.findOneAndUpdate(
{ _id: req.params.id },
{
$push: {
transactions: {
tx_signature: depositResult,
amount: stakeAmount,
type: "STAKED",
unixStamp: timestamp,
},
},
}
);
return res.status(200).send(depositResult);
}
}
} catch (error) {
console.log("STAKING ERROR", error);
res.status(400).send(error);
}
};
const WithdrawBUSDToUser = async (amount, address, userId) => {
try {
let tokencontract = new web3.eth.Contract(
BUSD_token_abi,
BUSD_token_contract_address
);
const wallet = web3.eth.accounts.privateKeyToAccount(
process.env.OPEN_POOL_PRVTKEY
);
const withdrawAmount = web3.utils.toWei(amount.toString(), "ether");
const networkId = await web3.eth.net.getId();
const tx = tokencontract.methods.swap(
address,
web3.utils.toWei(amount.toString(), "ether")
);
const gas = await tx.estimateGas({ from: wallet.address });
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(wallet.address);
const signedTx = await web3.eth.accounts.signTransaction(
{
to: BUSD_token_contract_address,
data,
gas,
gasPrice,
nonce,
chainId: networkId,
},
wallet.privateKey
);
const recipient = await web3.eth.sendSignedTransaction(
signedTx.rawTransaction
);
const hash = recipient.transactionHash;
console.log("HASH", hash);
const updateTx = await User.findOneAndUpdate(
{ _id: userId },
{
$push: {
transactions: {
tx_signature: recipient.transactionHash,
amount: amount,
type: "SWAP",
},
},
}
);
return recipient.transactionHash;
} catch (error) {
console.log(`SWAP ERR from Address ${address}`, error);
return error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment