Skip to content

Instantly share code, notes, and snippets.

@corbifex
corbifex / event_asset.js
Created Dec 14, 2020
SoldTicket reducer
View event_asset.js
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');
}
View buyTicket.js
await reducerHandler.invoke("event:soldTicket", {
eventId: asset.eventId,
typeId: asset.typeId,
});
View buyTicket.js
await reducerHandler.invoke("token:credit", {
address: organization.ownerAddress,
amount: asset.value,
});
View buyTicket.js
await reducerHandler.invoke("token:debit", {
address: senderAddress,
amount: asset.value,
});
@corbifex
corbifex / wsChannel.js
Last active Dec 2, 2020
Lisk wsChannel workaround
View wsChannel.js
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 Aug 31, 2020
Get the minimal fee for a transactions
View dynamicFees.ts
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 Aug 31, 2020
Verify if a claim for a period is allowed
View claimTransaction.ts
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 Aug 31, 2020
vote weight no calculation
View voteTransaction.ts
const voteWeightNo = votes
.filter(v => v.vote === 0)
.map(v => v.stake)
.reduce((accumulator, currentValue) => accumulator + currentValue);
@corbifex
corbifex / voteTransaction.ts
Created Aug 31, 2020
Calculate vote stake
View voteTransaction.ts
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 Aug 2, 2020
Fund transaction on frontend with dynamic fees and signing
View Fund.js
const tx = new FundContractTransaction({
nonce: nonce.toString(),
senderPublicKey: publicKey,
asset: {
...data
}
});
tx.fee = (tx.minFee + BigInt(65000)).toString();
tx.sign(config.networkIdentifier, passphrase);