Skip to content

Commit

Permalink
refactor context
Browse files Browse the repository at this point in the history
  • Loading branch information
alistair-singh committed Dec 2, 2024
1 parent a9a3ea3 commit 69a7619
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 361 deletions.
28 changes: 20 additions & 8 deletions web/packages/api/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import { Codec, Registry } from "@polkadot/types/types"
import { IERC20Metadata__factory, IERC20__factory } from "@snowbridge/contract-types"
import { Context } from "./index"

/**
* @deprecated use Parachain.properties()
*/
export interface NativeAsset {
tokenSymbol: string
tokenDecimal: number
ss58Format: number
}

/**
* @deprecated use Parachain.properties()
*/
export const parachainNativeAsset = async (api: ApiPromise): Promise<NativeAsset> => {
const properties = await api.rpc.system.properties()
const tokenSymbols = properties.tokenSymbol.unwrapOrDefault()
Expand Down Expand Up @@ -42,22 +48,26 @@ export const assetStatusInfo = async (
tokenAddress: string,
ownerAddress?: string
) => {
const assetHub = (await context.assetHub()).api
const ethereum = context.ethereum()
const gateway = context.gateway()

let [ethereumNetwork, isTokenRegistered] = await Promise.all([
context.ethereum.api.getNetwork(),
context.ethereum.contracts.gateway.isTokenRegistered(tokenAddress),
ethereum.getNetwork(),
gateway.isTokenRegistered(tokenAddress),
])

const ethereumChainId = ethereumNetwork.chainId
const multiLocation = erc20TokenToAssetLocation(
context.polkadot.api.assetHub.registry,
assetHub.registry,
ethereumChainId,
tokenAddress
)
const foreignAsset = (
await context.polkadot.api.assetHub.query.foreignAssets.asset(multiLocation)
await assetHub.query.foreignAssets.asset(multiLocation)
).toPrimitive() as { status: "Live" }

const tokenContract = IERC20__factory.connect(tokenAddress, context.ethereum.api)
const tokenContract = IERC20__factory.connect(tokenAddress, ethereum)
let ownerBalance = BigInt(0)
let tokenGatewayAllowance = BigInt(0)
let isValidERC20 = true
Expand Down Expand Up @@ -94,8 +104,9 @@ export const assetErc20Balance = async (
balance: bigint
gatewayAllowance: bigint
}> => {
const tokenContract = IERC20__factory.connect(token, context.ethereum.api)
const gateway = await context.ethereum.contracts.gateway.getAddress()
const ethereum = context.ethereum()
const tokenContract = IERC20__factory.connect(token, ethereum)
const gateway = await context.gateway().getAddress()
const [balance, gatewayAllowance] = await Promise.all([
tokenContract.balanceOf(owner),
tokenContract.allowance(owner, gateway),
Expand All @@ -116,7 +127,8 @@ export const assetErc20Metadata = async (
context: Context,
tokenAddress: string
): Promise<ERC20Metadata> => {
const tokenMetadata = IERC20Metadata__factory.connect(tokenAddress, context.ethereum.api)
const ethereum = context.ethereum()
const tokenMetadata = IERC20Metadata__factory.connect(tokenAddress, ethereum)
const [name, symbol, decimals] = await Promise.all([
tokenMetadata.name(),
tokenMetadata.symbol(),
Expand Down
34 changes: 19 additions & 15 deletions web/packages/api/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ export type Config = {
BEACON_HTTP_API: string
ETHEREUM_API: (secret: string) => string
RELAY_CHAIN_URL: string
ASSET_HUB_URL: string
BRIDGE_HUB_URL: string
GATEWAY_CONTRACT: string
BEEFY_CONTRACT: string
ASSET_HUB_PARAID: number
BRIDGE_HUB_PARAID: number
PRIMARY_GOVERNANCE_CHANNEL_ID: string
SECONDARY_GOVERNANCE_CHANNEL_ID: string
RELAYERS: Relayer[]
PARACHAINS: string[]
PARACHAINS: { [paraId: number]: string }
SUBSCAN_API?: {
RELAY_CHAIN_URL: string
ASSET_HUB_URL: string
Expand Down Expand Up @@ -117,9 +115,11 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
BEACON_HTTP_API: "http://127.0.0.1:9596",
ETHEREUM_API: () => "ws://127.0.0.1:8546",
RELAY_CHAIN_URL: "ws://127.0.0.1:9944",
ASSET_HUB_URL: "ws://127.0.0.1:12144",
BRIDGE_HUB_URL: "ws://127.0.0.1:11144",
PARACHAINS: ["ws://127.0.0.1:13144"],
PARACHAINS: {
1000: "ws://127.0.0.1:12144",
1002: "ws://127.0.0.1:11144",
2000: "ws://127.0.0.1:13144",
},
GATEWAY_CONTRACT: "0xEDa338E4dC46038493b885327842fD3E301CaB39",
BEEFY_CONTRACT: "0x992B9df075935E522EC7950F37eC8557e86f6fdb",
ASSET_HUB_PARAID: 1000,
Expand Down Expand Up @@ -296,9 +296,10 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
BEACON_HTTP_API: "https://lodestar-sepolia.chainsafe.io",
ETHEREUM_API: (key) => `https://eth-sepolia.g.alchemy.com/v2/${key}`,
RELAY_CHAIN_URL: "wss://paseo-rpc.dwellir.com",
ASSET_HUB_URL: "wss://asset-hub-paseo-rpc.dwellir.com",
BRIDGE_HUB_URL: "wss://bridge-hub-paseo.dotters.network",
PARACHAINS: [],
PARACHAINS: {
1000: "wss://asset-hub-paseo-rpc.dwellir.com",
1002: "wss://bridge-hub-paseo.dotters.network",
},
GATEWAY_CONTRACT: "0x5a84b15B618beEE6F6285F6bd2bA20a08673e473",
BEEFY_CONTRACT: "0xE7388f953f50d377D131350490156dB649E5DC10",
ASSET_HUB_PARAID: 1000,
Expand Down Expand Up @@ -588,9 +589,11 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
BEACON_HTTP_API: "https://lodestar-mainnet.chainsafe.io",
ETHEREUM_API: (key) => `https://eth-mainnet.g.alchemy.com/v2/${key}`,
RELAY_CHAIN_URL: "https://polkadot-rpc.dwellir.com",
ASSET_HUB_URL: "wss://asset-hub-polkadot-rpc.dwellir.com",
BRIDGE_HUB_URL: "https://bridge-hub-polkadot-rpc.dwellir.com",
PARACHAINS: ["https://polkadot-mythos-rpc.polkadot.io"],
PARACHAINS: {
1000: "wss://asset-hub-polkadot-rpc.dwellir.com",
1002: "https://bridge-hub-polkadot-rpc.dwellir.com",
3369: "https://polkadot-mythos-rpc.polkadot.io",
},
GATEWAY_CONTRACT: "0x27ca963c279c93801941e1eb8799c23f407d68e7",
BEEFY_CONTRACT: "0x6eD05bAa904df3DE117EcFa638d4CB84e1B8A00C",
ASSET_HUB_PARAID: 1000,
Expand Down Expand Up @@ -682,9 +685,10 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
BEACON_HTTP_API: "https://lodestar-sepolia.chainsafe.io",
ETHEREUM_API: (key) => `https://eth-sepolia.g.alchemy.com/v2/${key}`,
RELAY_CHAIN_URL: "https://westend-rpc.polkadot.io",
ASSET_HUB_URL: "wss://westend-asset-hub-rpc.polkadot.io",
BRIDGE_HUB_URL: "https://westend-bridge-hub-rpc.polkadot.io",
PARACHAINS: [],
PARACHAINS: {
1000: "wss://westend-asset-hub-rpc.polkadot.io",
1002: "https://westend-bridge-hub-rpc.polkadot.io",
},
GATEWAY_CONTRACT: "0x9ed8b47bc3417e3bd0507adc06e56e2fa360a4e9",
BEEFY_CONTRACT: "0x6DFaD3D73A28c48E4F4c616ECda80885b415283a",
ASSET_HUB_PARAID: 1000,
Expand Down
Loading

0 comments on commit 69a7619

Please sign in to comment.