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

feat(PLTF-275): get evm balances from union api #593

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/sdk/src/common/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
35 changes: 34 additions & 1 deletion packages/sdk/src/common/get-currency-asset-type.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -14,13 +15,45 @@ 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)
}
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) {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export interface IApisSdk {
activity: ApiClient.ActivityControllerApi
item: ApiClient.ItemControllerApi
ownership: ApiClient.OwnershipControllerApi
balances: ApiClient.BalanceControllerApi
}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/sdk/src/sdk-blockchains/ethereum/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -46,6 +46,16 @@ 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 ETH balance without wallet with CurrencyId", async () => {
const sdk = createSdk(undefined, "development")
const walletAddress = toUnionAddress("ETHEREUM:0xa14FC5C72222FAce8A1BcFb416aE2571fA1a7a91")
Expand Down
21 changes: 12 additions & 9 deletions packages/sdk/src/sdk-blockchains/ethereum/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,13 +37,17 @@ export class EthereumBalance {
}

async getBalance(address: UnionAddress, currency: RequestCurrency): Promise<BigNumber> {
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<IBlockchainTransaction> {
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand Down