-
Notifications
You must be signed in to change notification settings - Fork 14
/
agent-token.ts
103 lines (89 loc) · 3.32 KB
/
agent-token.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
Fadroma: Base Token Support
Copyright (C) 2023 Hack.bg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
import type { Agent, Address, ClientClass, Uint128, ICoin } from './agent'
import { Coin, Client } from './agent-client'
/** An identifiable token on a network. */
export abstract class Token {
/** The token's unique id. */
abstract get id (): string
/** Whether this token is fungible. */
abstract isFungible (): this is TokenFungible
}
/** An abstract non-fungible token. */
export abstract class TokenNonFungible extends Token {
/** @returns false */
isFungible = () => false
}
/** An abstract fungible token. */
export abstract class TokenFungible extends Token {
/** @returns true */
isFungible = () => true
/** Whether this token is natively supported by the chain. */
abstract isNative (): this is NativeToken
/** Whether this token is implemented by a smart contract. */
abstract isCustom (): this is CustomToken
}
/** The chain's natively implemented token (such as SCRT on Secret Network). */
export class NativeToken extends TokenFungible {
constructor (readonly denom: string) { super() }
/** The token's unique id. */
get id () { return this.denom }
/** @returns false */
isCustom = () => false
/** @returns true */
isNative = () => true
}
/** A contract-based token. */
export class CustomToken extends TokenFungible {
constructor (readonly addr: Address, readonly hash?: string) { super() }
/** The token contract's address. */
get id () { return this.addr }
/** @returns true */
isCustom = () => true
/** @returns false */
isNative = () => false
/** @returns Client */
asClient <C extends ClientClass<Client>> (
agent?: Agent, $C: C = Client as unknown as C
): InstanceType<C> {
const options = { agent, address: this.addr, codeHash: this.hash }
return new $C(options) as InstanceType<C>
}
}
/** A pair of tokens. */
export class Pair {
constructor (readonly a: Token, readonly b: Token) {}
get reverse (): Pair { return new Pair(this.b, this.a) }
}
/** An amount of a fungible token. */
export class Amount {
constructor (public token: TokenFungible, public amount: Uint128) {}
/** Pass this to 'send' field of ExecOpts. */
get asNativeBalance (): ICoin[] {
if (this.token.isNative()) return [new Coin(this.amount, this.token.denom)]
return []
}
}
/** A pair of token amounts. */
export class Swap {
constructor (readonly a: Amount, readonly b: Amount) {}
/** Pass this to 'send' field of ExecOpts */
get asNativeBalance (): ICoin[] {
let result: ICoin[] = []
if (this.a.token.isNative()) result = [...result, ...this.a.asNativeBalance]
if (this.b.token.isNative()) result = [...result, ...this.b.asNativeBalance]
return result
}
}