-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eaa9c5e
commit 3f484fa
Showing
8 changed files
with
428 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import BN = require('bn.js') | ||
import { from } from 'rxjs' | ||
import { concatMap } from 'rxjs/operators' | ||
|
||
import { | ||
Operation, | ||
toIOperationObservable | ||
} from '../operation' | ||
|
||
import { Address } from '../types' | ||
|
||
import { Scheme } from '../scheme' | ||
|
||
export class CTL4RScheme { | ||
|
||
constructor(public scheme: Scheme) { | ||
|
||
} | ||
|
||
public async getAgreementHash(): Promise<string> { | ||
const contract = await this.getContract() | ||
const result = await contract.methods.getAgreementHash().call() | ||
return result | ||
} | ||
|
||
public lock(amount: BN, period: number, batchIndexToLockIn: number, agreementHash: string): Operation<any> { | ||
const mapReceipt = (receipt: any) => { | ||
return receipt | ||
} | ||
|
||
const observable = from(this.getContract()) | ||
.pipe( | ||
concatMap((contract) => { | ||
let transaction: any | ||
transaction = contract.methods.lock( | ||
amount, | ||
period, | ||
batchIndexToLockIn, | ||
agreementHash | ||
) | ||
const errorHandler = async (error: Error) => { | ||
try { | ||
await transaction.call() | ||
} catch (err) { | ||
throw err | ||
} | ||
return error | ||
} | ||
return this.scheme.context.sendTransaction(transaction, mapReceipt, errorHandler) | ||
}) | ||
) | ||
return toIOperationObservable(observable) | ||
} | ||
|
||
public extendLocking(extendPeriod: number, | ||
batchIndexToLockIn: number, | ||
lockingId: number, | ||
agreementHash: string): Operation<any> { | ||
const mapReceipt = (receipt: any) => { | ||
return receipt | ||
} | ||
|
||
const observable = from(this.getContract()) | ||
.pipe( | ||
concatMap((contract) => { | ||
let transaction: any | ||
transaction = contract.methods.extendLocking( | ||
extendPeriod, | ||
batchIndexToLockIn, | ||
lockingId, | ||
agreementHash | ||
) | ||
const errorHandler = async (error: Error) => { | ||
try { | ||
await transaction.call() | ||
} catch (err) { | ||
throw err | ||
} | ||
return error | ||
} | ||
return this.scheme.context.sendTransaction(transaction, mapReceipt, errorHandler) | ||
}) | ||
) | ||
return toIOperationObservable(observable) | ||
} | ||
public release(beneficiary: Address, lockingId: number): Operation<any> { | ||
const mapReceipt = (receipt: any) => { | ||
return receipt | ||
} | ||
|
||
const observable = from(this.getContract()) | ||
.pipe( | ||
concatMap((contract) => { | ||
let transaction: any | ||
transaction = contract.methods.release( | ||
beneficiary, | ||
lockingId | ||
) | ||
const errorHandler = async (error: Error) => { | ||
try { | ||
await transaction.call() | ||
} catch (err) { | ||
throw err | ||
} | ||
return error | ||
} | ||
return this.scheme.context.sendTransaction(transaction, mapReceipt, errorHandler) | ||
}) | ||
) | ||
return toIOperationObservable(observable) | ||
} | ||
|
||
public async getContract() { | ||
const state = await this.scheme.fetchStaticState() | ||
await this.scheme.context.fetchContractInfos({fetchPolicy: 'network-only'}) | ||
const contract = this.scheme.context.getContract(state.address) | ||
return contract | ||
} | ||
|
||
public getScheme() { | ||
return this.scheme | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { first } from 'rxjs/operators' | ||
import { Arc, CTL4RScheme } from '../src' | ||
import { newArc } from './utils' | ||
import BN = require('bn.js') | ||
import { createAProposal, getTestAddresses, getTestDAO, voteToPassProposal, waitUntilTrue} from './utils' | ||
import { | ||
IProposalState, | ||
IProposalType, | ||
IProposalStage | ||
} from '../src/proposal' | ||
jest.setTimeout(60000) | ||
/** | ||
* Scheme test | ||
*/ | ||
describe('Scheme', () => { | ||
|
||
let arc: Arc | ||
let accounts: any | ||
|
||
const agreementHash = '0x0000000000000000000000000000000000000001000000000000000000000000' | ||
let continuousLocking4ReputationAddress : any | ||
let dao : any | ||
let proposalToAdd :any | ||
let token :any | ||
beforeAll(async () => { | ||
arc = await newArc() | ||
token = arc.GENToken() | ||
|
||
accounts = arc.web3.eth.accounts.wallet | ||
const contractInfoFactory = arc.getContractInfoByName('ContinuousLocking4ReputationFactory', '0.0.1-rc.55') | ||
const continuousLocking4ReputationFactory = arc.getContract(contractInfoFactory.address) | ||
|
||
// Avatar _avatar, | ||
// uint256 _reputationReward, | ||
// uint256 _startTime, | ||
// uint256 _batchTime, | ||
// uint256 _redeemEnableTime, | ||
// uint256 _maxLockingBatches, | ||
// uint256 _repRewardConstA, | ||
// uint256 _repRewardConstB, | ||
// uint256 _batchesIndexCap, | ||
// IERC20 _token, | ||
// bytes32 _agreementHash | ||
dao = await getTestDAO() | ||
const startTime = (await arc.web3.eth.getBlock('latest')).timestamp | ||
const redeemEnableTime = startTime + 1000 | ||
|
||
continuousLocking4ReputationAddress = await continuousLocking4ReputationFactory.methods.createCL4R( | ||
dao.id, | ||
new BN('10000000'), | ||
startTime, | ||
new BN('1000'), | ||
redeemEnableTime, | ||
new BN('12'), | ||
new BN('85000'), | ||
new BN('900'), | ||
new BN('5'), | ||
token.address, | ||
agreementHash | ||
).call() | ||
|
||
await continuousLocking4ReputationFactory.methods.createCL4R( | ||
dao.id, | ||
new BN('10000000'), | ||
startTime, | ||
new BN('1000'), | ||
redeemEnableTime, | ||
new BN('12'), | ||
new BN('85000'), | ||
new BN('900'), | ||
new BN('5'), | ||
token.address, | ||
agreementHash | ||
).send({gas: 2000000, from:accounts[0].address}) | ||
//now register that to a dao | ||
|
||
proposalToAdd = await createAProposal(dao, { | ||
descriptionHash: '', | ||
parametersHash: '0x0000000000000000000000000000000000000000000000000000000000001234', | ||
permissions: '0x0000001f', | ||
scheme: getTestAddresses(arc).base.SchemeRegistrar, | ||
schemeToRegister: continuousLocking4ReputationAddress, | ||
type: IProposalType.SchemeRegistrarAdd | ||
}) | ||
// accept the proposal by voting the hell out of it | ||
await voteToPassProposal(proposalToAdd) | ||
await proposalToAdd.execute() | ||
|
||
}) | ||
|
||
it('lock and extend lock', async () => { | ||
// check if proposal is indeed accepted etc | ||
const states: IProposalState[] = [] | ||
const lastState = () => states[states.length - 1] | ||
proposalToAdd.state().subscribe(((next: any) => states.push(next))) | ||
|
||
await waitUntilTrue(() => { | ||
return lastState() && lastState().stage === IProposalStage.Executed | ||
}) | ||
const schemes = await dao.schemes({ where: { address: continuousLocking4ReputationAddress.toLowerCase() } }).pipe(first()).toPromise() | ||
const scheme = schemes[0] | ||
expect(scheme.CTL4R).not.toBeFalsy() | ||
const ctl4r = scheme.CTL4R as CTL4RScheme | ||
await ctl4r.getScheme().context.fetchContractInfos({fetchPolicy: 'network-only'}) | ||
expect(await ctl4r.getAgreementHash()).toEqual(agreementHash) | ||
const lockAmount = new BN('300') | ||
await token.approveForStaking(continuousLocking4ReputationAddress.toLowerCase(), lockAmount).send() | ||
await token.mint(accounts[0].address, lockAmount).send() | ||
await arc.fetchContractInfos({fetchPolicy: 'network-only'}) | ||
const continuousLocking4ReputationContract = arc.getContract(continuousLocking4ReputationAddress.toLowerCase()) | ||
const lockCounterBefore = await continuousLocking4ReputationContract.methods.lockCounter().call() | ||
await ctl4r.lock(lockAmount,1,0,agreementHash).send() | ||
const lockCounterAfter = await continuousLocking4ReputationContract.methods.lockCounter().call() | ||
expect(Number(lockCounterBefore)+1).toEqual(Number(lockCounterAfter)) | ||
await ctl4r.extendLocking(2,0,lockCounterAfter,agreementHash).send() | ||
|
||
}) | ||
}) |