Class Name | BaseContract |
Extends | Logger |
Source | base-contract.ts |
Examples | base-contract.spec.ts |
The BaseContract is the base contract class used for
DataContracts <data-contract>
- ServiceContractss
Contracts, that inherit from BaseContracts
, are able to:
- manage a list of contract participants (called "members")
- manage the own state (a flag, that indicate its own life cycle status)
- manage members state (a flag, that indicate the members state in the contract)
What members can do, what non-members cannot do depends of the implementatin of the inheriting contracts.
new BaseContract(options);
Creates a new BaseContract instance.
options
-BaseContractOptions
: options for BaseContract constructor.executor
-Executor
_:Executor
_ instanceloader
-ContractLoader
_:ContractLoader
_ instancenameResolver
-NameResolver
_:NameResolver
_ instancelog
-Function
(optional): function to use for logging:(message, level) => {...}
logLevel
-LogLevel
_ (optional): messages with this level will be logged withlog
logLog
-LogLogInterface
_ (optional): container for collecting log messageslogLogLevel
-LogLevel
_ (optional): messages with this level will be pushed tologLog
BaseContract
instance
const baseContract = new BaseContract({
executor,
loader,
nameResolver,
});
baseContract.createUninitialized(factoryName, accountId[, businessCenterDomain, descriptionDfsHash]);
Create new contract but do not initialize it yet.
The API supports creating contracts, that inhert from BaseContract
. This is done by calling the respective factory. The factory calls are done via a function with this interface:
/// @notice create new contract instance
/// @param businessCenter address of the BusinessCenter to use or 0x0
/// @param provider future owner of the contract
/// @param _contractDescription DBCP definition of the contract
/// @param ensAddress address of the ENS contract
function createContract(
address businessCenter,
address provider,
bytes32 contractDescription,
address ensAddress) public returns (address);
The API supports creating contracts with this function. Contracts created this way may not be ready to use and require an additional function at the contract to be called before usage. This function is usually called init
and its arguments and implementation depends of the specific contract.
The createUninitialized
function performs a lookup for the respective factory contract and calls the createContract
function at it.
factoryName
-string
: contract factory name, used for ENS lookup; if the factory name contains periods, it is threaded as an absolute ENS domain and used as such, if not it will be used as${factoryName}.factory.${businessCenterDomain}
accountId
-string
: Ethereum account idbusinessCenterDomain
-string
(optional): business center in which the contract will be created; usenull
when working without business centerdescriptionDfsHash
-string
(optional): bytes32 hash for description in dfs
Promise
returns string
: Ethereum id of new contract
const contractOwner = '0x...';
const businessCenterDomain = 'testbc.evan';
const contractId = await baseContract.createUninitialized(
'testdatacontract', // factory name
contractOwner, // account, that will be owner of the new contract
businessCenterDomain, // business center, where the new contract will be created
);
baseContract.inviteToContract(businessCenterDomain, contract, inviterId, inviteeId);
Invite user to contract. To allow accounts to work with contract resources, they have to be added as members to the contract. This function does exactly that.
businessCenterDomain
-string
: ENS domain name of the business center the contract was created in; use null when working without business centercontract
-string
: Ethereum id of the contractinviterId
-string
: account id of inviting userinviteeId
-string
: account id of invited user
Promise
returns void
: resolved when done
const contractOwner = '0x0000000000000000000000000000000000000001';
const invitee = '0x0000000000000000000000000000000000000002';
const businessCenterDomain = 'testbc.evan';
const contract = loader.loadContract('BaseContractInterface', contractId);
await baseContract.inviteToContract(
businessCenterDomain,
contractId,
contractOwner,
invitee,
);
To check if an account is a member of a contract, the contract function isMember
can be used:
const isMember = await executor.executeContractCall(contract, 'isConsumer', invitee);
console.log(isMember);
// Output:
// true
baseContract.removeFromContract(businessCenterDomain, contract, accountId, idToBeRemoved);
Remove user from contract. To deny previously invited accounts to work with contract resources, they have to be removed as members from the contract. This function does exactly that.
businessCenterDomain
-string
: ENS domain name of the business center the contract was created in; use null when working without business centercontract
-string
: Ethereum id of the contractaccountId
-string
: account id of executing useridToBeRemoved
-string
: account id which should be removed
Promise
returns void
: resolved when done
const contractOwner = '0x0000000000000000000000000000000000000001';
const idToBeRemoved = '0x0000000000000000000000000000000000000002';
const businessCenterDomain = 'testbc.evan';
const contract = loader.loadContract('BaseContractInterface', contractId);
await baseContract.removeFromContract(
businessCenterDomain,
contractId,
contractOwner,
idToBeRemoved,
);
To check if an account is a member of a contract, the contract function isMember
can be used:
const isMember = await executor.executeContractCall(contract, 'isConsumer', idToBeRemoved);
console.log(isMember);
// Output:
// false
baseContract.changeContractState(contract, accountId, consumerId, state);
set state of a consumer. A members state reflects this members status in the contract. These status values can for example be be Active, Draft or Terminated.
contract
-string|any
: contract instance or contract idaccountId
-string
: Ethereum account idconsumerId
-string
: Ethereum account idstate
-ConsumerState
_: new state
Promise
returns void
: resolved when done
await baseContract.changeConsumerState(contractId, accountId, consumerId, ConsumerState.Active);
ConsumerState
_ is an enum in the BaseContract class, that holds the same state values as the BaseContract.sol. Alternatively integer values matching the enum in BaseContractInterface.sol can be used.
baseContract.changeContractState(contract, accountId, state);
Set state of the contract. The contracts state reflects the current state and how other members may be able to interact with it. So for example, a contract for tasks cannot have its tasks resolved, when the contract is still in Draft state. State transitions are limited to configured roles and allow going from one state to another only if configured for this role.
contract
-string|any
: contract instance or contract idaccountId
-string
: Ethereum account idstate
-ContractState
_: new state
Promise
returns void
: resolved when done
await baseContract.changeContractState(contractId, contractOwner, ContractState.Active);
ContractState
_ is an enum in the BaseContract class, that holds the same state values as the BaseContract.sol. Alternatively integer values matching the enum in BaseContractInterface.sol can be used.
Describes contracts overall state.
In most cases, this property can only be set by the contract owner.
export enum ContractState {
Initial,
Error,
Draft,
PendingApproval,
Approved,
Active,
VerifyTerminated,
Terminated,
};
Describes the state of a consumer or owner in a contract.
In most cases, this can be set the the member, thats status is updated or by a more privileged role, like a contract owner.
export enum ConsumerState {
Initial,
Error,
Draft,
Rejected,
Active,
Terminated
};