首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从Polkadot API更改存储

无法从Polkadot API更改存储
EN

Stack Overflow用户
提问于 2021-08-19 09:06:38
回答 1查看 258关注 0票数 0

我做了一个基于基板的项目,并使用polkadot.js在节点js中创建了一个应用程序,但是代码不会改变我的基板链中的存储空间。

index.js

代码语言:javascript
复制
// 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调度呼叫

代码语言:javascript
复制
#[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

代码语言:javascript
复制
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中的应用程序不会改变存储空间。我使用一个不返回任何内容的RPC查询存储。有人能帮我吗?

更新代码

代码语言:javascript
复制
...

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-26 01:08:35

在我看来,这里的问题是您没有实际提交执行book_ride的事务。

你写的代码是:

代码语言:javascript
复制
let booked = api.tx.carpooling.bookRide(12, 45);

但这实际上什么也做不了。要实际提交外部事务,您需要对事务进行signAndSubmit

代码语言:javascript
复制
// Sign and send the transaction using our account
const hash = await booked.signAndSend(alice);

有关更多上下文,请在这里查看示例和文档:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68844975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档