Skip to content

Commit ec64c17

Browse files
authored
Get repuation reward for batch (#576)
* Package version 0.2.84 * add getRepuationRewardForBatch * use Decimal.js
1 parent 17550ca commit ec64c17

File tree

8 files changed

+360
-433
lines changed

8 files changed

+360
-433
lines changed

package-lock.json

Lines changed: 297 additions & 407 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@daostack/arc.js",
3-
"version": "0.2.84",
3+
"version": "0.2.85",
44
"description": "",
55
"keywords": [],
66
"main": "dist/lib/index.js",
@@ -112,6 +112,7 @@
112112
"apollo-link-http": "^1.5.15",
113113
"apollo-link-retry": "^2.2.15",
114114
"apollo-link-ws": "^1.0.18",
115+
"decimal.js": "^10.2.1",
115116
"form-data": "^3.0.0",
116117
"graphql": "^14.4.2",
117118
"graphql-tag": "^2.10.1",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export { Reputation, IReputationState, IReputationQueryOptions } from './reputat
1212
export { Reward, IRewardState, IRewardStaticState, IRewardQueryOptions } from './reward'
1313
export { Scheme, ISchemeState, ISchemeStaticState, ISchemeQueryOptions } from './scheme'
1414
export { ReputationFromTokenScheme } from './schemes/reputationFromToken'
15-
export { CTL4RScheme } from './schemes/ctl4rep'
15+
export { CL4RScheme } from './schemes/cl4rep'
1616
export { IContributionReward} from './schemes/contributionReward'
1717
export { hasCompetitionContract, isCompetitionScheme,
1818
IProposalCreateOptionsCompetition,

src/scheme.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { mapGenesisProtocolParams } from './genesisProtocol'
66
import { Operation, toIOperationObservable } from './operation'
77
import { IProposalCreateOptions, IProposalQueryOptions, Proposal } from './proposal'
88
import { ISchemeQueryOptions, ISchemeState, ISchemeStaticState, SchemeBase } from './schemes/base'
9+
import { CL4RScheme } from './schemes/cl4rep'
910
import { CompetitionScheme, isCompetitionScheme } from './schemes/competition'
1011
import * as Competition from './schemes/competition'
1112
import * as ContributionReward from './schemes/contributionReward'
1213
import * as ContributionRewardExt from './schemes/contributionRewardExt'
13-
import { CTL4RScheme } from './schemes/ctl4rep'
1414
import * as GenericScheme from './schemes/genericScheme'
1515
import * as GenericSchemeMultiCall from './schemes/genericSchemeMultiCall'
1616
import { ReputationFromTokenScheme } from './schemes/reputationFromToken'
@@ -185,7 +185,7 @@ export class Scheme extends SchemeBase {
185185
public id: Address
186186
public staticState: ISchemeStaticState | null = null
187187
public ReputationFromToken: ReputationFromTokenScheme | null = null
188-
public CTL4R: CTL4RScheme | null = null
188+
public CTL4R: CL4RScheme | null = null
189189

190190
constructor(idOrOpts: Address | ISchemeStaticState, public context: Arc) {
191191
super(idOrOpts, context)
@@ -205,7 +205,7 @@ export class Scheme extends SchemeBase {
205205
this.ReputationFromToken = new ReputationFromTokenScheme(this)
206206
}
207207
if (this.staticState.name === 'ContinuousLocking4Reputation') {
208-
this.CTL4R = new CTL4RScheme(this)
208+
this.CTL4R = new CL4RScheme(this)
209209
}
210210
}
211211

src/schemes/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
IProposalCreateOptions,
99
IProposalQueryOptions, Proposal } from '../proposal'
1010
import { Address, ICommonQueryOptions, IStateful } from '../types'
11-
import { CTL4RScheme } from './ctl4rep'
11+
import { CL4RScheme } from './cl4rep'
1212
import { ReputationFromTokenScheme } from './reputationFromToken'
1313

1414
export interface ISchemeStaticState {
@@ -249,7 +249,7 @@ export abstract class SchemeBase implements IStateful<ISchemeState> {
249249
public id: Address
250250
public staticState: ISchemeStaticState | null = null
251251
public ReputationFromToken: ReputationFromTokenScheme | null = null
252-
public CTL4R: CTL4RScheme | null = null
252+
public CTL4R: CL4RScheme | null = null
253253

254254
constructor(idOrOpts: Address | ISchemeStaticState, public context: Arc) {
255255
this.context = context
@@ -286,7 +286,7 @@ export abstract class SchemeBase implements IStateful<ISchemeState> {
286286
this.ReputationFromToken = new ReputationFromTokenScheme(this)
287287
}
288288
if (this.staticState.name === 'ContinuousLocking4Reputation') {
289-
this.CTL4R = new CTL4RScheme(this)
289+
this.CTL4R = new CL4RScheme(this)
290290
}
291291
return state
292292
}

src/schemes/ctl4rep.ts renamed to src/schemes/cl4rep.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import BN = require('bn.js')
2+
import {Decimal} from 'decimal.js'
23
import { from } from 'rxjs'
34
import { concatMap } from 'rxjs/operators'
5+
import { realMathToBN, realMathToNumber } from '../utils'
46

57
import {
68
Operation,
@@ -11,12 +13,31 @@ import { Address } from '../types'
1113

1214
import { Scheme } from '../scheme'
1315

14-
export class CTL4RScheme {
16+
export class CL4RScheme {
1517

1618
constructor(public scheme: Scheme) {
1719

1820
}
1921

22+
/**
23+
* getRepuationRewardForBatch
24+
* according to the formula
25+
* repPerBatch = _repRewardConstA * ((_repRewardConstB/1000) ** batchIndex)
26+
* @param repRewardConstA
27+
* @param repRewardConstB
28+
* @param batchIndex the batchIndex to calculate
29+
* @return RepuationRewardForBatch
30+
*/
31+
public async getRepuationRewardForBatch(repRewardConstA: string,
32+
repRewardConstB: string,
33+
batchIndex: number)
34+
: Promise<Decimal> {
35+
const constB = new Decimal(realMathToNumber(new BN(repRewardConstB)))
36+
const constA = new Decimal(realMathToBN(new BN(repRewardConstA)).toString())
37+
return constA.times(constB.toPower(batchIndex))
38+
39+
}
40+
2041
public async getAgreementHash(): Promise<string> {
2142
const contract = await this.getContract()
2243
const result = await contract.methods.getAgreementHash().call()
@@ -25,13 +46,17 @@ export class CTL4RScheme {
2546

2647
public async getReputationRewardForLockingIds(lockingIds: number[],
2748
batchIndex: number,
28-
repuationRewardForPeriod: BN)
29-
: Promise<BN> {
30-
let reputation = new BN('0')
49+
repuationRewardForPeriod: Decimal)
50+
: Promise<Decimal> {
51+
let reputation = new Decimal('0')
3152
const contract = await this.getContract()
32-
const lockingTotalScore = new BN(await contract.methods.batches(batchIndex).call())
53+
const lockingTotalScore = new Decimal(await contract.methods.batches(batchIndex).call())
54+
55+
if (lockingTotalScore.isZero()) {
56+
return lockingTotalScore
57+
}
3358
for (const lockingId of lockingIds) {
34-
const lockingIdScore = new BN(await contract.methods.getLockingIdScore(batchIndex, lockingId).call())
59+
const lockingIdScore = new Decimal(await contract.methods.getLockingIdScore(batchIndex, lockingId).call())
3560
reputation = reputation.add(lockingIdScore.div(lockingTotalScore).mul(repuationRewardForPeriod))
3661
}
3762
return reputation

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ export function realMathToNumber(t: BN): number {
9494
return t.shrn(REAL_FBITS).toNumber() + fraction
9595
}
9696

97+
export function realMathToBN(t: BN): BN {
98+
const REAL_FBITS = 40
99+
const fraction = t.maskn(REAL_FBITS).div((new BN('2')).pow(new BN(REAL_FBITS.toString())))
100+
return t.shrn(REAL_FBITS).add(fraction)
101+
}
102+
97103
export const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'
98104

99105
/**

test/scheme-ctl4rep.spec.ts renamed to test/scheme-cl4rep.spec.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { first } from 'rxjs/operators'
2-
import { Arc, CTL4RScheme } from '../src'
2+
import { Arc, CL4RScheme } from '../src'
33
import { newArc } from './utils'
44
import BN = require('bn.js')
5+
import {Decimal} from 'decimal.js';
56
import { createAProposal, getTestAddresses, getTestDAO, voteToPassProposal, waitUntilTrue} from './utils'
67
import {
78
IProposalState,
@@ -22,6 +23,7 @@ describe('Scheme', () => {
2223
let dao : any
2324
let proposalToAdd :any
2425
let token :any
26+
const REP_CONST_A = '85000000000000000000000'
2527
beforeAll(async () => {
2628
arc = await newArc()
2729
token = arc.GENToken()
@@ -47,12 +49,12 @@ describe('Scheme', () => {
4749

4850
continuousLocking4ReputationAddress = await continuousLocking4ReputationFactory.methods.createCL4R(
4951
dao.id,
50-
new BN('10000000'),
52+
new BN('10000000000000000000000000'),
5153
startTime,
5254
new BN('1000'),
5355
redeemEnableTime,
5456
new BN('12'),
55-
new BN('85000'),
57+
new BN(REP_CONST_A),
5658
new BN('900'),
5759
new BN('5'),
5860
token.address,
@@ -61,12 +63,12 @@ describe('Scheme', () => {
6163

6264
await continuousLocking4ReputationFactory.methods.createCL4R(
6365
dao.id,
64-
new BN('10000000'),
66+
new BN('10000000000000000000000000'),
6567
startTime,
6668
new BN('1000'),
6769
redeemEnableTime,
6870
new BN('12'),
69-
new BN('85000'),
71+
new BN(REP_CONST_A),
7072
new BN('900'),
7173
new BN('5'),
7274
token.address,
@@ -100,22 +102,25 @@ describe('Scheme', () => {
100102
const schemes = await dao.schemes({ where: { address: continuousLocking4ReputationAddress.toLowerCase() } }).pipe(first()).toPromise()
101103
const scheme = schemes[0]
102104
expect(scheme.CTL4R).not.toBeFalsy()
103-
const ctl4r = scheme.CTL4R as CTL4RScheme
104-
await ctl4r.getScheme().context.fetchContractInfos({fetchPolicy: 'network-only'})
105-
expect(await ctl4r.getAgreementHash()).toEqual(agreementHash)
105+
const cl4r = scheme.CTL4R as CL4RScheme
106+
await cl4r.getScheme().context.fetchContractInfos({fetchPolicy: 'network-only'})
107+
expect(await cl4r.getAgreementHash()).toEqual(agreementHash)
106108
const lockAmount = new BN('300')
107109
await token.approveForStaking(continuousLocking4ReputationAddress.toLowerCase(), lockAmount).send()
108110
await token.mint(accounts[0].address, lockAmount).send()
109111
await arc.fetchContractInfos({fetchPolicy: 'network-only'})
110112
const continuousLocking4ReputationContract = arc.getContract(continuousLocking4ReputationAddress.toLowerCase())
111113
const lockCounterBefore = await continuousLocking4ReputationContract.methods.lockCounter().call()
112-
await ctl4r.lock(lockAmount,1,0,agreementHash).send()
114+
await cl4r.lock(lockAmount,1,0,agreementHash).send()
113115
const lockCounterAfter = await continuousLocking4ReputationContract.methods.lockCounter().call()
114116
expect(Number(lockCounterBefore)+1).toEqual(Number(lockCounterAfter))
115-
const reputationRewardPerPeriod = new BN('85000')
116-
const repreward = await ctl4r.getReputationRewardForLockingIds([lockCounterAfter],0,reputationRewardPerPeriod)
117+
const reputationRewardPerPeriod = new Decimal(REP_CONST_A)
118+
const A = await continuousLocking4ReputationContract.methods.repRewardConstA().call()
119+
const B = await continuousLocking4ReputationContract.methods.repRewardConstB().call()
120+
const repreward = await cl4r.getReputationRewardForLockingIds([lockCounterAfter],0,reputationRewardPerPeriod)
117121
expect(repreward).toEqual(reputationRewardPerPeriod)
118-
await ctl4r.extendLocking(2,0,lockCounterAfter,agreementHash).send()
122+
expect(await cl4r.getRepuationRewardForBatch(A,B,0)).toEqual(reputationRewardPerPeriod)
123+
await cl4r.extendLocking(2,0,lockCounterAfter,agreementHash).send()
119124

120125
})
121126
})

0 commit comments

Comments
 (0)