How to discover and use existing NEAR Contracts
near-contract-parser
If you are using a module bundler like:
You will be able to download it from npm near-contract-parser
Alternatively available via CDN
In order to parse the contract,
you will need to first download it from the blockchain.
The response from NEAR will look like this. The Contract code is encode in result.code_base64
{
"jsonrpc": "2.0",
"result": {
"code_base64": "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
"hash": "7KoFshMQkdyo5iTx8P2LbLu9jQpxRn24d27FrKShNVXs",
"block_height": 17814234,
"block_hash": "GT1D8nweVQU1zyCUv399x8vDv2ogVq71w17MyR66hXBB"
},
"id": "dontcare"
}
After you parse it with near-contract-parser, you will get a list of methods and/or probable interfaces of know contract types.
{
byMethod: {sayHi: [], say_hi: [], whoSaidHi: [], who_said_hi: []},
methodNames: ['sayHi', 'say_hi', 'whoSaidHi', 'who_said_hi'],
probableInterfaces: []
}
If you want to provide further hints into how to use the methods,
for ex. method parameters, typical deposit and results,
you can have a look at my project, which uses Firebase and Elastic
to parse all NEAR blocks and collect display this data
Best practice is to name Contract Methods in snake_case. As you can see the methods which are in CamelCase get duplicated in snake_case. I am using this function to clean up the results before showing them to users:
function removeDuplicatedMethods(methods) {
let filtered_methods = []
for (let method of methods) {
let snake_case_method = method.replace(/[A-Z]/g, (letter, index) => { return index === 0 ? letter.toLowerCase() : '_'+ letter.toLowerCase();});
let camel_case_method = method.toLowerCase().replace(/[-_][a-z]/g, (group) => group.slice(-1).toUpperCase());
if (!filtered_methods.includes(method) && !filtered_methods.includes(snake_case_method) && !filtered_methods.includes(camel_case_method))
filtered_methods.push(method)
}
return filtered_methods
}
Any improvements are welcome 😃