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 }); + } +}