Skip to content

Instantly share code, notes, and snippets.

@corbifex
corbifex / event_asset.js
Created December 14, 2020 08:36
SoldTicket reducer
const soldTicket = async ({params, stateStore}) => {
const { eventId, typeId } = params;
const registeredEventsBuffer = await stateStore.chain.get(
CHAIN_STATE_EVENTS
);
if (!registeredEventsBuffer) {
throw new Error('No events found');
}
@corbifex
corbifex / buyTicket.js
Created December 14, 2020 08:35
Sold tickets
await reducerHandler.invoke("event:soldTicket", {
eventId: asset.eventId,
typeId: asset.typeId,
});
@corbifex
corbifex / buyTicket.js
Created December 14, 2020 08:34
Credit tokens
await reducerHandler.invoke("token:credit", {
address: organization.ownerAddress,
amount: asset.value,
});
@corbifex
corbifex / buyTicket.js
Created December 14, 2020 08:33
Token debit
await reducerHandler.invoke("token:debit", {
address: senderAddress,
amount: asset.value,
});
@corbifex
corbifex / wsChannel.js
Last active December 2, 2020 16:48
Lisk wsChannel workaround
import WebSocket from 'isomorphic-ws';
import { EventEmitter } from 'events';
const CONNECTION_TIMEOUT = 2000;
const ACKNOWLEDGMENT_TIMEOUT = 2000;
const RESPONSE_TIMEOUT = 3000;
const timeout = async (ms, message) => new Promise((_, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
@corbifex
corbifex / dynamicFees.ts
Last active August 31, 2020 09:56
Get the minimal fee for a transactions
const tx = {
senderPublicKey: wallet.account.publicKey,
networkIdentifier,
nonce: wallet.account.nonce.toString(),
passphrase: passphrase,
asset: {
fundraiser: fundraiser,
amount: convertLSKToBeddows(amount.toString()),
message
}
@corbifex
corbifex / claimTransaction.ts
Created August 31, 2020 09:29
Verify if a claim for a period is allowed
public allowedToClaim(currentTime: number, startTime: number, payments: Array<Payment>): boolean {
const currentPeriod = this.calculateCurrentPeriod(currentTime, startTime);
if (payments.find(p =>
p.type === 0 &&
p.period === this.asset.period)) {
return false;
}
return currentPeriod >= this.asset.period && (this.asset.period * PERIOD) + startTime < currentTime;
}
@corbifex
corbifex / voteTransaction.ts
Created August 31, 2020 09:25
vote weight no calculation
const voteWeightNo = votes
.filter(v => v.vote === 0)
.map(v => v.stake)
.reduce((accumulator, currentValue) => accumulator + currentValue);
@corbifex
corbifex / voteTransaction.ts
Created August 31, 2020 09:22
Calculate vote stake
public calculateVoteStake(investments: Array<Investment>): bigint {
let voteStake = BigInt(0);
investments.map(investment => {
if (investment.address === this.senderId) {
voteStake += BigInt(investment.amount);
}
});
return voteStake;
}
@corbifex
corbifex / Fund.js
Created August 2, 2020 11:58
Fund transaction on frontend with dynamic fees and signing
const tx = new FundContractTransaction({
nonce: nonce.toString(),
senderPublicKey: publicKey,
asset: {
...data
}
});
tx.fee = (tx.minFee + BigInt(65000)).toString();
tx.sign(config.networkIdentifier, passphrase);