Skip to content

Instantly share code, notes, and snippets.

@pfrazee
Last active January 17, 2022 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfrazee/b6adba211a6a86ae6821f7fc5da2d136 to your computer and use it in GitHub Desktop.
Save pfrazee/b6adba211a6a86ae6821f7fc5da2d136 to your computer and use it in GitHub Desktop.
/**
* Counter
*
* This contract maintains a single numeric value which can only be incremented.
*/
import { index } from 'contract'
// database api
// =
export async function get () {
const entry = await index.get(`/counter`)
return Number(entry?.value || 0)
}
export function increment (_, emit) {
emit({op: 'INCREMENT'})
}
// transaction handler
// =
export const apply = {
async INCREMENT (tx, op) {
const current = await get()
tx.put(`/counter`, current + 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment