Reputation: 153
I have made a substrate based project and created an app in node js using polkadot.js but the code does not change the storage in my substrate chain.
index.js
// Import
const express = require('express');
const { ApiPromise, WsProvider } = require('@polkadot/api');
var crypto = require('crypto');
const app = express();
app.get('/index', (req, res) =>{
async function digestMessage(message) {
try{
const hash = await crypto.createHash('sha256',message).digest('hex');
return hash;
}
catch(error){
console.log(error);
}
}
async function main(){
// Construct
const wsProvider = new WsProvider('ws://127.0.0.1:9944');
const api = await ApiPromise.create({ provider: wsProvider,
rpc: {
carpooling: {
getDriver: {
description: 'Just a test method',
params: [],
type: "u32",
}}},
types: {
DriverOf: {
id: 'u32',
car_no: 'Hash',
location: ('u32', 'u32'),
price: 'u32',
},
CustomerOf: {
id: 'u32',
name: 'Hash',
location: ('u32', 'u32'),
},
}
});
try{
const cabNo = 'UP76 E 8550';
const output = digestMessage(cabNo);
output.then((hash)=>{
api.tx.carpooling.addNewCab(12,{id:12, car_no: hash,location: (10,20), price: 50});
})
let booked = api.tx.carpooling.bookRide(12, 45);
console.log(`The output from bookRide is ${booked}`);
let directSum = await api.rpc.carpooling.getDriver();
console.log(`The customerID from the RPC is ${directSum}`);
}
catch(error){
console.log(error);
}
}
main().then(() => console.log('completed'));
res.send("Done");
});
app.listen(6069);
The following code is in Pallets/carpooling/lib.rs
bookRide dispatch call
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
pub fn book_ride(origin: OriginFor<T>, driver_id: u32, customer_id: u32) -> DispatchResult {
// Check that the extrinsic was signed and get the signer.
// This function will return an error if the extrinsic is not signed.
// https://substrate.dev/docs/en/knowledgebase/runtime/origin
let who = ensure_signed(origin)?;
ensure!(
<Driver<T>>::contains_key(&driver_id),
Error::<T>::DriverDoesNotExist
);
ensure!(
!(<Booking<T>>::contains_key(&driver_id)),
Error::<T>::CabIsAlreadyBooked
);
<Booking<T>>::insert(&driver_id, &customer_id);
Self::deposit_event(Event::CabBooked(who, driver_id));
Ok(().into())
}
DriverOf struct
type DriverOf<T> = SDriver<<T as frame_system::Config>::Hash>;
#[derive(Encode, Decode, Copy, Clone, Default, PartialEq, RuntimeDebug)]
pub struct SDriver<Hash> {
pub id: u32,
pub car_no: Hash,
pub location: (u32, u32),
pub price: u32,
pub destination: (u32, u32),
}
The app in nodejs does not change the storage. I queried the storage using an RPC which returned None. Can anyone help me in this?
Updated Code
...
try{
const cabNo = 'UP76 E 8550';
const output = digestMessage(cabNo);
output.then(async (hash)=>{
const addDriver = api.tx.carpooling.addNewCab(12,{id:12, car_no: hash,location: (10,20), price: 50,destination: (30,40)});
const out = await addDriver.signAndSend(alice);
}).catch((e)=>{
console.log(e);
})
let booked = api.tx.carpooling.bookRide(12, 99);
const hash = await booked.signAndSend(alice);
let bookedCust = await api.rpc.carpooling.getDriver();
console.log(`The customerID from the RPC is ${bookedCust}`);
}
catch(error){
console.log(error);
}
...
Upvotes: 0
Views: 351
Reputation: 12434
It seems to me the problem here is that you have not actually submitted the transaction which does book_ride
.
The code you wrote is:
let booked = api.tx.carpooling.bookRide(12, 45);
But this doesn't actually do anything. To actually submit the extrinsic, you need to signAndSubmit
the transaction:
// Sign and send the transaction using our account
const hash = await booked.signAndSend(alice);
For more context, take a look at the example and documentation here:
https://polkadot.js.org/docs/api/examples/promise/make-transfer
Upvotes: 1