|
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); |