Last active
January 17, 2022 22:25
-
-
Save pfrazee/b6adba211a6a86ae6821f7fc5da2d136 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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