-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: first step - refactoring accounts and tokens
- Loading branch information
1 parent
bfcf1c7
commit 2fbd8b1
Showing
7 changed files
with
143 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Account, | ||
AccountType, | ||
GenDisposableSignerResponse, | ||
} from "@namada/types"; | ||
import { NamadaKeychain } from "hooks/useNamadaKeychain"; | ||
|
||
export const getDefaultTransparentAccount = async (): Promise< | ||
Account | undefined | ||
> => { | ||
const namada = await new NamadaKeychain().get(); | ||
return await namada?.defaultAccount(); | ||
}; | ||
|
||
export const getDisposableKeypair = async (): Promise< | ||
GenDisposableSignerResponse | never | ||
> => { | ||
const namada = await new NamadaKeychain().get(); | ||
const response = await namada?.genDisposableKeypair(); | ||
if (!response) { | ||
throw new Error("Failed to generate disposable signer"); | ||
} | ||
return response; | ||
}; | ||
|
||
// TODO: ?? should it be approved accounts here? | ||
export const getKeychainAccounts = async (): Promise<readonly Account[]> => { | ||
const namada = await new NamadaKeychain().get(); | ||
const result = await namada?.accounts(); | ||
return result || []; | ||
}; | ||
|
||
export const findAccountByAlias = ( | ||
accounts: Account[], | ||
alias: string | undefined | ||
): Account | undefined => accounts.find((a) => a.alias === alias); | ||
|
||
export const filterShieldedAccounts = ( | ||
accounts: readonly Account[] | ||
): Account[] => accounts.filter((a) => a.type === AccountType.ShieldedKeys); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Balance, IbcToken, NativeToken } from "@namada/indexer-client"; | ||
import BigNumber from "bignumber.js"; | ||
import { DenomTrace } from "cosmjs-types/ibc/applications/transfer/v1/transfer"; | ||
import { Address, TokenBalance } from "types"; | ||
import { isNamadaAsset, namadaAsset, toDisplayAmount } from "utils"; | ||
|
||
export const getNamTokenAmount = ( | ||
balances: Balance[], | ||
namTokenAddress: Address | ||
): BigNumber => { | ||
const balance = balances | ||
.filter(({ tokenAddress: ta }) => ta === namTokenAddress) | ||
.map(({ tokenAddress, minDenomAmount }) => { | ||
return { | ||
token: tokenAddress, | ||
amount: toDisplayAmount(namadaAsset(), new BigNumber(minDenomAmount)), | ||
}; | ||
}) | ||
.at(0); | ||
return balance ? BigNumber(balance.amount) : BigNumber(0); | ||
}; | ||
|
||
/** | ||
* Sum the dollar amount of a list of tokens | ||
* @param tokens | ||
* @returns The total of dollars, or `undefined` if at least one token has `dollar: undefined` | ||
*/ | ||
export const sumDollars = (tokens: TokenBalance[]): BigNumber | undefined => { | ||
let sum = new BigNumber(0); | ||
for (let i = 0; i < tokens.length; i++) { | ||
const { dollar } = tokens[i]; | ||
if (!dollar) { | ||
return undefined; | ||
} | ||
sum = sum.plus(dollar); | ||
} | ||
return sum; | ||
}; | ||
|
||
export const getTotalDollar = (list?: TokenBalance[]): BigNumber | undefined => | ||
sumDollars(list ?? []); | ||
|
||
export const getTotalNam = (list?: TokenBalance[]): BigNumber => | ||
list?.find((i) => isNamadaAsset(i.asset))?.amount ?? new BigNumber(0); | ||
|
||
export const tnamAddressToDenomTrace = ( | ||
address: string, | ||
chainTokens: (NativeToken | IbcToken)[] | ||
): DenomTrace | undefined => { | ||
const token = chainTokens.find((entry) => entry.address === address); | ||
const trace = token && "trace" in token ? token.trace : undefined; | ||
|
||
// If no trace, the token is NAM, but return undefined because we only care | ||
// about IBC tokens here | ||
if (typeof trace === "undefined") { | ||
return undefined; | ||
} | ||
|
||
const separatorIndex = trace.lastIndexOf("/"); | ||
if (separatorIndex === -1) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
path: trace.substring(0, separatorIndex), | ||
baseDenom: trace.substring(separatorIndex + 1), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters