Skip to content

Instantly share code, notes, and snippets.

View kristjank's full-sized avatar
💻
...

Kristjan Kosic kristjank

💻
...
View GitHub Profile
@kristjank
kristjank / blockchain-state.ts
Created June 12, 2019 12:36
Interacting with the state of the blockchain
import { app } from "@arkecosystem/core-container";
const blockchain = app.resolvePlugin("blockchain");
async function callBlockchainMethods() {
// Check if the blockchain is fully synced
blockchain.isSynced();
// Get the last block we've received
blockchain.getLastBlock();
@kristjank
kristjank / database-interaction.ts
Created June 12, 2019 12:35
Retrieving blocks and transactions from blockchain
import { app } from "@arkecosystem/core-container";
const database = app.resolvePlugin("database");
async function callDatabaseMethods() {
// Get a block from the database by its id
await database.getBlock("some block id");
// Skip the first 100 blocks, grab the next 100
await database.getBlocks(100, 100);
@kristjank
kristjank / event-listen.ts
Created June 12, 2019 12:33
Listening to events on blockchain
import { app } from "@arkecosystem/core-container";
import { EventEmitter, Logger } from "@arkecosystem/core-interfaces";
const logger = app.resolvePlugin<Logger.ILogger>("logger");
const emitter = app.resolvePlugin<EventEmitter.EventEmitter>("event-emitter");
emitter.on("forger.missing", delegate => {
// This will be a wallet object that contains information like the address, username, public key, votes, etc.
logger.warn(`${delegate.username} just missed a block.`);
@kristjank
kristjank / server-create.ts
Created June 12, 2019 12:32
Creating http server and serve endpoints on blockchain
import { createServer, mountServer } from "@arkecosystem/core-http-utils";
export async function startServer(config) {
const server = await createServer({
host: config.host,
port: config.port
});
server.route({
method: "GET",
@kristjank
kristjank / fee-simulator.js
Created December 4, 2018 17:29
dynamic-fee-simulator.js
const {
dynamicFeeManager,
transactionBuilder,
formatArktoshi,
} = require('@arkecosystem/crypto')
const transaction = transactionBuilder
.transfer()
.recipientId('D9YiyRYMBS2ofzqkufjrkB9nHofWgJLM7f')
.sign('secret passphrase')
@kristjank
kristjank / dynamic-fee-matcher.js
Created September 24, 2018 10:11
Dynamic fee selection method
/**
* Determine if transaction matches the accepted fee by delegate or max fee set by sender
* @param {Transaction} Transaction - transaction to check
* @return {Boolean} matches T/F
*/
module.exports = (transaction) => {
const transactionFee = transaction.fee.toNumber()
const staticFee = feeManager.getForTransaction(transaction)
const blockchain = container.resolvePlugin('blockchain')
const feeConstants = config.getConstants(blockchain.getLastBlock().data.height).fees
@kristjank
kristjank / Network.json
Last active September 25, 2018 13:28
Dynamic fee settings in network.json
// Found in networks config folder in crypto/lib/networks
"dynamicOffsets": {
"transfer": 100,
"secondSignature": 250,
"delegateRegistration": 500,
"vote": 100,
"multiSignature": 500,
"ipfs": 250,
"timelockTransfer": 500,
"multiPayment": 500,
@kristjank
kristjank / dynamic-fee.js
Last active August 16, 2019 09:02
Dynamic Fee calculation
/** Calculates delegate fee for processing and forging if transaction
* @param {Number} Fee price per byte in ARKTOSHI as set by forger/delegate in delegate.json setting feeMultiplier
* @param {Transaction} Transaction for which we calculate dynamic fee
* @returns {Number} Calculated dynamic fee in ARKTOSHI
*/
calculateFee (feeMultiplier, transaction) {
if (feeMultiplier <= 0) {
feeMultiplier = 1
}
// We get the transactions offset for a transaction type and
@kristjank
kristjank / Transaction payload - serialised.json
Created September 24, 2018 09:56
Serialised transaction
// serialised transaction Transfer
// serialised.length = 160
{"serialized":
{
"type":"Buffer",
"data":[255,1,30,0,22,116,215,2,3,215,223,228,78,119,16,57,51,79,71,18,251,149,173,53,82,84,246,116,200,245,210,134,80,49,153,21,123,123,247,195,87,14,1,0,0,0,0,0,0,6,84,73,68,58,32,48,0,194,235,11,0,0,0,0,15,0,0,0,30,72,161,107,90,67,151,232,118,184,105,194,229,52,233,123,39,157,230,102,177,48,68,2,32,69,177,255,192,154,14,176,62,80,182,75,185,130,144,48,143,68,177,152,153,1,75,93,101,109,175,41,10,149,220,23,79,2,32,15,59,8,76,121,202,240,45,147,56,172,137,149,29,183,136,156,137,156,69,81,149,205,254,98,22,126,19,61,71,244,227]
}
}
@kristjank
kristjank / Transaction payload - type transfer.json
Created September 24, 2018 09:52
Transaction payload - type transfer.json
// dynamic transaction payload
{
"verified":true,
"id":"afaa18bfc29762b7b5172fa1f71ea7471c0d9e6dcf2992638865ecb4b3d1f26b",
"version":1,
"timestamp":47674390,
"senderPublicKey":"03d7dfe44e771039334f4712fb95ad355254f674c8f5d286503199157b7bf7c357",
"recipientId":"DBm8ZgZvaDhgba9jyFNVwDGD8ysbN3w8Tv",
"type":0,
"vendorFieldHex":"5449443a2030",