Publish Smart Contract
Although, it is not possible to compile Smart Contracts with SignumJS, it is possible to deploy them. Usually, you'll need to deploy Smart Contracts only programmatically, if you need to create many instances of the same contract. This examples shows how to deploy a contract by its reference.
Deploying Smart Contracts by reference is a very efficient way, as no redundant space in the chain is occupied, it is cheaper and more contracts per block can be deployed. This feature is unique to Signum and called Green Contracts.
For this example, we assume a ficticious contract. The crucial part is the use of the referencedTransactionHash - The hash is available inside the transaction of the first deployment of the contracts code, which should be done before using either SmartJ or SmartC. Use the explorer to get the full hash of the deployed contract.
const {generateMasterKeys} = require("@signumjs/crypto");
const {LedgerClientFactory} = require("@signumjs/core");
const {Amount, ChainValue, convertHexStringToDecString, convertHexEndianess, convertStringToHexString} = require("@signumjs/util");
const LedgerHostUrls = {
TestNet: 'http://localhost:6876',
MainNet: 'https://europe.signum.network',
}
/**
* This is an example how to create a new green contract (by referencing existing code).
* It also shows how to initialize the new contract with its own data
*
* Note: this example rather demonstrates how to do it - it's non-working due to non-existent contract
*/
async function publishGreenContract({passphrase, name, decimals, amount}) {
try {
// we create our client depending on the parameter
const ledger = LedgerClientFactory.create({
nodeHost: LedgerHostUrls[ledgerType])
}
const senderKeys = generateMasterKeys(passphrase);
// Let's assume we have a contract with the following variable stack to be initialized
// 0. Token name
// 1. Token decimals
// 2. Quantity to be issued
// see also https://github.com/signum-network/signumjs/issues/26
const tokenName = convertHexStringToDecString(
convertHexEndianess(
convertStringToHexString(name)
))
const tokenDecimals = decimals
const tokenQuantity = ChainValue.create(tokenDecimals).setCompound(amount).getAtomic()
// this reference you get from the previous/first deployment of the contract, which serves as a template
// think of it like classes and objects in Object Oriented Programming
// example how to get the FULL HASH: https://t-chain.signum.network/tx/14079727760708466474
const contractReference = '2AF73BFFCC3D65C3DF8063C42C75764CD1D22025A22911BB3BC7DC761A23374F'
// Now that we have all the params we can publish a new contract instance with our custom data
const {transaction} = await ledger.contract.publishContractByReference({
referencedTransactionHash: contractReference,
feePlanck: Amount.fromSigna(155).getPlanck(), // as the imaginary contract issues a token, we need to send at least 150 SIGNA
data: [
tokenName,
tokenDecimals,
tokenQuantity
],
senderPublicKey: senderKeys.publicKey,
senderPrivateKey: senderKeys.signPrivateKey
})
console.info('Contract deployed successfully - tx id:', transaction)
} catch (e) {
// If the API returns an exception,
// the return error object is of type HttpError
console.error(e);
}
}
(async () => {
await publishGreenContract({
name: 'NEWTOKEN',
decimals: 2,
amount: 1_000_000,
passphrase: 'YourVerySecretPassphraseWhichCannotBeRevealedHere'
});
})();