无法从 Polkadot 更改存储 API

Cannot change storage from Polkadot API

我已经创建了一个基于 substrate 的项目,并使用 polkadot.js 在 node js 中创建了一个应用程序,但代码不会更改我的 substrate 链中的存储。

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);

以下代码在Pallets/carpooling/lib.rs

bookRide 派遣电话

#[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),
    }

nodejs中的app不改变存储。我使用返回 None 的 RPC 查询了存储。谁能帮我解决这个问题?

更新代码

...

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);
}
    
   
... 

在我看来这里的问题是你没有实际提交 book_ride.

的交易

你写的代码是:

let booked = api.tx.carpooling.bookRide(12, 45);

但这实际上并没有做任何事情。要实际提交外部,您需要 signAndSubmit 交易:

// Sign and send the transaction using our account
const hash = await booked.signAndSend(alice);

有关更多上下文,请查看此处的示例和文档:

https://polkadot.js.org/docs/api/examples/promise/make-transfer