【发布时间】:2021-07-11 01:55:39
【问题描述】:
我有一个ink! 合约,它调用扩展方法fetch_random()。
// runtime/src/lib.rs
pub struct FetchRandomExtension;
impl ChainExtension<Runtime> for FetchRandomExtension {
fn call<E: Ext>(
func_id: u32,
env: Environment<E, InitState>,
) -> Result<RetVal, DispatchError>
where
<E::T as SysConfig>::AccountId:
UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
{
match func_id {
1101 => {
let mut env = env.buf_in_buf_out();
let random_seed = crate::RandomnessCollectiveFlip::random_seed().0;
let random_slice = random_seed.encode();
env.write(&random_slice, false, None).map_err(|_| {
DispatchError::Other("ChainExtension failed to call random")
})?;
}
_ => {
return Err(DispatchError::Other("Unimplemented func_id"))
}
}
Ok(RetVal::Converging(0))
}
fn enabled() -> bool {
true
}
}
// contract/lib.rs
let new_random = self.env().extension().fetch_random()?;
如何编写扩展处理程序来接收let new_random = self.env().extension().fetch_random(1, "hello", true)?; 等参数?
【问题讨论】:
-
我现在只是在解决这个问题,但我想我在某处读到这些函数可以占用字节,所以可能使用比例编码然后序列化
标签: substrate polkadot rust-ink