People keep saying I should share my experiences so here is a quick post of me arguing with ChatGPT and getting help from the community to set up claim function for Terracore.
Although the function is useful it's more of a 'hello world' for me.
So I started the journey with ChatGPT, my only experience with the AI is asking it questions, basically using it instead of Google.
Always be nice to our future overlords!
@echo offecho Installing Node.js...
msiexec /i "https://nodejs.org/dist/v14.17.1/node-v14.17.1-x64.msi" /passiveecho Installing Git...
msiexec /i "https://github.com/git-for-windows/git/releases/download/v2.33.0.windows.2/Git-2.33.0.2-64-bit.exe" /passiveecho Installing Python...
msiexec /i "https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe" /passiveecho Installing MongoDB...
msiexec /i "https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.2-signed.msi" /passiveecho Installing Visual Studio Code...
start "" "https://code.visualstudio.com/docs/?dv=win"echo Installation completed.
I was very aware that ChatGPT only goes up to 2021 but went with it! 😎
Copied and pasted the code from TerraCore and set an environment variable to my active key.
Of course lots of errors, copied and pasted into the AI and it tried to fix everything for me however I was still getting an error which the AI said I needed to add permission to hive to run custom JSON.
After a bit of head banging, web searching and pleading with the AI I decided to ask for help in the TerraCore Discord which was quickly answered. It had nothing to do with permissions but I was using the wrong libraries, hiveio not dsteem!
Many thanks to hackzilla and der_sebi!
Back to ChatGPT
However errors which resulted in me going back and forth between dsteem and hiveio until...
Bingo!
const { Client, PrivateKey } = require('@hiveio/dhive');
const client = new Client('https://api.hive.blog');
const player = {
username: 'hive_username'
};
const amount = 100;
async function claim(player, amount) {
const privateKey = PrivateKey.fromString(process.env.ACTIVE_KEY);
const publicKey = privateKey.createPublic();
const op = [
'custom_json',
{
required_auths: [],
required_posting_auths: [player.username],
id: 'terracore_claim',
json: JSON.stringify({ amount, 'tx-hash': generateHash() })
}
];
const result = await client.broadcast.sendOperations([op], privateKey, [publicKey]);
console.log('Transaction broadcasted:', result);
}
function generateHash() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}
claim(player, amount);
Now that I have a starting point I can experiment with other functions on Hive.
Great fun coding without coding! 🤣
Maybe this helps someone?
Stay Frugal!