Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/erdjs-v12' into guardedTran…
Browse files Browse the repository at this point in the history
…saction
  • Loading branch information
schimih committed Sep 12, 2022
2 parents 750fa41 + 336f032 commit 7d0ec7b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## 12.0.0
- [Breaking change: Implementation of topological sort to sort custom types by their type dependencies (change attribute visibility)](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/224)

## 11.0.0
- [Breaking change: Sender is now mandatory when constructing a transaction](https://github.com/ElrondNetwork/elrond-sdk-erdjs/pull/TBD)
- Switched to MIT license
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@elrondnetwork/erdjs",
"version": "11.0.0",
"version": "12.0.0",
"description": "Smart Contracts interaction framework",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
44 changes: 32 additions & 12 deletions src/smartcontracts/typesystem/abiRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { EndpointDefinition, EndpointParameterDefinition } from "./endpoint";

export class AbiRegistry {
readonly interfaces: ContractInterface[] = [];
readonly customTypes: CustomType[] = [];
private customTypes: CustomType[] = [];

static create(json: { name: string; endpoints: any[]; types: any[] }): AbiRegistry {
static create(json: { name: string; endpoints: any[]; types: Record<string, any> }): AbiRegistry {
let registry = new AbiRegistry().extend(json);
let remappedRegistry = registry.remapToKnownTypes();
return remappedRegistry;
}

private extend(json: { name: string; endpoints: any[]; types: any[] }): AbiRegistry {
private extend(json: { name: string; endpoints: any[]; types: Record<string, any> }): AbiRegistry {
json.types = json.types || {};

// The "endpoints" collection is interpreted by "ContractInterface".
Expand Down Expand Up @@ -49,17 +49,37 @@ export class AbiRegistry {
}

private sortCustomTypesByDependencies() {
// TODO: Improve consistency of the sorting function (and make sure the sorting is stable): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
this.customTypes.sort((a: CustomType, b: CustomType) => {
const bDependsOnA = b.getNamesOfDependencies().indexOf(a.getName()) > -1;
if (bDependsOnA) {
// Sort "a" before "b".
return -1;
// Use of topological sort algorithm to sort custom types by dependencies.
let dependencies: { [key: string]: string[] } = {};
let visited: { [key: string]: boolean } = {};
this.customTypes.forEach((type: CustomType) => {
dependencies[type.getName()] = type.getNamesOfDependencies();
visited[type.getName()] = false;
});
let sortedArray = new Array<CustomType>();

const topologicalSortUtil = (name: string, visited: { [key: string]: boolean }, sortedArray: CustomType[]) => {
visited[name] = true;

for (const dependency of dependencies[name]) {
if (!this.customTypes.find((e) => e.getName() == dependency)) continue;
if (!visited[dependency]) {
topologicalSortUtil(dependency, visited, sortedArray);
}
}
const type = this.customTypes.find((e) => e.getName() == name);
if (type) {
sortedArray.push(type);
}
};

// Sort "b" before "a".
return 1;
});
for (const type of this.customTypes) {
if (!visited[type.getName()]) {
topologicalSortUtil(type.getName(), visited, sortedArray);
}
}

this.customTypes = sortedArray;
}

getInterface(name: string): ContractInterface {
Expand Down
24 changes: 12 additions & 12 deletions src/testdata/custom-types-out-of-order.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
}
]
},
"TypeC": {
"type": "struct",
"fields": [
{
"name": "foobar",
"type": "u64"
}
]
},
"EsdtTokenType": {
"type": "enum",
"variants": [
Expand All @@ -35,15 +44,6 @@
}
]
},
"TypeA": {
"type": "struct",
"fields": [
{
"name": "b",
"type": "TypeB"
}
]
},
"TypeB": {
"type": "struct",
"fields": [
Expand All @@ -53,12 +53,12 @@
}
]
},
"TypeC": {
"TypeA": {
"type": "struct",
"fields": [
{
"name": "foobar",
"type": "u64"
"name": "b",
"type": "TypeB"
}
]
}
Expand Down

0 comments on commit 7d0ec7b

Please sign in to comment.