Comments (11)
You are never supposed to work with the type names directly.
So you should never do -
const something = api.createType('SomeType', { foo: 'bar' });
...
await api.tx.somewhere.something(something);
...
In all cases you should just let the API create the types, aka -
await api.tx.somewhere.something({ foo: 'bar' });
So if you don't use API do do the internal conversion and instead use a name-based createType
yourself, you will be into a world of hurt. Never do that. Let the API create since it always knows what is exposed.
So we can go round and round, it stays a wontfix.
from api.
Just to add: I do think there is merit behind using api.createType
. In a static environment for testing it can be incredibly useful. Especially when you want to mock storage queries from the chain for testing purposes.
Here is an example below:
// This was taken directly from polkadot-js
export function createApiWithAugmentations(metadataHex: `0x${string}`): ApiPromise {
const registry = new TypeRegistry();
const metadata = new Metadata(registry, metadataHex);
registry.setMetadata(metadata);
const api = new ApiPromise({
provider: new WsProvider('ws://', false),
registry,
});
api.injectMetadata(metadata, true, registry);
return api;
}
const mockApi = createApiWithAugmentations(<some_metadata>);
const getSafeXcmVersion = () =>
Promise.resolve().then(() => {
return mockApi.registry.createType('Option<u32>', 2);
});
const adjustedMockApi = {
registry: mockApi.registry
query: {
polkadotXcm: {
safeXcmVersion: getSafeXcmVersion
}
}
}
I also think it can be really useful when constructing the Call
type or ExtrinsicPayload
types for decoding. But generally speaking I do agree that in the case of building a tx.something
it makes sense to just pass the params as is.
Edit: fix typo.
from api.
@jacogr is that articulated somewhere? Maybe it should be mentioned here, at least? https://polkadot.js.org/docs/api/start/types.create
Until the issue happened, we had no idea that this is a bad practice, and I can easily find tons of cases where people do it
@TarikGul just created a PR for said doc page: polkadot-js/docs#455
from api.
@jacogr is that articulated somewhere? Maybe it should be mentioned here, at least?
https://polkadot.js.org/docs/api/start/types.create
Until the issue happened, we had no idea that this is a bad practice, and I can easily find tons of cases where people do it
from api.
@jacogr appending the paths
in front of the types is really not a great idea. I think you probably tried to make them unique in this way. However, this is no guarantee that there doesn't exist a different type with the same name + prefix.
from api.
I think users probably only require the type when they want to instantiate a Call
. Instead of them trying to instantiate the type
on their own, polkadot-js
should give them the type by calling something like type_for("my_call", index)
. Then they could for example get the correct type for xcm
to instantiate it.
from api.
There are no clashes - when a clash happens, it exposes the type by the Lookup id only.
(These metadata type ids change with each and every runtime upgrade, so you end up in a much worse position using the ids plus some prefix. The type ids/indexes are far from stable, it is the things that changes the most each and every upgrade.)
Types are generated for each metadata, so when (like in this case), there is a rename, the types will be different. So the breakage here is certainly expected, the type names are generated based on what is in the metadata.
TL;DR The code behaves as expected. Nothing to do here, the names changed, the exposed types changed. These are generated automatically.
from api.
I mean I get that the code works as expected. However, I think that people should not write their code in this way. They should not try to search for a type by name and instead get the type they are interested in by index from a call. This should be much more stable. I mean I'm also open to other suggestions. However for decoding we shouldn't have this problem as polkadot-js knows the types and can decode it properly. The only problem should be around encoding for transactions that we have seen now.
from api.
Ahh okay thank you!
from api.
Thank you @jacogr and @bkchr . Will close this here
from api.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.
from api.
Related Issues (20)
- How to use the metadata type in a custom type?
- Block with more than u16 events fails to be decoded
- About the callback issue encountered by nodejs when using @ polkadot/API
- api.tx.staking.bond takes in 3 params but it should be 2 HOT 1
- Call hash differs in UI vs created with API HOT 2
- Connecting to Polkadot/Api HOT 3
- Support new `ink!` contract events HOT 8
- `Unable to decode storage society.candidates` when there's a vouch in the candidates list HOT 4
- Memory Leak in `query.entriesPaged`
- [Parachain] Trying to add an already reserved node 12D3KooWJb4BDK6gRLH27mYnd22igZQi6W3xraAJsvSanaccPNVx as reserved on SetId(0). HOT 6
- `api-contracts`: handle foreign events gracefully HOT 7
- Rework documentation on types-support, and typegen HOT 3
- Arithmetic operations on balance.free throwing an error HOT 3
- `api.derive.accounts.identity` called with SuperIdentity doesn't return SubIdentities HOT 1
- Unable to update polkadot and kusama via `yarn build:interfaces` for v1002000 HOT 1
- `ApiPromise.create` doesn't resolve with the `ScProvier` HOT 3
- update issue templates
- [Tracking upstream] Remove `erasStakersClipped` from `api.derive.staking._erasExposure` when applicable HOT 3
- [Tracking upstream] Addition of TransactionExtension
- Support `minimumPeriod` of `0` HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from api.