Skip to content

Instantly share code, notes, and snippets.

@KilianB
Created March 26, 2022 09:43
Show Gist options
  • Save KilianB/e3a6d4e7e1297b737934b0e2e7d91dd1 to your computer and use it in GitHub Desktop.
Save KilianB/e3a6d4e7e1297b737934b0e2e7d91dd1 to your computer and use it in GitHub Desktop.
HEDERA_ACCOUNT_ID=0.0.XXXXXXXX
HEDERA_PRIVATE_KEY=YourPrivateKey

Claim Reddish Pixels

This is an example on how to programatically claim pixels on the redish pixel game canvas by sending messages to the HCS (Hedera Consensus Services). Please be aware that dedicated canvases without user cooldown exist for botting.

Prerequisites:

  • NodeJs
  • A Hedera account with at least 0.0001$ of Hbar

Run the following commands to get started

# create directory
mkdir CanvasAutomation
cd CanvasAutomation

# Setup project files 
npm init -y
# Install dependencies
npm i @hashgraph/sdk dotenv typescript

# Create necessary empty files 
touch .env
touch index.ts

# Setup typescript
tsc --init

Copy the content of index.ts and env into the correct files and replace the missing values with your desired input.

You will need to provide:

  • your hedera account id
  • your private key
  • the hcs topic id of the game you want to interact with (additionally location, description and color)

Save the files and convert the .ts file into javascript which can be executed by the node runtime

tsc

An index.js file should now appear alongside the index.ts file. (You can also run tsc --watch) and the index.js file will automatically be regenerated each time you make changes to the typescript file.

node ./index.js
import { AccountId, PrivateKey, Client, TopicMessageSubmitTransaction, Status } from '@hashgraph/sdk';
import { config } from 'dotenv';
/*
* Import personal credentials. You can skip this and hardcode your values.
* But getting used to do it properly can't hurt.
*/
config();
if (process.env.HEDERA_ACCOUNT_ID === undefined) {
throw new Error('Hedera Account Id not specified in env file');
}
if (process.env.HEDERA_PRIVATE_KEY === undefined) {
throw new Error('Hedera Private Key not specified in env file');
}
const ACCOUNT_ID = AccountId.fromString(process.env.HEDERA_ACCOUNT_ID);
const ACCOUNT_KEY = PrivateKey.fromString(process.env.HEDERA_PRIVATE_KEY);
/**
* Specify which pixel you want to claim and how to set its value
*/
// The HCS topic id can be found in the /pixels overview page or copied from the url /pixel/0.0.XXXXXXX
const GAME_HCS_TOPIC_ID = '0.0.XXXXXX';
// Which pixel do you want to claim? [0-size-1]
const pixelX = 0;
const pixelY = 0;
const color = '#00bced';
const description = 'My first pixel claimed via a script';
/*
* Setup entry point with the hedera network
*/
const client = process.env.NODE_ENV === 'development' ? Client.forTestnet() : Client.forMainnet();
client.setOperator(ACCOUNT_ID, ACCOUNT_KEY);
/**
* Send a message to the HCS service and set a pixel on the specified canvas to the supplied color.
*
* @param x the pixels x position on the canvas
* @param y the pixels y position on the canvas
* @param hexColor hex 6 color value of the pixel
* @param description arbitrary text message displayed when looking at the pixel via the homepage
*/
const claimPixel = async (x: number, y: number, hexColor: string, description: string) => {
//Descriptions are truncated after 240 characters on the homepage. You can choose to send a longer message but this only means increased transaction fees.
const descriptionEscaped = description.trim().substring(0, 240);
//Construct payload. This format is specified in the docs found on the homepage.
const payload = `${hexColor}|${x}|${y}|${descriptionEscaped}`;
const submitTx = new TopicMessageSubmitTransaction({
topicId: GAME_HCS_TOPIC_ID,
message: payload,
});
const txSubmitted = await submitTx.execute(client);
const txReceipt = await txSubmitted.getReceipt(client);
if (txReceipt.status != Status.Success) {
console.log('Message could not be sent correctly: ' + txReceipt.status);
} else {
console.log('Message submitted to the network. The update should be visible after 4 - 5 seconds');
}
};
// Finally claim your pixel.
/*
* Here you can run a loop.
* Be aware that we are publishing a transaction to the DLT which incurs a fee.
* Additionally you should spread your load across multiple hcs nodes or introduce
* a small delay or else your traffic might be dropped.
*/
claimPixel(pixelX, pixelY, color, description);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment