Created
December 5, 2019 22:02
-
-
Save thr0wn/e57b03ce919a4053a6d7c62095462646 to your computer and use it in GitHub Desktop.
WIP - Dice robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sendBch = require("./send-bch"); | |
let BITBOX = require("bitbox-sdk").BITBOX; | |
let bitbox = new BITBOX(); | |
const Mnemonic = | |
"..."; | |
const Addrx2 = "bitcoincash:qz9cq5rlkdrjy2zkfzqscq847q9n07mu5y7hj8fcge"; | |
const Addrx10 = "bitcoincash:qz9cq5pgwgx68wfevx0t78xalkh33xa0v5wlx6nppx"; | |
const getWallet = BITBOX => { | |
const rootSeedBuffer = BITBOX.Mnemonic.toSeed(Mnemonic); | |
const masterHDNode = BITBOX.HDNode.fromSeed(rootSeedBuffer); | |
const hdNodeBip44Account = BITBOX.HDNode.derivePath( | |
masterHDNode, | |
"m/44'/145'/0'" | |
); | |
const change = BITBOX.HDNode.derivePath(hdNodeBip44Account, "0/0"); | |
const cashAddress = BITBOX.HDNode.toCashAddress(change); | |
return { | |
cashAddress, | |
fundingWif: BITBOX.HDNode.toWIF(change), | |
change | |
}; | |
}; | |
const getBalance = async (BITBOX, wallet, logs = true) => { | |
const log = logs ? console.log.bind(console) : () => null; | |
try { | |
let bitcoinCashBalance = await BITBOX.Address.details(wallet.cashAddress); | |
return { | |
...bitcoinCashBalance, | |
totalBalance: | |
bitcoinCashBalance.balance + bitcoinCashBalance.unconfirmedBalance | |
}; | |
} catch (err) { | |
log(`Error in getBalance: `, err.message); | |
throw err; | |
} | |
}; | |
const delay = () => | |
new Promise(resolve => setTimeout(resolve, (Math.random() * 5 + 5) * 1000)); | |
(async () => { | |
try { | |
const wallet = getWallet(bitbox); | |
let victories = 0; | |
let balance = await getBalance(bitbox, wallet); | |
while (true) { | |
if (balance.totalBalance >= 0.01) { | |
try { | |
if (victories > 0) { | |
console.info("Betting x10..."); | |
await sendBch(bitbox, wallet, { | |
addresses: [Addrx10], | |
values: [0.01], | |
balance: balance.totalBalance | |
}); | |
} else { | |
console.info("Betting x2..."); | |
await sendBch(bitbox, wallet, { | |
addresses: [Addrx2], | |
values: [0.01], | |
balance: balance.totalBalance | |
}); | |
} | |
} catch (err) { | |
await delay(); | |
continue; | |
} | |
console.info("..."); | |
await delay(); | |
const newBalance = await getBalance(bitbox, wallet); | |
if (newBalance.totalBalance > balance.totalBalance) { | |
console.info("Win!"); | |
victories++; | |
} else { | |
console.info("Lose!"); | |
victories--; | |
} | |
balance = newBalance; | |
} else { | |
console.info("0 balance"); | |
return; | |
} | |
} | |
} catch (error) { | |
console.trace(error.stack); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment