Skip to content

Commit

Permalink
Merge pull request #15 from AstralProtocol/Subgraph-SpatialAssetsv02
Browse files Browse the repository at this point in the history
Subgraph spatial assetsv02
  • Loading branch information
0xTimepunk committed Feb 17, 2021
2 parents 1972386 + 52479b2 commit 9892e80
Show file tree
Hide file tree
Showing 14 changed files with 5,219 additions and 12,111 deletions.
52 changes: 6 additions & 46 deletions packages/contracts/README.md
Expand Up @@ -2,6 +2,12 @@

[![Build Status](https://www.travis-ci.com/AstralProtocol/astralprotocol.svg?branch=master)](https://www.travis-ci.com/AstralProtocol/astralprotocol) [![Coverage Status](https://coveralls.io/repos/github/AstralProtocol/astralprotocol/badge.svg?branch=master)](https://coveralls.io/github/AstralProtocol/astralprotocol?branch=master)

## Description

These contracts serve as the Registry for the Astral Protocol GeoDIDs. It allows binding of a GeoDID to an ethereum address and CID name resolving.

By registering a spatial asset Smart Contract events are triggered, which are picked up by the subgraph indexer to build the tree of relationships for easy querying.

## Usage

- Run ganache `yarn ganache`
Expand All @@ -10,49 +16,3 @@
- You can deploy an instance by running `yarn new-instance`. It builds a tree with some GeoDIDs with fake ids and cids.
- You can test the removal of some links by running `yarn remove-links`.
- Watch the changes in a locally deployed subgraph.

## Description

These contracts serve as the Registry for the Astral Protocol GeoDIDs. It allows binding of a GeoDID to an ethereum address and CID name resolving.

By registering a spatial asset Smart Contract events are triggered, which are picked up by the subgraph indexer to build the tree of relationships for easy querying.

The most important function is:

`function registerSpatialAsset(address owner, uint256 geoDIDId, uint256 parentGeoDIDId , uint256[] memory childrenGeoDIDIds, uint256 cid, bytes32 offChainStorage, uint256 geoDIDtype) public`

---

### Required parameters:

- Owner:

Owner of the geodid. Currently must be msg.sender.

- geoDIDId:

Id of the geodid generated after its creation

- cid:

CID of the GeoDID Document

- offChainStorage:

`Bytes32` encoded string of an approved offchain storage. Default `'FILECOIN'`, should be encoded with `web3.utils.asciiToHex()`.

- geoDIDtype:

Must be set to 0 for `'Collection'` type GeoDIDs, or 1 for `'Item'`type GeoDIDs.

---

Optional parameters:

- parentGeoDIDId:

Id of the GeoDID to be added as parent of `geoDIDId`. Must exist already. If no parent is to be added, `parentGeoDIDId`should be set to 0.

- childrenGeoDIDIds:

Ids of the children GeoDIDs to be added. Must exist already. If no children is to be added, `childrenGeoDIDIds`should be set to [].
34 changes: 17 additions & 17 deletions packages/contracts/contracts/SpatialAssets.sol
Expand Up @@ -16,7 +16,7 @@ contract SpatialAssets is Context, AccessControl {
/**
* @dev Emitted when Spatial Assets of id `id` are transferred to `to``.
*/
event SpatialAssetRegistered(address indexed to, uint256 indexed geoDIDId, uint256 indexed cid, bytes32 offChainStorage, bool root, bool canBeParent);
event SpatialAssetRegistered(address indexed to, uint256 indexed geoDIDId, uint256 indexed cid, bytes32 offChainStorage, uint256 root, bool canBeParent);

/**
* @dev Emitted when Spatial Assets of id `id` are deactivated.
Expand Down Expand Up @@ -58,17 +58,10 @@ contract SpatialAssets is Context, AccessControl {

// Mapping from GeodidID to parenthood type
mapping (uint256 => bool) private _canBeParent;
/*
// Mapping from GeiDID id to Cids (for versioning - implement methods to create/update and fetch?)
mapping (uint256 => uint256[]) private _cids;
function updateCids (uint256[] geoDidIdsToChange, uint256[] _cidsToChange) {
for (...){

// Mapping from GeodidID to root GeoDID
mapping (uint256 => uint256) private _root;

}
}
*/
// Mapping from id to spatial asset external storage
mapping (uint256 => bytes32) private _externalStorage;

Expand Down Expand Up @@ -96,7 +89,7 @@ contract SpatialAssets is Context, AccessControl {
}

/**
* @dev Registers a new user with the ability to register a spatial asset.
* @dev Registers a new storage that can accept GeoDID document creation
*/
function enableStorage(bytes32 offChainStorage) public {
require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "SpatialAssets: must have admin role to edit allowed offchain storages");
Expand All @@ -105,7 +98,7 @@ contract SpatialAssets is Context, AccessControl {
}

/**
* @dev Registers a new user with the ability to register a spatial asset.
* @dev Disables an existing storage
*/
function disableStorage(bytes32 offChainStorage) public {
require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "SpatialAssets: must have admin role to edit allowed offchain storages");
Expand Down Expand Up @@ -136,10 +129,11 @@ contract SpatialAssets is Context, AccessControl {

if (parentGeoDIDId == 0) {
_hasParent[geoDIDId] = false;
emit SpatialAssetRegistered(owner, geoDIDId, cid, offChainStorage, true, _canBeParent[geoDIDId]);
_root[geoDIDId] = geoDIDId;
emit SpatialAssetRegistered(owner, geoDIDId, cid, offChainStorage, geoDIDId, _canBeParent[geoDIDId]);
} else {
_hasParent[geoDIDId] = true;
emit SpatialAssetRegistered(owner, geoDIDId, cid, offChainStorage, false, _canBeParent[geoDIDId]);
emit SpatialAssetRegistered(owner, geoDIDId, cid, offChainStorage, _root[parentGeoDIDId], _canBeParent[geoDIDId]);
emit ParentAdded(geoDIDId, parentGeoDIDId);
}

Expand Down Expand Up @@ -170,6 +164,7 @@ contract SpatialAssets is Context, AccessControl {
uint256 childrenGeoDID = childrenGeoDIDIds[j];
if (_owners[childrenGeoDID] != address(0) && !_hasParent[childrenGeoDID]) {
_hasParent[childrenGeoDID] = true;
_root[childrenGeoDID] = _root[geoDIDId];
emit ChildrenAdded(geoDIDId, childrenGeoDID);
}
}
Expand All @@ -189,6 +184,7 @@ contract SpatialAssets is Context, AccessControl {
require(_canBeParent[parentGeoDIDId], "SpatialAssets: parentGeoDIDId must be able to be parent (a Collection)");

_hasParent[geoDIDId] = true;
_root[geoDIDId] = _root[parentGeoDIDId];
emit ParentAdded(geoDIDId, parentGeoDIDId);
}

Expand All @@ -209,6 +205,7 @@ contract SpatialAssets is Context, AccessControl {
uint256 childrenGeoDID = childrenGeoDIDIds[j];
if (_owners[childrenGeoDID] != address(0) && _hasParent[childrenGeoDID]) {
_hasParent[childrenGeoDID] = false;
_root[childrenGeoDID] = childrenGeoDID;
emit ChildrenRemoved(geoDIDId, childrenGeoDID);
}
}
Expand All @@ -228,7 +225,8 @@ contract SpatialAssets is Context, AccessControl {
require(_hasParent[geoDIDId], "SpatialAssets: GeoDID does not have a parent to remove");

_hasParent[geoDIDId] = false;

_root[geoDIDId] = geoDIDId;

emit ParentRemoved(geoDIDId, parentGeoDIDId);
}

Expand All @@ -243,13 +241,15 @@ contract SpatialAssets is Context, AccessControl {
_externalStorage[geoDIDId] = "";
_cids[geoDIDId] = 0;
_hasParent[geoDIDId] = false;

_root[geoDIDId] = 0;

uint256 childrensLen = childrenToRemove.length;

if (childrensLen > 0){
for(uint256 j=0; j < childrensLen; j++) {
uint256 childrenGeoDID = childrenToRemove[j];
if (_owners[childrenGeoDID] != address(0) && _hasParent[childrenGeoDID]) {
_root[childrenGeoDID] = childrenGeoDID;
_hasParent[childrenGeoDID] = false;
}
}
Expand Down

0 comments on commit 9892e80

Please sign in to comment.