When business events in Alibaba Cloud Simple Message Queue (MNS) need an immutable audit trail, you can use Function Compute to bridge MNS and your blockchain automatically. When a message arrives in an MNS topic, a Function Compute trigger invokes a function that calls your smart contract's REST API and writes the message to the blockchain channel.
How it works
The data pipeline runs in the following sequence:
A message is published to an MNS topic.
The MNS trigger fires the Function Compute function.
The function calls the Blockchain as a Service REST API.
The smart contract writes the message to the blockchain channel.
Prerequisites
Before you begin, ensure that you have:
The cloud service integration module installed. For details, see Install the cloud service integration module.
The cloud service integration sample code downloaded from GitHub.
The Function Compute CLI tool funcraft installed. For details, see funcraft introduction and installation method.
Push MNS messages to blockchain
Step 1: Configure the function
Go to the
mns2blockchaindirectory in the sample code.Open
index.jsand update the following configuration items based on the inline comments:Configuration item Description REST API service address The REST API endpoint of your Blockchain as a Service instance Refresh token The refresh token for authenticating with the REST API Channel name The blockchain channel to write messages to Smart contract name The name of the smart contract to invoke In
index.js, update the smart contract method name and parameters to match your service scenario, using the inline comments as a guide.Open
template.ymland update the Function Compute service name and function name. The defaults are:Service name:
octopusFunction name:
MNS2BlockChain
For all available configuration options, see the funcraft documentation.
Step 2: Deploy the function
In the mns2blockchain directory, run the following command to deploy the function to Function Compute:
fun deployStep 3: Create an MNS topic
In Alibaba Cloud Simple Message Queue (formerly MNS), create a topic. For setup instructions, see Simple Message Queue (formerly MNS) quick start.
Step 4: Configure the MNS trigger
Log on to the Function Compute console.
Navigate to the function deployed in Step 2.
Add an MNS trigger for the function, and set Event Format to JSON.

Set Event Format to JSON. Without this setting, the function cannot parse the message payload correctly.
Step 5: Verify the integration
In the Alibaba Cloud Simple Message Queue (formerly MNS) console, send a test message to the MNS topic. The trigger invokes the function, which calls the smart contract and writes the message to the blockchain channel.