Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enriched wallet info endpoint #496

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,65 @@ Returns a list of available wallets

---

### `GET /api/enriched/wallet`

Returns a list of available wallets with enriched information. It executes on-chain requests to populate the list of owners of each safe, and provides the attributes `all_safe_backup_owners_match` and `single_safe_backup_owner_per_chain`.

<details>
<summary>Response</summary>

```json
[
{
"address":"0xFafd5cb31a611C5e5aa65ea8c6226EB4328175E7",
"all_safe_backup_owners_match":false,
"ledger_type":"ethereum",
"safe_chains":[
"gnosis",
"ethereum",
"base",
"optimistic"
],
"safe_nonce":110558881674480320952254000342160989674913430251257716940579305238321962891821,
"safes":{
"base":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"owners":[
// No owners for this safe
]
}
},
"ethereum":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"owners":[
"0x46eC2E77Fe3E367252f1A8a77470CE8eEd2A985b"
]
}
},
"gnosis":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"owners":[
"0x46eC2E77Fe3E367252f1A8a77470CE8eEd2A985b"
]
}
},
"optimistic":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"owners":[
"0x46eC2E77Fe3E367252f1A8a77470CE8eEd2A985b"
]
}
}
},
"single_safe_backup_owner_per_chain":false
}
]
```

</details>

---

### `POST /api/wallet`

Creates a master wallet for given chain type. If a wallet already exists for a given chain type, it returns the already existing wallet without creating an additional one.
Expand Down
9 changes: 9 additions & 0 deletions operate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,15 @@ async def _create_wallet(request: Request) -> t.List[t.Dict]:
wallet, mnemonic = manager.create(ledger_type=ledger_type)
return JSONResponse(content={"wallet": wallet.json, "mnemonic": mnemonic})

@app.get("/api/enriched/wallet")
@with_retries
async def _get_wallet_safe(request: Request) -> t.List[t.Dict]:
"""Get wallets."""
wallets = []
for wallet in operate.wallet_manager:
wallets.append(wallet.enriched_json)
return JSONResponse(content=wallets)

@app.get("/api/wallet/safe")
@with_retries
async def _get_safes(request: Request) -> t.List[t.Dict]:
Expand Down
31 changes: 31 additions & 0 deletions operate/wallet/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def update_backup_owner(
"""Update backup owner."""
raise NotImplementedError()

# TODO move to resource.py ?
jmoreira-valory marked this conversation as resolved.
Show resolved Hide resolved
@property
def enriched_json(self) -> t.Dict:
"""Get JSON representation with extended information (e.g., safe owners)."""
raise NotImplementedError

@classmethod
def migrate_format(cls, path: Path) -> bool:
"""Migrate the JSON file format if needed."""
Expand Down Expand Up @@ -393,6 +399,31 @@ def update_backup_owner(

return False

@property
def enriched_json(self) -> t.Dict:
"""Get JSON representation with extended information (e.g., safe owners)."""
rpc = None
wallet_json = self.json

if not self.safes:
return wallet_json

owner_sets = set()
for chain, safe in self.safes.items():
ledger_api = self.ledger_api(chain=chain, rpc=rpc)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove rpc? Can't be passed to a @property.

owners = get_owners(ledger_api=ledger_api, safe=safe)
owners.remove(self.address)
wallet_json["safes"][chain.value] = {
wallet_json["safes"][chain.value]: {"owners": owners}
}
owner_sets.add(frozenset(owners))

wallet_json["all_safe_backup_owners_match"] = len(owner_sets) == 1
wallet_json["single_safe_backup_owner_per_chain"] = all(
len(owner) == 1 for owner in owner_sets
)
return wallet_json

@classmethod
def load(cls, path: Path) -> "EthereumMasterWallet":
"""Load master wallet."""
Expand Down
Loading