Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could I send a sudo call? #84

Closed
Dengjianping opened this issue Apr 8, 2020 · 3 comments
Closed

How could I send a sudo call? #84

Dengjianping opened this issue Apr 8, 2020 · 3 comments

Comments

@Dengjianping
Copy link

Hi there,

I just used this crate create a general call for my own module, and it worked as expected.
But while I tried to send a sudo call, it failed, the error message likes

DEBUG runtime  DispatchError
DEBUG runtime  Bad origin

And I checked examples and test cases, it looks there's no sample codes in it.
So my problem is how can I compose a sudo call and send it to a running node.

@ascjones
Copy link
Contributor

ascjones commented Apr 9, 2020

Can you provide the code you used to construct the Sudo call so I can reproduce it?

@Dengjianping
Copy link
Author

I figured that out.

But I made some change on this crate, just a little.

This is my own sudo function, it looks like this

pub fn issue(
	origin, // need sudo right
        #[compact] id: T::AssetId, // u32
        token_type: TokenType, // enum
        target: <T::Lookup as StaticLookup>::Source,
        #[compact] amount: T::Balance, // u128
)

I created a struct for these parameters.

pub trait AssetBalances: System {
	type Balance: Member + Parameter + Default + AtLeast32Bit + Copy + Zero;
	type AssetId: Member + Parameter + Default + AtLeast32Bit + Copy;
}

impl AssetBalances for DefaultNodeRuntime {
	type Balance = u128;
	type AssetId = u32;
}

#[derive(codec::Encode)]
pub struct IssueArgs<T: AssetBalances> {
	#[codec(compact)]
	id: <T as AssetBalances>::AssetId,
	token_type: TokenType,
	target: <T as System>::Address,
	#[codec(compact)]
	amount: <T as AssetBalances>::Balance,
}

pub fn issue<T: AssetBalances>(
	id: <T as AssetBalances>::AssetId,
	token_type: TokenType,
	target: <T as System>::Address,
	amount: <T as AssetBalances>::Balance
) -> Call<IssueArgs<T>> {
	Call::new(
		MODULE,
		ISSUE,
		IssueArgs {
			id, token_type, target, amount
		}
	)
}

This is the key point. I modified this line, replaced it with the following code.

let proposal = self.client
                .metadata()
                .module_with_calls(&call.module)
                .and_then(|module| module.call(&call.function, call.args))?;
let call = self.client
                .metadata()
                .module_with_calls("Sudo")
                .and_then(|module| module.call("sudo", proposal))?;

Now, it worked. If I don't modify this line of code, it will get error like Bad origin.

It looks not that easy to compose a sudo call, maybe I use it the wrong way.

@Dengjianping
Copy link
Author

This issue has been solved, this is the solution of snippet.

let proposal = client.metadata().module_with_calls("MyModule")
		.and_then(|module| module.call("my_call", args))
		.map_err(|_| crate::Error::SubxtError("failed to compose a sudo call"))?;
let call = Call::new("Sudo", "sudo", proposal);

So Im' going to close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants