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: support mainnet ICP and DOD token #171

Merged
merged 1 commit into from
Oct 19, 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
919 changes: 919 additions & 0 deletions apps/wallet/public/token-icons/DOD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions apps/wallet/src/apis/react-query/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from "@tanstack/react-query"
import { QueryCacheKey } from "./query-keys"
import { GetAllAssetsAsync } from "../services/token"
import { getSupportedChains } from "../../utils/chain"
import { ChainId, ChainInfo } from "../../utils/basicTypes"
import { AssetId, Chain, ChainAssetType, ChainId, ChainInfo, ChainNetwork, DecimalPlaces } from "../../utils/basicTypes"
import { RootAssetInfo } from "../models"
import hibitIdSession from "../../stores/session"
import BigNumber from "bignumber.js"
Expand All @@ -16,7 +16,35 @@ export const useAllAssetListQuery = () => {
if (!res.isSuccess || !res.value) {
throw new Error('Get all asset list failed')
}
return res.value
return [
...res.value,
{
assetId: AssetId.fromString('90000'), // ICP temp id
chain: Chain.Dfinity,
chainNetwork: ChainNetwork.DfinityMainNet,
chainAssetType: ChainAssetType.Native,
contractAddress: '',
decimalPlaces: DecimalPlaces.fromNumber(8),
isBaseToken: true,
icon: '',
displayName: 'ICP',
assetSymbol: 'ICP',
subAssets: [],
},
{
assetId: AssetId.fromString('90001'), // DOD temp id
chain: Chain.Dfinity,
chainNetwork: ChainNetwork.DfinityMainNet,
chainAssetType: ChainAssetType.ICRC1,
contractAddress: 'cp4zx-yiaaa-aaaah-aqzea-cai',
decimalPlaces: null, // decimal needs to be queried at runtime
isBaseToken: true,
icon: '',
displayName: 'Doge On Doge',
assetSymbol: 'DOD',
subAssets: [],
},
]
},
staleTime: 60 * 60 * 1000, // 1 hour
})
Expand Down
2 changes: 1 addition & 1 deletion apps/wallet/src/utils/chain/chain-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export const Dfinity: ChainInfo = {
nativeAssetDecimals: 8,
supportedSignaturesSchemas: [WalletSignatureSchema.IcpEddsa],
explorer: 'https://dashboard.internetcomputer.org',
rpcUrls: [],
rpcUrls: ['https://ic0.app'],
getTxLink: (txId: string) => {
if (typeof txId !== 'string') {
return ''
Expand Down
25 changes: 20 additions & 5 deletions apps/wallet/src/utils/chain/chain-wallets/dfinity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Secp256k1KeyIdentity } from '@dfinity/identity-secp256k1';
import { createAgent } from '@dfinity/utils'
import { HttpAgent } from "@dfinity/agent";
import { AccountIdentifier, LedgerCanister } from '@dfinity/ledger-icp'
import { IcrcLedgerCanister } from "@dfinity/ledger-icrc";
import { IcrcLedgerCanister, IcrcMetadataResponseEntries } from "@dfinity/ledger-icrc";
import { Principal } from "@dfinity/principal";
import { Icrc49CallCanisterRequest, Icrc49CallCanisterResult, IcrcErrorCode, JsonRpcResponseError, JsonRpcResponseSuccess } from "./types";
import { buildJsonRpcError, buildJsonRpcResponse } from "./utils";
Expand Down Expand Up @@ -71,7 +71,8 @@ export class DfinityChainWallet extends BaseChainWallet {
const balance = await ledger.balance({
owner: Principal.fromText(address)
})
return new BigNumber(String(balance)).shiftedBy(-assetInfo.decimalPlaces.value)
const decimals = assetInfo.decimalPlaces?.value ?? (await this.getIcrcDecimals(ledger))
return new BigNumber(String(balance)).shiftedBy(-decimals)
}

throw new Error(`Dfinity: unsupported chain asset type ${assetInfo.chainAssetType.toString()}`);
Expand All @@ -98,12 +99,13 @@ export class DfinityChainWallet extends BaseChainWallet {
// ICRC
if (assetInfo.chainAssetType.equals(ChainAssetType.ICRC1)) {
const ledger = this.getIcrcLedger(assetInfo.contractAddress)
const decimals = assetInfo.decimalPlaces?.value ?? (await this.getIcrcDecimals(ledger))
const blockIndex = await ledger.transfer({
to: {
owner: Principal.fromText(toAddress),
subaccount: [],
},
amount: BigInt(amount.shiftedBy(assetInfo.decimalPlaces.value).toString()),
amount: BigInt(amount.shiftedBy(decimals).toString()),
})
console.debug(`Dfinity: ICRC transfer blockIndex ${blockIndex}`)
return ''
Expand All @@ -129,7 +131,8 @@ export class DfinityChainWallet extends BaseChainWallet {
if (assetInfo.chainAssetType.equals(ChainAssetType.ICRC1)) {
const ledger = this.getIcrcLedger(assetInfo.contractAddress)
const fee = await ledger.transactionFee({})
return new BigNumber(String(fee)).shiftedBy(-assetInfo.decimalPlaces.value)
const decimals = assetInfo.decimalPlaces?.value ?? (await this.getIcrcDecimals(ledger))
return new BigNumber(String(fee)).shiftedBy(-decimals)
}

throw new Error(`Dfinity: unsupported chain asset type ${assetInfo.chainAssetType.toString()}`);
Expand Down Expand Up @@ -163,7 +166,7 @@ export class DfinityChainWallet extends BaseChainWallet {
this.identity = Secp256k1KeyIdentity.fromSeedPhrase(phrase)
this.agent = await createAgent({
identity: this.identity,
host: RUNTIME_ICRC_HOST || 'https://ic0.app',
host: RUNTIME_ICRC_HOST || this.chainInfo.rpcUrls[0],
fetchRootKey: RUNTIME_ICRC_DEV,
})
}
Expand All @@ -181,4 +184,16 @@ export class DfinityChainWallet extends BaseChainWallet {
canisterId: Principal.fromText(canisterId),
})
}

private getIcrcDecimals = async (ledger: IcrcLedgerCanister) => {
let decimals = 8
const metaData = await ledger.metadata({})
for (const entry of metaData) {
if (entry[0] === IcrcMetadataResponseEntries.DECIMALS) {
decimals = Number(String((entry[1] as any).Nat))
break;
}
}
return decimals
}
}
1 change: 0 additions & 1 deletion apps/wallet/src/utils/chain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const SupportedChainsForTestnet = [
TonTestnet,
// TronNile,
// TODO: Dfinity testnet
Dfinity,
];

export function getChainByChainId(chainId: ChainId | null, devMode?: boolean): ChainInfo | null {
Expand Down
Loading