Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Merge c2fcdd3 into 73fc4a1
Browse files Browse the repository at this point in the history
  • Loading branch information
vkonst committed Nov 4, 2020
2 parents 73fc4a1 + c2fcdd3 commit 7f93967
Show file tree
Hide file tree
Showing 65 changed files with 2,124 additions and 2,044 deletions.
1 change: 1 addition & 0 deletions packages/protocol/scripts/deployERC20.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const ERC20Token = require('../build/contracts/ERC20Token.json');

(async () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/protocol/scripts/deployRuleEngine.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const SimpleRestrictedRuleEngine = require('./build/contracts/SimpleRestrictedRuleEngine.json');
/* eslint-disable @typescript-eslint/no-var-requires */
const SimpleRestrictedRuleEngine = require('../build/contracts/SimpleRestrictedRuleEngine.json');


(async () => {
const account = (await web3.eth.getAccounts())[0];
const admin = '0x4880Fdd12855C012F35f97A4109219108C06da0D';
let ruleEngine = new web3.eth.Contract(SimpleRestrictedRuleEngine.abi);
ruleEngine = await ruleEngine.deploy({ data: SimpleRestrictedRuleEngine.bytecode, arguments: [account] }).send({ from: account });
ruleEngine = await ruleEngine.deploy({ data: SimpleRestrictedRuleEngine.bytecode, arguments: [account] })
.send({ from: account });
ruleEngine.methods.addAdmin(admin).send({ from: account });
console.log(ruleEngine.options.address);
})();
89 changes: 44 additions & 45 deletions packages/protocol/scripts/linkLibraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
* Source (adopted): buidler-deploy/src/helpers.ts:
*/

/* global web3, Web3 */
const soliditySha3 = (web3 || Web3).utils.soliditySha3;

module.exports = {
isLinkingNeeded,
linkLibraries,
isLinkingNeeded,
linkLibraries,
}

/**
* @param bytecode
* @return {Boolean} `true` if the bytecode needs linking (contains placeholders)
*/
function isLinkingNeeded(bytecode) {
return bytecode.search("_") > 0;
return bytecode.search("_") > 0;
}

/**
Expand All @@ -37,61 +36,61 @@ function isLinkingNeeded(bytecode) {
* @return {String} bytecode - linked bytecode
*/
function linkLibraries(artifact, libraries) {
let { bytecode, linkReferences } = artifact;
let { bytecode, linkReferences } = artifact;

if (libraries) {
if (linkReferences) {
for (const [ , fileReferences] of Object.entries(linkReferences)) {
for (const [libName, fixups] of Object.entries(fileReferences)) {
const addr = libraries[libName];
if (addr === undefined) continue;
if (libraries) {
if (linkReferences) {
for (const [ , fileReferences] of Object.entries(linkReferences)) {
for (const [libName, fixups] of Object.entries(fileReferences)) {
const addr = libraries[libName];
if (addr === undefined) continue;

for (const fixup of fixups) {
bytecode =
bytecode.substr(0, 2 + fixup.start * 2) +
addr.substr(2) +
bytecode.substr(2 + (fixup.start + fixup.length) * 2);
}
}
}
} else {
bytecode = linkRawLibraries(bytecode, libraries);
for (const fixup of fixups) {
bytecode =
bytecode.substr(0, 2 + fixup.start * 2) +
addr.substr(2) +
bytecode.substr(2 + (fixup.start + fixup.length) * 2);
}
}
}
} else {
bytecode = linkRawLibraries(bytecode, libraries);
}
}

return bytecode;
return bytecode;
}

function linkRawLibraries(bytecode, libraries) {
for (const libName of Object.keys(libraries)) {
const libAddress = libraries[libName];
bytecode = linkRawLibrary(bytecode, libName, libAddress);
}
return bytecode;
for (const libName of Object.keys(libraries)) {
const libAddress = libraries[libName];
bytecode = linkRawLibrary(bytecode, libName, libAddress);
}
return bytecode;
}

function linkRawLibrary(bytecode, libraryName, libraryAddress) {
const address = libraryAddress.replace("0x", "");
const isHashed = (libraryName.startsWith("$") && libraryName.endsWith("$"));
const encodedLibraryName = isHashed
? libraryName.slice(1, libraryName.length - 1)
: hashLibName(libraryName);
const address = libraryAddress.replace("0x", "");
const isHashed = (libraryName.startsWith("$") && libraryName.endsWith("$"));
const encodedLibraryName = isHashed
? libraryName.slice(1, libraryName.length - 1)
: hashLibName(libraryName);

const pattern = new RegExp(`_+\\$${encodedLibraryName}\\$_+`, "g");
if (!pattern.exec(bytecode)) {
throw new Error(
`Can't link '${libraryName}' (${encodedLibraryName}) in \n----\n ${bytecode}\n----\n`
);
}
const pattern = new RegExp(`_+\\$${encodedLibraryName}\\$_+`, "g");
if (!pattern.exec(bytecode)) {
throw new Error(
`Can't link '${libraryName}' (${encodedLibraryName}) in \n----\n ${bytecode}\n----\n`
);
}

return bytecode.replace(pattern, address);
return bytecode.replace(pattern, address);
}

function hashLibName(libName) {
// TODO: fix library name hashing
throw new Error("library name hashing not yet supported");
// "reference" code (from `buidler-deplpyer`): `return solidityKeccak256(["string"], [libName]).slice(2, 36)`
// expected result (example): `hashLibName("CEGEncoder") === "42afaa163c16ede2bfb2fbb9ec69691405"`
// implementation that returns unexpected result:
return soliditySha3({ type: 'string', value: libName }).slice(2, 36);
// TODO: fix library name hashing
throw new Error("library name hashing not yet supported");
// "reference" code (from `buidler-deplpyer`): `return solidityKeccak256(["string"], [libName]).slice(2, 36)`
// expected result (example): `hashLibName("CEGEncoder") === "42afaa163c16ede2bfb2fbb9ec69691405"`
// implementation that returns unexpected result:
return soliditySha3({ type: 'string', value: libName }).slice(2, 36);
}
24 changes: 19 additions & 5 deletions packages/protocol/src/types/ACTUS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@ import { CEGEngine } from './contracts/CEGEngine';
import { PAMEngine } from './contracts/PAMEngine';
import { CERTFEngine } from './contracts/CERTFEngine';
import { STKEngine } from "./contracts/STKEngine";
import dictionary from "./dictionary/dictionary.json";

// Union Types
export type UEngine = ANNEngine | CECEngine | CEGEngine | PAMEngine | CERTFEngine | STKEngine;
export type UTerms = ANNTerms | CECTerms | CEGTerms | PAMTerms | CERTFTerms | STKTerms;

// schedule ids
export const NON_CYLIC_SCHEDULE_ID = '255';
// TODO: Replace hardcoded event values ids with names (#useEventName)
// FP, PR, PY, IP, IPCI, RR, SC, COF, COP, REF, REP, EXE
export const CYCLIC_EVENTS = ['5', '6', '8', '10', '11', '13', '27', '17', '18', '19', '21', '25'];

const eventIndex = (acronym: string): number => (dictionary as any).EventType.allowedValues[acronym];
export const CYCLIC_EVENTS = [
eventIndex('FP'),
eventIndex('PR'),
eventIndex('PY'),
eventIndex('IP'),
eventIndex('IPCI'),
eventIndex('RR'),
eventIndex('SC'),
eventIndex('COF'),
eventIndex('COP'),
eventIndex('REF'),
eventIndex('REP'),
eventIndex('EXE'),
];

// IPS
export interface IPS {
Expand Down Expand Up @@ -133,7 +147,7 @@ export interface Terms {
dividendRecordPeriod: IP;
dividendPaymentPeriod: IP;
splitSettlementPeriod: IP;

cycleOfInterestPayment: IPS;
cycleOfRateReset: IPS;
cycleOfScalingIndex: IPS;
Expand Down Expand Up @@ -790,7 +804,7 @@ export function isSTKTerms (obj: any): obj is STKTerms {
if (obj.quantity == undefined || typeof obj.quantity !== 'number' && typeof obj.quantity !== 'string') { return false; }
if (obj.priceAtPurchaseDate == undefined || typeof obj.priceAtPurchaseDate !== 'number' && typeof obj.priceAtPurchaseDate !== 'string') { return false; }
if (obj.redemptionPrice == undefined || typeof obj.redemptionPrice !== 'number' && typeof obj.redemptionPrice !== 'string') { return false; }

if (!isIP(obj.redemptionRecordPeriod)) { return false; }
if (!isIP(obj.redemptionPaymentPeriod)) { return false; }
if (!isIP(obj.dividendRecordPeriod)) { return false; }
Expand Down
68 changes: 68 additions & 0 deletions packages/protocol/src/types/dictionary/dictionary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"_comments_": "actus-dictionary version: https://github.com/atpar/actus-dictionary/commit/b85b9b378967de6bfc4d8b6687b520c48bce9890",

"ContractType": {
"_comments_": "Enum indexes for ContractType acronyms",
"allowedValues": {
"PAM": 0,
"ANN": 1,
"NAM": 2,
"LAM": 3,
"LAX": 4,
"CLM": 5,
"UMP": 6,
"CSH": 7,
"STK": 8,
"COM": 9,
"SWAPS": 10,
"SWPPV": 11,
"FXOUT": 12,
"CAPFL": 13,
"FUTUR": 14,
"OPTNS": 15,
"CEG": 16,
"CEC": 17,
"CERTF": 18
}
},

"EventType": {
"_comments_": "Enum indexes for EventType acronyms",
"allowedValues": {
"NE": 0,
"CE": 1,
"ISS": 2,
"IED": 3,
"PRD": 4,
"FP": 5,
"PR": 6,
"PD": 7,
"PY": 8,
"PP": 9,
"IP": 10,
"IPCI": 11,
"RRF": 12,
"RR": 13,
"DIF": 14,
"DIX": 15,
"DIP": 16,
"COF": 17,
"COP": 18,
"REF": 19,
"REX": 20,
"REP": 21,
"SPF": 22,
"SPS": 23,
"EXO": 24,
"EXE": 25,
"ST": 26,
"SC": 27,
"IPCB": 28,
"PRF": 29,
"MC": 30,
"TD": 31,
"MD": 32,
"AD": 33
}
}
}
8 changes: 4 additions & 4 deletions packages/protocol/test/ACTUS/Core/TestCore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions packages/protocol/test/ACTUS/Core/TestSchedule.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*global before, beforeEach, describe, it, web3*/
/* eslint-disable @typescript-eslint/no-var-requires */
const assert = require('assert');
const buidlerRuntime = require('@nomiclabs/buidler');

const { getSnapshotTaker, deployTestCore } = require('../../helper/setupTestEnvironment');


/** Covered test cases:
- cycle not set, different overlaps, with or without endtime
- Different overlaps of segment and cycle with or without addEndtime
- start before, end before
- start before, end within
- start before, end after
- start within, end within
- start within, end after
- start after, end after
The tests are divided into multiple parts to avoid a stack which is too deep
*/
- cycle not set, different overlaps, with or without endtime
- Different overlaps of segment and cycle with or without addEndtime
- start before, end before
- start before, end within
- start before, end after
- start within, end within
- start within, end after
- start after, end after
The tests are divided into multiple parts to avoid a stack which is too deep
*/
describe('Core', () => {
/** @param {any} self - `this` inside `before()`/`it()` */
const snapshotTaker = (self) => getSnapshotTaker(buidlerRuntime, self, async () => {
Expand All @@ -33,10 +33,10 @@ describe('Core', () => {
const compactDates = [];

for (date of dates) {
if (date.toString() === '0') { continue; }
if (date.toString() === '0') { continue; }
compactDates.push(date.toString());
}

return compactDates;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/test/ACTUS/Core/TestScheduleACTUS.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global before, beforeEach, describe, it, web3*/
/* eslint-disable @typescript-eslint/no-var-requires */
const assert = require('assert');
const buidlerRuntime = require('@nomiclabs/buidler');

Expand All @@ -24,10 +24,10 @@ describe('Core', () => {
const compactDates = [];

for (date of dates) {
if (date.toString() === '0') { continue; }
if (date.toString() === '0') { continue; }
compactDates.push(date.toString());
}

return compactDates;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/test/ACTUS/Core/TestSignedMath.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global before, beforeEach, describe, it, web3*/
/* eslint-disable @typescript-eslint/no-var-requires */
const assert = require('assert');
const buidlerRuntime = require('@nomiclabs/buidler');
const BigNumber = require('bignumber.js');
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('SignedMath', () => {
// min of 1 and -1 should be -1
assert.strictEqual(await this.TestSignedMath.methods._min(1, -1).call(), '-1')
});

it('should test max', async () => {
// max of 1 and 2 should be 2
assert.strictEqual(await this.TestSignedMath.methods._max(1, 2).call(), '2')
Expand Down

0 comments on commit 7f93967

Please sign in to comment.