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

Format ts codes #1199

Merged
merged 6 commits into from
May 16, 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ go/
gocache/
go.work*
control/target/
web/packages/operations/.env.polkadot
web/packages/operations/.env.rococo
3 changes: 2 additions & 1 deletion relayer/cmd/import_beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"fmt"
"os"

"github.com/snowfork/snowbridge/relayer/relays/beacon/config"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer"
"github.com/snowfork/snowbridge/relayer/relays/beacon/header/syncer/api"
"github.com/snowfork/snowbridge/relayer/relays/beacon/protocol"
"github.com/snowfork/snowbridge/relayer/relays/beacon/store"
"os"

_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
Expand Down
5 changes: 4 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
},
"devDependencies": {
"@remix-project/remixd": "^0.6.14",
"turbo": "^1.9.4"
"turbo": "^1.9.4",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.4.5"
}
}
45 changes: 26 additions & 19 deletions web/packages/api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"prefer-const": ["off", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}]
},
"ignorePatterns": ["dist", "artifacts", "build", "contracts", "truffle-config.js"]
}
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"prefer-const": [
"off",
{
"destructuring": "any",
"ignoreReadBeforeAssign": false
}
]
},
"ignorePatterns": [
"dist",
"artifacts",
"build",
"contracts",
"truffle-config.js"
]
}
42 changes: 21 additions & 21 deletions web/packages/api/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 100,
"tabWidth": 4,
"singleQuote": false,
"bracketSpacing": true,
"explicitTypes": "always"
}
},
{
"files": "*.ts",
"options": {
"semi": false,
"printWidth": 100,
"tabWidth": 4,
"singleQuote": false
}
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 100,
"tabWidth": 4,
"singleQuote": false,
"bracketSpacing": true,
"explicitTypes": "always"
}
]
}
},
{
"files": "*.ts",
"options": {
"semi": false,
"printWidth": 100,
"tabWidth": 4,
"singleQuote": false
}
}
]
}
3 changes: 2 additions & 1 deletion web/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"lint": "eslint ."
"lint": "eslint .",
"format": "prettier . --write"
},
"devDependencies": {
"@types/node": "^18.19.31",
Expand Down
63 changes: 46 additions & 17 deletions web/packages/api/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ApiPromise } from "@polkadot/api"
import { Codec, Registry } from "@polkadot/types/types"
import { IERC20Metadata__factory, IERC20__factory } from "@snowbridge/contract-types"
import { Context } from './index'
import { Context } from "./index"

export const parachainNativeToken = async (api: ApiPromise): Promise<{
export const parachainNativeToken = async (
api: ApiPromise
): Promise<{
tokenSymbol: string
tokenDecimal: number
ss58Format: number | null
Expand All @@ -19,37 +21,54 @@ export const parachainNativeToken = async (api: ApiPromise): Promise<{
}
}

export const erc20TokenToAssetLocation = (registry: Registry, ethChainId: bigint, token: string) => {
return registry.createType('StagingXcmV3MultiLocation', {
export const erc20TokenToAssetLocation = (
registry: Registry,
ethChainId: bigint,
token: string
) => {
return registry.createType("StagingXcmV3MultiLocation", {
parents: 2,
interior: {
X2: [
{ GlobalConsensus: { Ethereum: { chain_id: ethChainId } } },
{ AccountKey20: { key: token } },
]
}
],
},
})
}

export const assetStatusInfo = async (context: Context, tokenAddress: string, ownerAddress?: string) => {
export const assetStatusInfo = async (
context: Context,
tokenAddress: string,
ownerAddress?: string
) => {
let [ethereumNetwork, isTokenRegistered] = await Promise.all([
context.ethereum.api.getNetwork(),
context.ethereum.contracts.gateway.isTokenRegistered(tokenAddress)
context.ethereum.contracts.gateway.isTokenRegistered(tokenAddress),
])

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

const tokenContract = IERC20__factory.connect(tokenAddress, context.ethereum.api)
let ownerBalance = BigInt(0)
let tokenGatewayAllowance = BigInt(0)
let isValidERC20 = true
try {
const erc20balance = await assetErc20Balance(context, tokenAddress, ownerAddress ?? '0x0000000000000000000000000000000000000000')
const erc20balance = await assetErc20Balance(
context,
tokenAddress,
ownerAddress ?? "0x0000000000000000000000000000000000000000"
)
ownerBalance = erc20balance.balance
tokenGatewayAllowance = erc20balance.gatewayAllowance

} catch {
isValidERC20 = false
}
Expand All @@ -67,9 +86,13 @@ export const assetStatusInfo = async (context: Context, tokenAddress: string, ow
}
}

export const assetErc20Balance = async (context: Context, token: string, owner: string): Promise<{
balance: bigint,
gatewayAllowance: bigint,
export const assetErc20Balance = async (
context: Context,
token: string,
owner: string
): Promise<{
balance: bigint
gatewayAllowance: bigint
}> => {
const tokenContract = IERC20__factory.connect(token, context.ethereum.api)
const gateway = await context.ethereum.contracts.gateway.getAddress()
Expand All @@ -78,7 +101,8 @@ export const assetErc20Balance = async (context: Context, token: string, owner:
tokenContract.allowance(owner, gateway),
])
return {
balance, gatewayAllowance
balance,
gatewayAllowance,
}
}

Expand All @@ -92,7 +116,12 @@ export const assetErc20Metadata = async (context: Context, tokenAddress: string)
return { name, symbol, decimals }
}

export const palletAssetsBalance = async (api: ApiPromise, location: Codec, address: string, instance = "assets"): Promise<bigint | null> => {
export const palletAssetsBalance = async (
api: ApiPromise,
location: Codec,
address: string,
instance = "assets"
): Promise<bigint | null> => {
let account = (await api.query[instance].account(location, address)).toPrimitive() as any
if (account !== null) {
return BigInt(account.balance)
Expand Down
Loading
Loading