Service Contract¶
Class Name | ServiceContract |
---|---|
Extends | Logger |
Source | service-contract.ts |
Examples | service-contract.spec.ts |
constructor¶
new ServiceContract(options);
Creates a new ServiceContract instance.
Parameters¶
options
-ServiceContractOptions
: options for ServiceContract constructor.cryptoProvider
-CryptoProvider
:CryptoProvider
instancedfs
-DfsInterface
:DfsInterface
instancekeyProvider
-KeyProvider
:KeyProvider
instancesharing
-Sharing
:Sharing
instanceweb3
-Web3
:Web3
instancedefaultCryptoAlgo
-string
(optional): crypto algorith name fromCryptoProvider
, defaults toaes
log
-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
Returns¶
ServiceContract
instance
Example¶
const serviceContract = new ServiceContract({
cryptoProvide,
dfs,
executor,
keyProvider,
loader,
nameResolver,
sharing,
web3,
});
create¶
serviceContract.create(accountId, businessCenterDomain, service[, descriptionDfsHash]);
create and initialize new contract
Parameters¶
accountId
-string
: owner of the new contract and transaction executorbusinessCenterDomain
-string
: ENS domain name of the business centerservice
-any
: service definitiondescriptionHash
-string
(optional): bytes2 hash of DBCP description, defaults to0x0000000000000000000000000000000000000000000000000000000000000000
Returns¶
Promise
returns any
: contract instance
Example¶
const serviceContract = await serviceContract.create(accounts[0], businessCenterDomain, sampleService);
= Service =¶
The service is the communication pattern definition for the ServiceContract
. A single service contract can only have one service definition and all calls and answers must follow its defition.
To create calls and answers with different patterns, create a new ServiceContract
and use an updated service definition there.
setService¶
serviceContract.setService(contract, accountId, service, businessCenterDomain[, skipValidation]);
Set service description.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDservice
-any
: service to setbusinessCenterDomain
-string
: domain of the business the service contract belongs toskipValidation
-bool
(optional): skip validation of service definition, validation is enabled by default
Returns¶
Promise
returns void
: resolved when done
Example¶
await serviceContract.setService(contract, accounts[0], sampleService, businessCenterDomain);
getService¶
serviceContract.getService(contract, accountId);
Gets the service of a service contract.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account ID
Returns¶
Promise
returns string
: service description as JSON string
= Calls =¶
Calls are the requests done by authors, that initiate a service conversation. They are basically the first part of conversations and allow answers to be added to them. Calls are usually broadcasted or multicasted.
Samples for calls are:
- capacity requests
- information requests
- information broadcasts
sendCall¶
serviceContract.sendCall(contract, accountId, call);
Send a call to a service.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDcall
-any
: call to send
Returns¶
Promise
returns number
: id of new call
getCalls¶
serviceContract.getCalls(contract, accountId[, count, offset, reverse]);
Get all calls from a contract.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDcount
-number
(optional): number of elments to retrieve, defaults to10
offset
-number
(optional): skip this many elements, defaults to0
reverse
-boolean
(optional): retrieve last elements first, defaults tofalse
Returns¶
Promise
returns any[]
: the calls
getCall¶
serviceContract.getCall(contract, accountId, callId);
Get a call from a contract.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDcallId
-number
: index of the call to retrieve
Returns¶
Promise
returns any
: a single call
getCallCount¶
serviceContract.getCallCount(contract);
Get number of calls of a contract.
Parameters¶
contract
-any|string
: smart contract instance or contract ID
Returns¶
Promise
returns number
: number of calls
Example¶
let callCount = await serviceContract.getCallCount(contract);
console.log(callCount);
// Output:
// 2
await serviceContract.sendCall(contract, accounts[0], sampleCall);
callCount = await serviceContract.getCallCount(contract);
console.log(callCount);
// Output:
// 3
getCallOwner¶
serviceContract.getCallOwner(contract, callId);
Gets the owner/creator of a call.
Parameters¶
contract
-any|string
: smart contract instance or contract IDcallId
-number
: index of the call to retrieve owner for
Returns¶
Promise
returns string
: account id of call owner
Example¶
console.log(await serviceContract.getCallOwner(contract, 2));
// Output:
0x0000000000000000000000000000000000000001
addToCallSharing¶
serviceContract.addToCallSharing(contract, accountId, callId, to[, hashKey, contentKey, section]);
Adds list of accounts to a calls sharings list.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDcallId
-number
: id of the call to retrieveto
-string[]
: accountIds, to add sharings forhashKey
-string
(optional): hash key to share, if omitted, key is retrieved withaccountId
contentKey
-string
(optional): content key to share, if omitted, key is retrieved withaccountId
section
-string
(optional): section to share key for, defaults to ‘*’
Returns¶
Promise
returns void
: resolved when done
Example¶
// account[0] adds accounts[2] to a sharing
await serviceContract.addToCallSharing(contract, accounts[0], callId, [accounts[2]]);
= Answers =¶
Answers are replies to calls. Answers can only be created as answers to calls. Answers are usually directed to the author of a call.
Examples are
- capacity replies
- information responses
sendAnswer¶
serviceContract.sendAnswer(contract, accountId, answer, callId, callAuthor);
Send answer to service contract call.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDanswer
-any
: answer to sendcallId
-number
: index of the call to which the answer was createdcallAuthor
-string
: Ethereum account ID of the creator of the initial call
Returns¶
Promise
returns number
: id of new answer
Example¶
await serviceContract.inviteToContract(businessCenterDomain, contract.options.address, accounts[0], accounts[2]);
const contentKey = await sharing.getKey(contract.options.address, accounts[0], '*', 0);
await sharing.addSharing(contract.options.address, accounts[0], accounts[2], '*', 0, contentKey);
await serviceContract.sendCall(contract, accounts[0], sampleCall);
const call = await serviceContract.getCall(contract, accounts[0], 0);
const answerId = await serviceContract.sendAnswer(contract, accounts[2], sampleAnswer, 0, call.metadata.author);
getAnswers¶
serviceContract.getAnswers(contract, accountId, callid[, count, offset, reverse]);
Retrieves answers for a given call.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDcallId
-number
: index of the call to which the answers were createdcount
-number
(optional): number of elements to retrieve, defaults to10
offset
-number
(optional): skip this many elements, defaults to0
reverse
-boolean
(optional): retrieve last elements first, defaults tofalse
Returns¶
Promise
returns any[]
: the answers
getAnswer¶
serviceContract.getAnswer(contract, accountId, answerIndex);
Get a answer from a contract.
Parameters¶
contract
-any|string
: smart contract instance or contract IDaccountId
-string
: Ethereum account IDcallId
-number
: index of the call to which the answer was createdanswerIndex
-number
: index of the answer to retrieve
Returns¶
Promise
returns any
: a single answer
getAnswerCount¶
serviceContract.getAnswerCount(contract, callId);
Retrieves number of answers for a given call.
Parameters¶
contract
-any|string
: smart contract instance or contract IDcallId
-number
: index of the call to which the answer was created
Returns¶
Promise
returns number
: number of answers
Example¶
const sampleCallId = 3;
let answerCount = await serviceContract.getAnswerCount(contract, sampleCallId);
console.log(answerCount);
// Output:
// 2
await serviceContract.sendAnswer(contract, accounts[0], sampleAnswer, sampleCallId, accounts[1]);
answerCount = await serviceContract.getAnswerCount(contract, sampleCallId);
console.log(answerCount);
// Output:
// 3