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

Fix sui token balances #96

Merged
merged 3 commits into from
Sep 1, 2023
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
2 changes: 1 addition & 1 deletion examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const allChainWallets = {
],
},
sui: {
rebalance: {
enabled: false,
strategy: 'default',
interval: 10000,
minBalanceThreshold: 0.1,
},
wallets: [
{ privateKey: 'ODV9VYi3eSljEWWmpWh8s9m/P2BNNxU/Vp8jwADeNuw=' },
{ address: '0x934042d46762fadf9f61ef07aa265fc14d28525c7051224a5f1cba2409aef307' },
Expand All @@ -33,6 +39,10 @@ const allChainWallets = {
{ address: '0x18337b4c5b964b7645506542589c5ed496e794af82f98b3789fed96f61a94c96' },
{ address: '0x9ae846f88db3476d7c9f2d8fc49722f7085a3b46aad998120dd11ebeab83e021' },
{ address: '0xcb39d897bf0561af7531d37db9781e54528269fed4761275931ce32f20352977' },
{
address: '0x8f11fe7121be742f46e2b3bc2eba081efdc3027697c317a917a2d16fd9b59ab1',
tokens: ['USDC', 'USDT', '0x5d1f47ea69bb0de31c313d7acf89b890dbb8991ea8e03c6c355171f84bb1ba4a::turbos::TURBOS']
},
]
},
klatyn: {
Expand Down
76 changes: 0 additions & 76 deletions examples/wallet-watcher.ts

This file was deleted.

92 changes: 40 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"eslint-plugin-unused-imports": "^2.0.0",
"ganache": "^7.8.0",
"ganache-cli": "^6.12.2",
"jest": "^29.5.0",
"grpc-tools": "^1.12.4",
"jest": "^29.5.0",
"prettier": "^2.8.7",
"ts-jest": "^29.1.0",
"typescript": "^5.0.2"
Expand Down
4 changes: 3 additions & 1 deletion src/balances/sui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseFixed } from '@ethersproject/bignumber';
export type SuiTokenData = {
symbol: string;
decimals: number;
address: string;
};

export interface SuiTransactionDetails {
Expand Down Expand Up @@ -45,7 +46,8 @@ export async function pullSuiTokenData(

return {
symbol: coinData.symbol,
decimals: coinData.decimals
decimals: coinData.decimals,
address: address
};
}

Expand Down
26 changes: 15 additions & 11 deletions src/wallets/sui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
BaseWalletOptions,
TransferRecepit, WalletData,
} from "../base-wallet";
import { pullSuiNativeBalance, pullSuiTokenBalances, pullSuiTokenData, transferSuiNativeBalance } from "../../balances/sui";
import {
SuiTokenData,
pullSuiNativeBalance,
pullSuiTokenBalances,
pullSuiTokenData,
transferSuiNativeBalance
} from "../../balances/sui";

import {
SUI_CHAIN_CONFIG,
Expand Down Expand Up @@ -43,13 +49,12 @@ export type SuiDefaultConfigs = Record<string, SuiDefaultConfig>;

export type SuiChainName = keyof typeof SUI_CHAINS;

// TODO: See other token types
const SUI_HEX_ADDRESS_REGEX = /^0x[a-fA-F0-9]{64}::coin::COIN$/;
const SUI_HEX_ADDRESS_REGEX = /^0x[a-fA-F0-9]{64}::\w+::\w+$/;

export class SuiWalletToolbox extends WalletToolbox {
provider: Connection;
private chainConfig: SuiChainConfig;
private tokenData: Record<string, any> = {};
private tokenData: Record<string, SuiTokenData> = {};
public options: SuiWalletOptions;

constructor(
Expand Down Expand Up @@ -105,13 +110,12 @@ export class SuiWalletToolbox extends WalletToolbox {
}

public parseTokensConfig(tokens: string[], failOnInvalidTokens: boolean): string[] {
const knownTokens = this.getKnownTokens();
const validTokens: string[] = [];
for (const token of tokens) {
if (this.isValidNativeTokenAddress(token)) {
validTokens.push(token);
} else if (this.isKnownToken(token)) {
abhidtu2014 marked this conversation as resolved.
Show resolved Hide resolved
validTokens.push(token.toUpperCase());
validTokens.push(this.getKnownTokens()[token.toUpperCase()]);
} else if (failOnInvalidTokens) {
throw new Error(`Invalid token address: ${token}`);
}
Expand All @@ -129,10 +133,10 @@ export class SuiWalletToolbox extends WalletToolbox {

public async warmup() {
const tokens = this.walletTokens(this.wallets);
await mapConcurrent(tokens, async (token: string): Promise<void> => {
// token can be stored as symbol or address, so we normalize to address here
const tokenAddress = this.isKnownToken(token) ? this.getKnownTokens()[token] : token;
this.tokenData[tokenAddress] = await pullSuiTokenData(this.provider, tokenAddress);
await mapConcurrent(tokens, async (tokenAddress: string): Promise<void> => {
// tokens has addresses, per this.parseTokensConfig() contract
const tokenData = await pullSuiTokenData(this.provider, tokenAddress);
this.tokenData[tokenAddress] = tokenData;
}, 1);

this.logger.debug(`Sui token data: ${JSON.stringify(this.tokenData)}`);
Expand Down Expand Up @@ -170,7 +174,7 @@ export class SuiWalletToolbox extends WalletToolbox {
const symbol: string = tokenData?.symbol ? tokenData.symbol : "";

for (const balance of allBalances) {
if (balance.coinType === tokenAddress) {
if (balance.coinType === tokenData.address) {

const formattedBalance = formatFixed(
balance.totalBalance,
Expand Down
7 changes: 5 additions & 2 deletions test/wallets/sui/sui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ jest.mock("../../../src/balances/sui", () => ({
return {
symbol: "USDC",
decimals: 6,
address: "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN"
};
case "0xfded065d61215141ea7afac2e5c1a7b23514a91cbb2cfd86a38ed97e2f7871ac::PEPE::PEPE":
return {
symbol: "PEPE",
decimals: 9,
address: "0xfded065d61215141ea7afac2e5c1a7b23514a91cbb2cfd86a38ed97e2f7871ac::PEPE::PEPE"
};
case "0xd01cebc27fe22868df462f33603646549e13a4b279f5e900b99b9843680445e1::SHIBA::SHIBA":
return {
symbol: "SHIBA",
decimals: 9,
address: "0xd01cebc27fe22868df462f33603646549e13a4b279f5e900b99b9843680445e1::SHIBA::SHIBA"
};
}
})
Expand Down Expand Up @@ -69,7 +72,7 @@ describe("sui wallet tests", () => {
const wallets: WalletConfig[] = [
{
address: "0x0000000000000000000000000000000000000000000000000000000000000000",
tokens: ["USDC", "PEPE", "SHIBA"],
tokens: ["USDC", "PEPE", "SHIBA", "0x5d1f47ea69bb0de31c313d7acf89b890dbb8991ea8e03c6c355171f84bb1ba4a::turbos::TURBOS"],
},
];
const wallet = createWalletToolbox(
Expand All @@ -80,7 +83,7 @@ describe("sui wallet tests", () => {
);

await wallet.warmup();
expect(pullSuiTokenData).toHaveBeenCalled();
expect(pullSuiTokenData).toHaveBeenCalledTimes(2); // KnownToken (USDC) and Valid Address (TURBOS)

const tokenBalances = await wallet.pullTokenBalances(
"0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down