From 115e97c70a630c9b00f216940957f3bb75b96f4f Mon Sep 17 00:00:00 2001 From: Aleksandr Sozinov Date: Wed, 31 Jan 2024 02:42:42 +0700 Subject: [PATCH 1/2] feat(PLTF-275): get evm balances from union api --- packages/sdk/src/common/apis.ts | 1 + .../sdk/src/common/get-currency-asset-type.ts | 35 ++++++++++++++++++- packages/sdk/src/domain.ts | 1 + .../sdk-blockchains/ethereum/balance.test.ts | 22 +++++++++++- .../src/sdk-blockchains/ethereum/balance.ts | 21 ++++++----- yarn.lock | 16 ++++----- 6 files changed, 77 insertions(+), 19 deletions(-) diff --git a/packages/sdk/src/common/apis.ts b/packages/sdk/src/common/apis.ts index cd8b34c1a..a0be38d16 100644 --- a/packages/sdk/src/common/apis.ts +++ b/packages/sdk/src/common/apis.ts @@ -46,6 +46,7 @@ export function createApisSdk( ownership: new ApiClient.OwnershipControllerApi(configuration), order: new ApiClient.OrderControllerApi(configuration), activity: new ApiClient.ActivityControllerApi(configuration), + balances: new ApiClient.BalanceControllerApi(configuration), } } diff --git a/packages/sdk/src/common/get-currency-asset-type.ts b/packages/sdk/src/common/get-currency-asset-type.ts index 9ab41f955..909bc63d2 100644 --- a/packages/sdk/src/common/get-currency-asset-type.ts +++ b/packages/sdk/src/common/get-currency-asset-type.ts @@ -1,7 +1,8 @@ import { toBigNumber, toContractAddress, toCurrencyId, toItemId, ZERO_ADDRESS } from "@rarible/types" import type * as ApiClient from "@rarible/api-client" import { Blockchain } from "@rarible/api-client" -import { isEVMBlockchain } from "../sdk-blockchains/ethereum/common" +import type { AssetType, EthErc20AssetType, EthEthereumAssetType } from "@rarible/api-client/build/models/AssetType" +import { isEVMBlockchain } from "@rarible/sdk-common" import type { RequestCurrency, RequestCurrencyAssetType } from "./domain" export function getCurrencyAssetType(currency: RequestCurrency): RequestCurrencyAssetType { @@ -14,6 +15,18 @@ export function getCurrencyAssetType(currency: RequestCurrency): RequestCurrency } } +export function getEVMCurrencyId( + currency: ApiClient.EthErc20AssetType | ApiClient.EthEthereumAssetType, +): ApiClient.CurrencyId { + if (isRequestCurrencyAssetType(currency)) { + return currency + } else if (isAssetType(currency)) { + return convertEVMAssetTypeToCurrencyId(currency) + } else { + throw new Error(`Unrecognized RequestCurrency ${JSON.stringify(currency)}`) + } +} + export function isRequestCurrencyAssetType(x: RequestCurrency): x is ApiClient.CurrencyId { return typeof x === "string" && !!toCurrencyId(x) } @@ -21,6 +34,26 @@ export function isAssetType(x: RequestCurrency): x is RequestCurrencyAssetType { return typeof x === "object" && "@type" in x } +export function isEth(x: AssetType): x is EthEthereumAssetType { + return x["@type"] === "ETH" +} + +export function isErc20(x: AssetType): x is EthErc20AssetType { + return x["@type"] === "ERC20" +} + +export function convertEVMAssetTypeToCurrencyId( + id: RequestCurrencyAssetType, +): ApiClient.CurrencyId { + if (isEth(id)) { + return toCurrencyId(`${id.blockchain || Blockchain.ETHEREUM}:${ZERO_ADDRESS}`) + } + if (isErc20(id)) { + return toCurrencyId(id.contract) + } + throw new Error(`Unsupported currency type: ${id}`) +} + export function convertCurrencyIdToAssetType(id: ApiClient.CurrencyId): RequestCurrencyAssetType { const { blockchain, contract, tokenId } = getDataFromCurrencyId(id) if (isEVMBlockchain(blockchain) || blockchain === Blockchain.IMMUTABLEX) { diff --git a/packages/sdk/src/domain.ts b/packages/sdk/src/domain.ts index 6fb3b99c7..9ae7519e6 100644 --- a/packages/sdk/src/domain.ts +++ b/packages/sdk/src/domain.ts @@ -148,6 +148,7 @@ export interface IApisSdk { activity: ApiClient.ActivityControllerApi item: ApiClient.ItemControllerApi ownership: ApiClient.OwnershipControllerApi + balances: ApiClient.BalanceControllerApi } /** diff --git a/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts b/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts index 0e1db71e7..e4ccd722c 100644 --- a/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts +++ b/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts @@ -3,8 +3,8 @@ import { EthereumWallet } from "@rarible/sdk-wallet" import type { Address } from "@rarible/types" import { toAddress, toContractAddress, toCurrencyId, toUnionAddress, ZERO_ADDRESS } from "@rarible/types" import type { AssetType } from "@rarible/api-client" -import type { BigNumberValue } from "@rarible/utils" import { Blockchain } from "@rarible/api-client" +import type { BigNumberValue } from "@rarible/utils" import BigNumber from "bignumber.js" import { createWethContract } from "@rarible/ethereum-sdk-test-common" import { retry } from "../../common/retry" @@ -46,6 +46,26 @@ describe("get balance", () => { expect(balance.toString()).toEqual("1.9355") }) + test.concurrent("get Polygon balance without wallet", async () => { + const sdk = createSdk(undefined, "development") + const walletAddress = toUnionAddress("ETHEREUM:0xa14FC5C72222FAce8A1BcFb416aE2571fA1a7a91") + const balance = await sdk.balances.getBalance(walletAddress, { + "@type": "ETH", + blockchain: Blockchain.POLYGON, + }) + expect(balance.toString()).toEqual("0") + }) + + test.concurrent("get Polygon balance without wallet", async () => { + const sdk = createSdk(undefined, "development") + const walletAddress = toUnionAddress("ETHEREUM:0xa14FC5C72222FAce8A1BcFb416aE2571fA1a7a91") + const balance = await sdk.balances.getBalance(walletAddress, { + "@type": "ETH", + blockchain: Blockchain.POLYGON, + }) + expect(balance.toString()).toEqual("0") + }) + test.concurrent("get ETH balance without wallet with CurrencyId", async () => { const sdk = createSdk(undefined, "development") const walletAddress = toUnionAddress("ETHEREUM:0xa14FC5C72222FAce8A1BcFb416aE2571fA1a7a91") diff --git a/packages/sdk/src/sdk-blockchains/ethereum/balance.ts b/packages/sdk/src/sdk-blockchains/ethereum/balance.ts index 2c692a3aa..6a7af2a8d 100644 --- a/packages/sdk/src/sdk-blockchains/ethereum/balance.ts +++ b/packages/sdk/src/sdk-blockchains/ethereum/balance.ts @@ -8,20 +8,19 @@ import { Blockchain } from "@rarible/api-client" import { Action } from "@rarible/action" import type { Maybe } from "@rarible/types/build/maybe" import type { EthereumWallet } from "@rarible/sdk-wallet" +import type * as ApiClient from "@rarible/api-client" +import { extractBlockchain } from "@rarible/sdk-common" import type { ConvertRequest } from "../../types/balances" import type { DepositBiddingBalanceRequest, GetBiddingBalanceRequest, WithdrawBiddingBalanceRequest, } from "../../types/balances" -import { getCurrencyAssetType } from "../../common/get-currency-asset-type" +import { getCurrencyAssetType, getEVMCurrencyId, isErc20, isEth } from "../../common/get-currency-asset-type" import type { RequestCurrency } from "../../common/domain" import type { IApisSdk } from "../../domain" -import { extractBlockchain } from "../../common/extract-blockchain" import { convertEthereumContractAddress, - convertToEthereumAddress, - convertToEthereumAssetType, getWalletNetwork, isEVMBlockchain, } from "./common" @@ -38,13 +37,17 @@ export class EthereumBalance { } async getBalance(address: UnionAddress, currency: RequestCurrency): Promise { - const assetType = convertToEthereumAssetType(getCurrencyAssetType(currency)) - if (assetType.assetClass !== "ETH" && assetType.assetClass !== "ERC20") { + const type = getCurrencyAssetType(currency) + if (!isEth(type) && !isErc20(type)) { throw new Error("Unsupported asset type for getting balance") } - const addressRaw = convertToEthereumAddress(address) - const value = await this.sdk.balances.getBalance(addressRaw, assetType) - return toBn(value) + const response = await this.apis.balances.getBalance({ + currencyId: getEVMCurrencyId( + currency as ApiClient.EthErc20AssetType | ApiClient.EthEthereumAssetType + ), + owner: address, + }) + return toBn(response.decimal) } async convert(request: ConvertRequest): Promise { diff --git a/yarn.lock b/yarn.lock index aa3b8bb68..38dc5b7da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7290,10 +7290,10 @@ dependencies: "@rarible/types" "~0.9.2" -"@rarible/fcl-types@^0.5.76": - version "0.5.77" - resolved "https://registry.yarnpkg.com/@rarible/fcl-types/-/fcl-types-0.5.77.tgz#cab188d14d66b2f46ed44d96d50e97841ca00f02" - integrity sha512-RKD+9qKOxNcibyvDKpupaIe0PIDgk80EFOVx7R+TOozMm9WjQUclU6262C8WX2rvO9RvBWoQ5yXhL6/vHwPk/Q== +"@rarible/fcl-types@^0.5.78": + version "0.5.78" + resolved "https://registry.yarnpkg.com/@rarible/fcl-types/-/fcl-types-0.5.78.tgz#290c04ad92e6855ff63fafd14659bc250e23aba4" + integrity sha512-q/9bOZ+Y5L4UXFUBTNzLAQ/UIgqv0fRKy1C7e5j9j9Bygf6rWgS7qOMpASomDwIpS8EkciaMWlfGIJIIHrpo7w== "@rarible/fcl-types@~0.5.56": version "0.5.56" @@ -7308,13 +7308,13 @@ "@rarible/types" "^0.9.16" "@rarible/flow-sdk@~0.5.76-beta.0": - version "0.5.76-beta.0" - resolved "https://registry.yarnpkg.com/@rarible/flow-sdk/-/flow-sdk-0.5.76-beta.0.tgz#1fd83a55b3d436838caed2f462c691ef52242988" - integrity sha512-wYuGMj4K0FvB3Upx0UCTFirSgLn9ha34xmLmY9OKmGkuqYpMMdWGZFVyJ9UhhWtmmhWIF7otLiPF7dJ62bb2ZA== + version "0.5.78" + resolved "https://registry.yarnpkg.com/@rarible/flow-sdk/-/flow-sdk-0.5.78.tgz#8d9110a2cc53f2f61ad670dedec3fea677ad2fd0" + integrity sha512-64crgdVCH34VG28McxU88PHUeANr7du+GLOt21UV2Pq3Ed4/+Mtqj+CSBGxXMJHYBJ8ujIYSW3AGAunW09zv2g== dependencies: "@onflow/fcl" "~1.3.2" "@onflow/types" "~0.0.5" - "@rarible/fcl-types" "^0.5.76" + "@rarible/fcl-types" "^0.5.78" "@rarible/flow-api-client" "~0.0.5-fix.2" "@rarible/utils" "~0.5.0" From 0147da45f041f6b1e9dfd170d108bbc0037c7a65 Mon Sep 17 00:00:00 2001 From: Aleksandr Sozinov Date: Wed, 31 Jan 2024 02:45:14 +0700 Subject: [PATCH 2/2] chore: balance test --- .../sdk/src/sdk-blockchains/ethereum/balance.test.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts b/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts index e4ccd722c..08d743ca6 100644 --- a/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts +++ b/packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts @@ -56,16 +56,6 @@ describe("get balance", () => { expect(balance.toString()).toEqual("0") }) - test.concurrent("get Polygon balance without wallet", async () => { - const sdk = createSdk(undefined, "development") - const walletAddress = toUnionAddress("ETHEREUM:0xa14FC5C72222FAce8A1BcFb416aE2571fA1a7a91") - const balance = await sdk.balances.getBalance(walletAddress, { - "@type": "ETH", - blockchain: Blockchain.POLYGON, - }) - expect(balance.toString()).toEqual("0") - }) - test.concurrent("get ETH balance without wallet with CurrencyId", async () => { const sdk = createSdk(undefined, "development") const walletAddress = toUnionAddress("ETHEREUM:0xa14FC5C72222FAce8A1BcFb416aE2571fA1a7a91")