Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add Org treasury to org profile
Sebastian Martinez committed 4 years ago
commit 497074a0a5dda9539c570911c93648f4da223bf5
parent 1f5193b032d938ac41fd5b92da50402ff4bc70f6
2 files changed +41 -5
modified src/base/orgs/View.svelte
@@ -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>
modified src/utils.ts
@@ -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.