Add Org treasury to org profile

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
This commit is contained in:
Sebastian Martinez
2021-11-02 10:25:15 +01:00
parent 1f5193b032
commit 497074a0a5
2 changed files with 41 additions and 5 deletions
+26
View File
@@ -1,6 +1,7 @@
<script lang="ts">
import type { SvelteComponent } from 'svelte';
import type { Config } from '@app/config';
import type { BigNumber } from "ethers";
import { formatName, explorerLink } from '@app/utils';
import { session } from '@app/session';
import Loading from '@app/Loading.svelte';
@@ -44,6 +45,20 @@
const transferOwnership = () => {
transferOwnerForm = TransferOwnership;
};
$: getOrgTreasury = async (org: Org): Promise<Array<{ name: string; symbol: string; logo: string; decimals: number; balance: BigNumber }>| undefined> => {
const addressType = await utils.identifyAddress(org.owner, config);
// We query the org treasury only for Gnosis Safes, to maintain some privacy for EOA org owners.
if (addressType === utils.AddressType.Safe) {
try {
const tokens = await utils.getTokens(org.owner, config);
const balance = await config.provider.getBalance(org.owner);
// To maintain the format we hardcode the ETH specs.
return [{ balance, decimals: 18, logo: "", name: "Ethereum", symbol: "ETH" }, ...tokens];
} catch (e) {
console.error(e);
}
}
};
$: isOwner = (org: Org): boolean => $session
? utils.isAddressEqual(org.owner, $session.address)
: false;
@@ -207,6 +222,17 @@
</button>
{/if}
</div>
{#await getOrgTreasury(org) then tokens}
{#if tokens}
<div class="label">Treasury</div>
<div>
{#each tokens as token}
{` ${utils.formatBalance(token.balance)} ${token.symbol} `}
{/each}
</div>
<div></div>
{/if}
{/await}
<!-- Seed Address -->
{#if profile.seedId && profile.seedHost}
<div class="label">Seed</div>
+15 -5
View File
@@ -1,5 +1,5 @@
import { ethers } from "ethers";
import type { BigNumber } from "ethers";
import { BigNumber } from "ethers";
import multibase from 'multibase';
import multihashes from 'multihashes';
import EthersSafe, { EthersAdapter } from "@gnosis.pm/safe-core-sdk";
@@ -380,12 +380,22 @@ export async function getOwnerSafes(owner: string, config: Config): Promise<stri
// Get token balances for an address.
export async function getTokens(address: string, config: Config):
Promise<Array<{ tokenName: string; tokenLogo: string }>>
Promise<Array<{ name: string; symbol: string; decimals: number; logo: string; balance: BigNumber }>>
{
await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]);
const userBalances = await config.provider.send("alchemy_getTokenBalances", [address, "DEFAULT_TOKENS"]);
const balances = userBalances.tokenBalances.filter((token: any) => {
// alchemy_getTokenBalances sometimes returns 0x and this does not work well with ethers.BigNumber
if (token.tokenBalance !== "0x") {
if (!BigNumber.from(token.tokenBalance).isZero()) {
return token;
}
}
}).map(async (token: any) => {
const tokenMetaData = await config.provider.send("alchemy_getTokenMetadata", [token.contractAddress]);
return { ...tokenMetaData, balance: BigNumber.from(token.tokenBalance) };
});
// TODO
return [];
return Promise.all(balances);
}
// Check whether the given path has a markdown file extension.