The Smart Contract API is available under the "api" global object
There is a variable called "owner" that holds the id of the owner of the Smart Contract. By using this variable, you can restrict certain actions to the owner exclusively.
For example:
actions.removeUser = async (payload) => {
if (api.sender !== api.owner) return;
const { userId } = payload;
if (userId && typeof userId === 'string') {
let user = await api.db.findOne('users', { 'id': userId });
if (user)
await api.db.remove('users', user);
}
}
From a Smart Contract, you can call another Smart Contract simply by using the following global functions:
executeSmartContract(contract: string, action: string, payload: JSON stringified)
The Smart Contract will be executed with the same sender as the current sender.
For example:
actions.addUser = async (payload) => {
const newUser = {
'id': sender
};
await api.db.insert('users', newUser);
await api.executeSmartContract('books_contract', 'addBook', '{ "title": "The Awesome Book" }')
}
executeSmartContractAsOwner(contract: string, action: string, payload: JSON stringified)
The Smart Contract will be executed with the sender set as the current contract owner.
For example:
actions.transferTokensToUser = async (payload) => {
const { to } = payload;
await api.executeSmartContractAsOwner('tokens', 'transfer', { symbol: 'TKN', quantity: 123.456, to });
}
3. Transfer tokens from a smart contract to a user or another contract (available only if the "tokens" smart contract has been deployed)
Smart Contracts have the ability to hold tokens that can only be moved within the Smart Contract itself. This can be done via the global function:
transferTokens(to: string, symbol: string, quantity: number, type: string ('user' | 'contract'))
The transfer will be executed with the 'null' account set as the sender.
For example:
actions.sendRewards = async function (payload) {
const { to, quantity } = payload;
await api.transferTokens(to, 'TKN', quantity, 'user');
}
When a transaction is sent via a Steem transfer you can access to two variables in the payload of the action:
- recipient: give you the username Steem of the recipient
- amountSTEEMSBD: give you the amount of STEEM or SBD sent (ie. "10.000 STEEM" or "10.000 SBD")
When a transaction is sent via a custom_json operation you can access to the following variable via the payload:
- isSignedWithActiveKey: true if the custom_json was signed with the active key of the account, false otherwise
The Steem block number from which the transaction was initiated is available via the following variable:
- api.refSteemBlockNumber: Steem block number
The transaction id from which the transaction was initiated is available via the following variable:
- api.transactionId: transaction id
A deterministic random number generator is available via the following function:
- api.random(): returns a random number between 0 and 1 based on the Steem transaction hash, the Steem block hash and the previous Steem block hash
The contract version is available via the following variable:
- api.contractVersion: integer that represents the version of the contract (every time a contract is updated, the version is increased by 1 (the first version being 1)
If the contract action has been called by another contract you'll have the following object available in the payload:
- callingContractInfo: object holding information about the contract that called the contract action For example:
actions.sendRewards = async function (payload) {
const { to, quantity, callingContractInfo } = payload;
if (callingContractInfo.name === 'tokens' && callingContractInfo.version === 3) {
await api.transferTokens(to, 'TKN', quantity, 'user');
}
}
- keyword: api.BigNumber
- link to the library's code source: http://mikemcl.github.io/bignumber.js/
- purpose: make the management of numbers easier as javascript has issues with dealing with decimals
- keyword: api.validator
- link to the library's code source: https://github.com/chriso/validator.js
- purpose: allows you to perform validations within a smart contract as the Regular Expression are disabled