Initial work on reputation nft

This commit is contained in:
Ladd Hoffman 2023-01-12 09:04:40 -06:00
parent 75bb48a0e5
commit 92da97fede
1 changed files with 28 additions and 0 deletions

View File

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