-
Notifications
You must be signed in to change notification settings - Fork 652
/
address.ts
82 lines (78 loc) · 2.16 KB
/
address.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { LCDClient as TerraLCDClient } from "@terra-money/terra.js";
import { LCDClient as XplaLCDClient } from "@xpla/xpla.js";
import { keccak256 } from "ethers/lib/utils";
import { isNativeDenom } from "../terra";
import {
CHAIN_ID_INJECTIVE,
CHAIN_ID_TERRA,
CHAIN_ID_XPLA,
coalesceCosmWasmChainId,
CosmWasmChainId,
CosmWasmChainName,
isTerraChain,
} from "../utils";
export const isNativeDenomInjective = (denom: string) => denom === "inj";
export const isNativeDenomXpla = (denom: string) => denom === "axpla";
export function isNativeCosmWasmDenom(
chainId: CosmWasmChainId,
address: string
) {
return (
(isTerraChain(chainId) && isNativeDenom(address)) ||
(chainId === CHAIN_ID_INJECTIVE && isNativeDenomInjective(address)) ||
(chainId === CHAIN_ID_XPLA && isNativeDenomXpla(address))
);
}
export function buildTokenId(
chain: Exclude<
CosmWasmChainId | CosmWasmChainName,
typeof CHAIN_ID_TERRA | "terra"
>,
address: string
) {
const chainId: CosmWasmChainId = coalesceCosmWasmChainId(chain);
return (
(isNativeCosmWasmDenom(chainId, address) ? "01" : "00") +
keccak256(Buffer.from(address, "utf-8")).substring(4)
);
}
export interface ExternalIdResponse {
token_id: {
Bank?: { denom: string };
Contract?: {
NativeCW20?: {
contract_address: string;
};
ForeignToken?: {
chain_id: string;
foreign_address: string;
};
};
};
}
// returns the TokenId corresponding to the ExternalTokenId
// see cosmwasm token_addresses.rs
export const queryExternalId = async (
client: TerraLCDClient | XplaLCDClient,
tokenBridgeAddress: string,
externalTokenId: string
) => {
try {
const response = await client.wasm.contractQuery<ExternalIdResponse>(
tokenBridgeAddress,
{
external_id: {
external_id: Buffer.from(externalTokenId, "hex").toString("base64"),
},
}
);
return (
// response depends on the token type
response.token_id.Bank?.denom ||
response.token_id.Contract?.NativeCW20?.contract_address ||
response.token_id.Contract?.ForeignToken?.foreign_address
);
} catch {
return null;
}
};