From 92da97fede8acc59a5c4d63bdd75b90776a8e419 Mon Sep 17 00:00:00 2001 From: Ladd Hoffman Date: Thu, 12 Jan 2023 09:04:40 -0600 Subject: [PATCH] Initial work on reputation nft --- forum-network/src/classes/reputation-token.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 forum-network/src/classes/reputation-token.js diff --git a/forum-network/src/classes/reputation-token.js b/forum-network/src/classes/reputation-token.js new file mode 100644 index 0000000..6e48648 --- /dev/null +++ b/forum-network/src/classes/reputation-token.js @@ -0,0 +1,28 @@ +import { ERC721 } from './erc721.js'; +import { CryptoUtil } from './crypto.js'; + +export class ReputationToken extends ERC721 { + constructor() { + super('Reputation', 'REP'); + this.histories = new Map(); // token id --> {increment, context (i.e. validation pool id)} + this.values = new Map(); // token id --> current value + } + + mint(to, value, context) { + const tokenId = CryptoUtil.randomUUID(); + super.mint(to, tokenId); + this.values.set(tokenId, value); + this.histories.set(tokenId, [{ increment: value, context }]); + } + + incrementValue(tokenId, increment, context) { + const value = this.values.get(tokenId); + const newValue = value + increment; + const history = this.histories.get(tokenId); + if (newValue < 0) { + throw new Error('Token value can not become negative'); + } + this.values.set(tokenId, newValue); + history.push({ increment, context }); + } +}