
The service simplifies Quorum blockchain instance lifecycle management and is now available for SCP trial accounts.
Quorum is a code fork of Ethereum by JPMorgan Chase, available as an open source project on GitHub. Unlike Ethereum, Quorum enables private and permissioned blockchains. At a glance, Quorum offers:
SAP Cloud Platform now offers a free-of-charge “dev” service plan for developers to build with Quorum in a playground environment. The plan offers Quorum instances that are shared with other developers.
The playground environment allows developers to get familiar with Soliditysmart contracts and leverage Cloud Foundry to build blockchain applications. To use the “dev” plan, a free account on SAP Cloud Platform is required. To set up a “dev” instance, follow this link to the official documentation.
In the near future, there will be three more service plans in addition to the Dev plan:
Prerequisites:
The following code snippet represents a Solidity smart contract that initializes an integer value. The contract also contains functionality to update the integer value and read the current value.
pragma solidity ^0.5.0;
contract SimpleStorage {
uint public storedData;
constructor (uint initVal) public {
storedData = initVal;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}
This contract can be deployed using any existing Ethereum development tool that can connect to the instance’s RPC API, such as Truffle or Remix (a CORS browser plugin is required for Remix at this point). For learning purposes, the blog post demonstrates how to compile and deploy a smart contract using a few lines of code from scratch, using the web3 and solc NPM packages.
const Web3 = require('web3')
const solc = require('solc')
The following code connects to the Quorum instance’s RPC endpoint. The RPC URL can be obtained from the Quorum dashboard.
const rpc = 'paste RPC url from dashboard here'
const web3 = new Web3(new Web3.providers.HttpProvider(rpc))
Next, the contract needs to be compiled. You could also load the contract from a file and compile more than just one contract. For simplicity, the example puts everything in code:
// Compile smart contract using the solc compiler
const sourceCode = `
pragma solidity ^0.5.0;
contract SimpleStorage {
uint public storedData;
constructor (uint initVal) public {
storedData = initVal;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}
`
const input = {
language: 'Solidity',
sources: { 'simplestorage.sol': { content: sourceCode } },
settings: { outputSelection: { '*': { '*': [ '*' ] } } }
}
const compiled = JSON.parse(solc.compile(JSON.stringify(input)))
if (compiled.errors > 0) {
console.error(compiled.errors)
process.exit(1)
}
const compiledContracts = compiled.contracts['simplestorage.sol']
console.log('successfully compiled contract(s)')
The following code is wrapped in an async function and a try/catch block to make use of ECMAScript’s await keyword. The use of the await keyword makes the following snippets more readable.
;(async () => {
try {
The following code will be here
} catch (err) {
console.error(err)
}
})()
Quorum on SAP Cloud Platform locks all accounts with a password by default. The following code unlocks the account for 10 seconds.
// Select account and unlock the account with your password
const accountAddress = ''
const accountPassword = ''
await web3.eth.personal.unlockAccount(accountAddress, accountPassword, 10)
console.log('account unlocked')
The following code deploys the byte code of the compiled contract and initializes the contract with value “1”. Note that a Gas limit is still required. Unlike Ethereum, Quorum does not subtract any transaction fees from the account. Once the contract deployment transaction is confirmed, the contract address can be used to interact with the contract.
// Deploy contract, initialize with value 1
const contract = await new web3.eth.Contract(compiledContracts.SimpleStorage.abi).deploy({
data: '0x' + compiledContracts.SimpleStorage.evm.bytecode.object,
arguments: [1]
}).send({
from: accountAddress,
gas: 300000
})
const contractAddress = contract.options.address
console.log('contract successfully deployed')
The following code snippet reads the value from the contract that was initialized earlier. At this point, the same “contract” object could be reused. Instead, this example demonstrates how a different Quorum instance can interact with the same contract, using the ABI and the address of the deployed contract.
// Retrieve contract using ABI and address and read from it.
const newContract = new web3.eth.Contract(compiledContracts.SimpleStorage.abi, contractAddress)
let result = await newContract.methods.get().call()
console.log('successfully read from contract:', result)
The final snippet shows how to change the value to a different integer. This will cause another transaction. Similar to the contract deployment, the the specification of the account address is required.
// Change value from 1 to 42
await newContract.methods.set(42).send({ from: accountAddress })
result = await newContract.methods.get().call()
console.log('successfully wrote and read new value to contract:', result)
In another post, I explain how to make use of rapid development using Truffle with Quorum on SAP Cloud Platform: https://blogs.sap.com/2019/01/08/using-truffle-with-quorum-on-sap-cloud-platform
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
7 | |
7 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |