Skip to content

Commit 92b4711

Browse files
committed
chore(ts-sdk-sui): added suitoken
Signed-off-by: kaancaglan <[email protected]>
1 parent f863ed5 commit 92b4711

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

ts-sdk/src/Token.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,41 @@ import { constFalse, constTrue } from "effect/Function"
88
import * as Chain from "./schema/chain.js"
99
import * as Hex from "./schema/hex.js"
1010
import * as Utils from "./Utils.js"
11+
import { isValidSuiAddress, normalizeSuiAddress } from "@mysten/sui/utils"
12+
13+
/**
14+
* @category schemas
15+
* @since 2.0.0
16+
*/
17+
export class SuiCoin extends S.TaggedClass<SuiCoin>()("SuiCoin", {
18+
address: S.String.pipe(
19+
S.filter((value) => {
20+
const parts = value.split("::")
21+
if (parts.length !== 3) return false
22+
const [addr, module, name] = parts
23+
24+
const ident = /^[A-Za-z_][A-Za-z0-9_]*$/
25+
if (!ident.test(module) || !ident.test(name)) return false
26+
27+
const norm = normalizeSuiAddress(addr)
28+
return isValidSuiAddress(norm)
29+
}, {
30+
description:
31+
"Sui coin type in the form 0x<hex>::<Module>::<Name> with a valid Sui address and Move identifiers",
32+
}),
33+
S.annotations({
34+
examples: [
35+
"0x2::sui::SUI",
36+
"0x9003c05db750fe8fb33d8e9a7de814b2ca1af024dc67e06f8529260b03d86fdd::usdt_faucet::USDT_FAUCET",
37+
],
38+
}),
39+
),
40+
}) {
41+
[Hash.symbol](): number {
42+
return Hash.string(this.address)
43+
}
44+
}
45+
1146

1247
/**
1348
* @category schemas
@@ -132,6 +167,7 @@ export const Any = S.Union(
132167
CosmosTokenFactory,
133168
CosmosBank,
134169
CosmosIbcClassic,
170+
SuiCoin,
135171
)
136172
/**
137173
* @category models
@@ -154,6 +190,7 @@ export const TokenFromString = S.transformOrFail(
154190
S.decodeEither(CosmosIbcClassic)({ _tag: "CosmosIbcClassic", address }),
155191
S.decodeEither(CosmosTokenFactory)({ _tag: "CosmosTokenFactory", address }),
156192
S.decodeEither(Cw20)({ _tag: "Cw20", address }),
193+
S.decodeEither(SuiCoin)({ _tag: "SuiCoin", address }),
157194
]),
158195
Effect.orElse(() => S.decodeEither(Erc20)({ _tag: "Erc20", address })),
159196
Effect.orElse(() => S.decodeEither(CosmosBank)({ _tag: "CosmosBank", address })),
@@ -184,6 +221,8 @@ export const AnyFromEncoded = (rpcType: Chain.RpcType) =>
184221
)),
185222
Match.when("aptos", (fromA) =>
186223
Effect.fail(new ParseResult.Type(ast, fromA, "Aptos not supported."))),
224+
Match.when("sui", () =>
225+
pipe(fromA, S.decode(S.compose(Hex.StringFromHex, TokenFromString)))),
187226
Match.exhaustive,
188227
Effect.catchTag("ParseError", (error) =>
189228
ParseResult.fail(error.issue)),
@@ -196,6 +235,17 @@ export const AnyFromEncoded = (rpcType: Chain.RpcType) =>
196235
},
197236
)
198237

238+
export const normalizeSuiTypeTag = (t: string): string => {
239+
const [addr, mod, name] = t.split("::")
240+
return `${normalizeSuiAddress(addr)}::${mod}::${name}`
241+
}
242+
243+
const isNativeSui = (t: string): boolean => {
244+
// compare on normalized address to avoid short/long mismatch
245+
const norm = normalizeSuiTypeTag(t)
246+
return norm === "0x2::sui::SUI"
247+
}
248+
199249
/**
200250
* @category predicates
201251
* @since 2.0.0
@@ -208,5 +258,6 @@ export const isNative = Match.type<Any>().pipe(
208258
Cw20: constFalse,
209259
Erc20: constFalse,
210260
EvmGas: constTrue,
261+
SuiCoin: (t) => isNativeSui(t.address),
211262
}),
212263
)

0 commit comments

Comments
 (0)