My first time trying to code for blockchain (with help from ChatGPT and community)

in #blog10 months ago

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.

image.png

image.png

Always be nice to our future overlords!


@echo off

echo Installing Node.js...
msiexec /i "https://nodejs.org/dist/v14.17.1/node-v14.17.1-x64.msi" /passive

echo 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" /passive

echo Installing Python...
msiexec /i "https://www.python.org/ftp/python/3.9.6/python-3.9.6-amd64.exe" /passive

echo Installing MongoDB...
msiexec /i "https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.2-signed.msi" /passive

echo 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!

image.png

Many thanks to hackzilla and der_sebi!

Back to ChatGPT
image.png

However errors which resulted in me going back and forth between dsteem and hiveio until...

image.png

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!

Sort:  

PIZZA!
The Hive.Pizza team manually curated this post.

You can now send $PIZZA tips in Discord via tip.cc!

This post has been supported by @Terraboost with a 70% upvote! Delagate HP to Terraboost to Earn Daily HIVE rewards for supporting the @Terracore community!

Play Terracore | Delegate HP | Join Discord

Something pretty important:

@echo off
echo Installing Node.js...
msiexec /i "https://nodejs.org/dist/v14.17.1/node-v14.17.1-x64.msi" /passive

Node 14 is EOL. Node 16 goes EOL as of September. Probably worth using Node.JS 18.

While things might work, new deps might break and probably a good idea to user a more recent version.

Totally agree!
ChatGPT is only aware of events up to 2021