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

Commit

Permalink
TEC Registry with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hysz committed Mar 6, 2019
1 parent 2c32902 commit 6675efc
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contracts/tec/compiler.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
}
}
},
"contracts": ["src/TEC.sol", "test/TestLibs.sol", "test/TestMixins.sol"]
"contracts": ["src/TEC.sol", "src/registry/TECRegistry.sol", "test/TestLibs.sol", "test/TestMixins.sol"]
}
45 changes: 45 additions & 0 deletions contracts/tec/contracts/src/registry/MixinTECRegistryCore.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.5.3;
pragma experimental ABIEncoderV2;

import "./interfaces/ITECRegistryCore.sol";


// solhint-disable no-empty-blocks
contract MixinTECRegistryCore is
ITECRegistryCore
{
// mapping from `tecOperator` -> `tecEndpoint`
mapping (address => string) internal tecEndpoints;

/// @dev Called by a TEC operator to set the endpoint of their TEC.
/// @param tecEndpoint endpoint of the TEC.
function setTecEndpoint(string calldata tecEndpoint) external {
address tecOperator = msg.sender;
tecEndpoints[tecOperator] = tecEndpoint;
emit TecEndpointSet(tecOperator, tecEndpoint);
}

/// @dev Gets the endpoint for a TEC.
/// @param tecOperator operator of the TEC endpoint.
function getTecEndpoint(address tecOperator) external view returns (string memory tecEndpoint) {
return tecEndpoints[tecOperator];
}
}
33 changes: 33 additions & 0 deletions contracts/tec/contracts/src/registry/TECRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.5.3;
pragma experimental ABIEncoderV2;

import "./MixinTECRegistryCore.sol";


// solhint-disable no-empty-blocks
contract TECRegistry is
MixinTECRegistryCore
{
constructor ()
public
MixinTECRegistryCore()
{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.5.3;
pragma experimental ABIEncoderV2;


// solhint-disable no-empty-blocks
contract ITECRegistryCore
{
/// @dev Emitted when a TEC endpoint is set.
event TecEndpointSet(
address tecOperator,
string tecEndpoint
);

/// @dev Called by a TEC operator to set the endpoint of their TEC.
/// @param tecEndpoint endpoint of the TEC.
function setTecEndpoint(string calldata tecEndpoint) external;

/// @dev Gets the endpoint for a TEC.
/// @param tecOperator operator of the TEC endpoint.
function getTecEndpoint(address tecOperator) external view returns (string memory tecEndpoint);
}
2 changes: 1 addition & 1 deletion contracts/tec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"lint-contracts": "solhint -c ../.solhint.json contracts/**/**/**/**/*.sol"
},
"config": {
"abis": "./generated-artifacts/@(IExchange|TEC|TestLibs|TestMixins).json",
"abis": "./generated-artifacts/@(TEC|TECRegistry|TestLibs|TestMixins).json",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually."
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions contracts/tec/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import { ContractArtifact } from 'ethereum-types';

import * as TEC from '../generated-artifacts/TEC.json';
import * as TECRegistry from '../generated-artifacts/TECRegistry.json';
import * as TestLibs from '../generated-artifacts/TestLibs.json';
import * as TestMixins from '../generated-artifacts/TestMixins.json';
export const artifacts = {
TEC: TEC as ContractArtifact,
TECRegistry: TECRegistry as ContractArtifact,
TestLibs: TestLibs as ContractArtifact,
TestMixins: TestMixins as ContractArtifact,
};
1 change: 1 addition & 0 deletions contracts/tec/src/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Warning: This file is auto-generated by contracts-gen. Don't edit manually.
* -----------------------------------------------------------------------------
*/
export * from '../generated-wrappers/t_e_c_registry';
export * from '../generated-wrappers/tec';
export * from '../generated-wrappers/test_libs';
export * from '../generated-wrappers/test_mixins';
70 changes: 70 additions & 0 deletions contracts/tec/test/tec_registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { artifacts as exchangeArtifacts } from '@0x/contracts-exchange';
import { chaiSetup, provider, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';

import { TECRegistryTecEndpointSetEventArgs } from '../src';

import { TECRegistryWrapper } from './utils/tec_registry_wrapper';

chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
web3Wrapper.abiDecoder.addABI(exchangeArtifacts.Exchange.compilerOutput.abi);
// tslint:disable:no-unnecessary-type-assertion
describe('TEC Registry tests', () => {
let tecOperator: string;
const tecEndpoint = 'http://sometec.0x.org';
const nilTecEndpoint = '';
let tecRegistryWrapper: TECRegistryWrapper;
// tests
before(async () => {
await blockchainLifecycle.startAsync();
});
after(async () => {
await blockchainLifecycle.revertAsync();
});
before(async () => {
// setup accounts (skip owner)
const accounts = await web3Wrapper.getAvailableAddressesAsync();
[, tecOperator] = accounts;
// deploy tec registry
tecRegistryWrapper = new TECRegistryWrapper(provider);
await tecRegistryWrapper.deployTecRegistryAsync();
});
beforeEach(async () => {
await blockchainLifecycle.startAsync();
});
afterEach(async () => {
await blockchainLifecycle.revertAsync();
});
describe('core', () => {
it('Should successfully set a TEC endpoint', async () => {
await tecRegistryWrapper.setTecEndpointAsync(tecOperator, tecEndpoint);
const recordedTecEndpoint = await tecRegistryWrapper.getTecEndpointAsync(tecOperator);
expect(recordedTecEndpoint).to.be.equal(tecEndpoint);
});
it('Should successfully unset a TEC endpoint', async () => {
// set TEC endpoint
await tecRegistryWrapper.setTecEndpointAsync(tecOperator, tecEndpoint);
let recordedTecEndpoint = await tecRegistryWrapper.getTecEndpointAsync(tecOperator);
expect(recordedTecEndpoint).to.be.equal(tecEndpoint);
// unset TEC endpoint
await tecRegistryWrapper.setTecEndpointAsync(tecOperator, nilTecEndpoint);
recordedTecEndpoint = await tecRegistryWrapper.getTecEndpointAsync(tecOperator);
expect(recordedTecEndpoint).to.be.equal(nilTecEndpoint);
});
it('Should emit an event when setting TEC endpoint', async () => {
// set TEC endpoint
const txReceipt = await tecRegistryWrapper.setTecEndpointAsync(tecOperator, tecEndpoint);
const recordedTecEndpoint = await tecRegistryWrapper.getTecEndpointAsync(tecOperator);
expect(recordedTecEndpoint).to.be.equal(tecEndpoint);
// validate event
expect(txReceipt.logs.length).to.be.equal(1);
const log = txReceipt.logs[0] as LogWithDecodedArgs<TECRegistryTecEndpointSetEventArgs>;
expect(log.args.tecOperator).to.be.equal(tecOperator);
expect(log.args.tecEndpoint).to.be.equal(tecEndpoint);
});
});
});
62 changes: 62 additions & 0 deletions contracts/tec/test/utils/tec_registry_wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { LogDecoder, txDefaults } from '@0x/contracts-test-utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import { TransactionReceiptWithDecodedLogs, ZeroExProvider } from 'ethereum-types';
import * as _ from 'lodash';

import { artifacts, TECRegistryContract } from '../../src';

export class TECRegistryWrapper {
private readonly _web3Wrapper: Web3Wrapper;
private readonly _provider: ZeroExProvider;
private readonly _logDecoder: LogDecoder;
private _tecRegistryContract?: TECRegistryContract;
/**
* Instanitates an ERC20Wrapper
* @param provider Web3 provider to use for all JSON RPC requests
* @param tecOperatorAddresses Addresses of TEC operators
* @param contractOwnerAddress Desired owner of the contract
* Instance of ERC20Wrapper
*/
constructor(provider: ZeroExProvider) {
this._web3Wrapper = new Web3Wrapper(provider);
this._provider = provider;
this._logDecoder = new LogDecoder(this._web3Wrapper, artifacts);
}
public async deployTecRegistryAsync(): Promise<TECRegistryContract> {
this._tecRegistryContract = await TECRegistryContract.deployFrom0xArtifactAsync(
artifacts.TECRegistry,
this._provider,
txDefaults,
);
if (_.isUndefined(this._tecRegistryContract)) {
throw new Error(`Failed to deploy TEC Registry contract.`);
}
return this._tecRegistryContract;
}
public async setTecEndpointAsync(
tecOperator: string,
tecEndpoint: string,
): Promise<TransactionReceiptWithDecodedLogs> {
this._assertTECRegistryDeployed();
const txReceipt = await this._logDecoder.getTxWithDecodedLogsAsync(
await (this._tecRegistryContract as TECRegistryContract).setTecEndpoint.sendTransactionAsync(tecEndpoint, {
from: tecOperator,
}),
);
return txReceipt;
}
public async getTecEndpointAsync(tecOperator: string): Promise<string> {
this._assertTECRegistryDeployed();
const tecEndpoint = await (this._tecRegistryContract as TECRegistryContract).getTecEndpoint.callAsync(
tecOperator,
);
return tecEndpoint;
}
private _assertTECRegistryDeployed(): void {
if (_.isUndefined(this._tecRegistryContract)) {
throw new Error(
'The TEC Registry contract was not deployed through the TECRegistryWrapper. Call `eployTecRegistryAsync` to deploy.',
);
}
}
}
1 change: 1 addition & 0 deletions contracts/tec/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"include": ["./src/**/*", "./test/**/*", "./generated-wrappers/**/*"],
"files": [
"generated-artifacts/TEC.json",
"generated-artifacts/TECRegistry.json",
"generated-artifacts/TestLibs.json",
"generated-artifacts/TestMixins.json"
],
Expand Down

0 comments on commit 6675efc

Please sign in to comment.