1
1
import { ExecuteResult , SigningCosmWasmClient } from 'secretjs' ;
2
2
import { divDecimals , unlockToken } from '../../utils' ;
3
3
4
- export const Snip20SwapHash = ( params : { tx_id : string , address : string } ) : string => {
4
+ export const Snip20SwapHash = ( params : { tx_id : string ; address : string } ) : string => {
5
5
return `${ params . tx_id } |${ params . address } ` ;
6
+ } ;
7
+
8
+ export interface Snip20TokenInfo {
9
+ name : string ;
10
+ symbol : string ;
11
+ decimals : number ;
12
+ total_supply ?: string ;
6
13
}
7
14
15
+ export const GetSnip20Params = async ( params : {
16
+ secretjs : SigningCosmWasmClient ;
17
+ address : string ;
18
+ } ) : Promise < Snip20TokenInfo > => {
19
+ const { secretjs, address } = params ;
20
+
21
+ try {
22
+ const paramsResponse = await secretjs . queryContractSmart ( address , { token_info : { } } ) ;
8
23
9
- export const Snip20GetBalance = async ( params : { secretjs : SigningCosmWasmClient , token : string , address : string , key : string , decimals : number } ) => {
24
+ return {
25
+ name : paramsResponse . token_info . name ,
26
+ symbol : paramsResponse . token_info . symbol ,
27
+ decimals : paramsResponse . token_info . decimals ,
28
+ total_supply : paramsResponse . token_info ?. total_supply ,
29
+ } ;
30
+ } catch ( e ) {
31
+ throw Error ( 'Failed to get info' ) ;
32
+ }
33
+ } ;
10
34
11
- const { secretjs, address, token, key, decimals } = params ;
35
+ export const Snip20GetBalance = async ( params : {
36
+ secretjs : SigningCosmWasmClient ;
37
+ token : string ;
38
+ address : string ;
39
+ key : string ;
40
+ decimals : number ;
41
+ } ) => {
42
+ const { secretjs, address, token, key, decimals } = params ;
12
43
13
44
let balanceResponse ;
14
45
try {
@@ -19,7 +50,7 @@ export const Snip20GetBalance = async (params: { secretjs: SigningCosmWasmClient
19
50
} ,
20
51
} ) ;
21
52
} catch ( e ) {
22
- console . log ( e )
53
+ console . log ( e ) ;
23
54
return unlockToken ;
24
55
}
25
56
@@ -30,42 +61,47 @@ export const Snip20GetBalance = async (params: { secretjs: SigningCosmWasmClient
30
61
if ( Number ( balanceResponse . balance . amount ) === 0 ) {
31
62
return '0' ;
32
63
}
33
- return divDecimals (
34
- balanceResponse . balance . amount ,
35
- decimals ,
36
- ) ;
37
- }
64
+ return divDecimals ( balanceResponse . balance . amount , decimals ) ;
65
+ } ;
38
66
39
- export const Snip20SendToBridge = async ( params : { secretjs : SigningCosmWasmClient , address : string , amount : string , msg : string , recipient ?: string } ) : Promise < string > => {
40
- const tx = await Snip20Send ( { recipient : params . recipient || process . env . SCRT_SWAP_CONTRACT , ...params } ) ;
67
+ export const Snip20SendToBridge = async ( params : {
68
+ secretjs : SigningCosmWasmClient ;
69
+ address : string ;
70
+ amount : string ;
71
+ msg : string ;
72
+ recipient ?: string ;
73
+ } ) : Promise < string > => {
74
+ const tx = await Snip20Send ( {
75
+ recipient : params . recipient || process . env . SCRT_SWAP_CONTRACT ,
76
+ ...params ,
77
+ } ) ;
41
78
42
- const txIdKvp = tx . logs [ 0 ] . events [ 1 ] . attributes . find (
43
- kv => kv . key === 'tx_id' ,
44
- ) ;
79
+ const txIdKvp = tx . logs [ 0 ] . events [ 1 ] . attributes . find ( kv => kv . key === 'tx_id' ) ;
45
80
46
81
let tx_id : string ;
47
82
if ( txIdKvp && txIdKvp . value ) {
48
83
tx_id = txIdKvp . value ;
49
84
} else {
50
- throw new Error ( " Failed to get tx_id" )
85
+ throw new Error ( ' Failed to get tx_id' ) ;
51
86
}
52
87
53
- return tx_id
54
- }
55
-
56
- export const Snip20Send = async ( params : { secretjs : SigningCosmWasmClient , address : string , amount : string , msg : string , recipient : string } ) : Promise < ExecuteResult > => {
88
+ return tx_id ;
89
+ } ;
57
90
58
- const { secretjs, address, amount, msg, recipient } = params ;
91
+ export const Snip20Send = async ( params : {
92
+ secretjs : SigningCosmWasmClient ;
93
+ address : string ;
94
+ amount : string ;
95
+ msg : string ;
96
+ recipient : string ;
97
+ } ) : Promise < ExecuteResult > => {
98
+ const { secretjs, address, amount, msg, recipient } = params ;
59
99
60
- return await secretjs . execute (
61
- address ,
62
- {
63
- send : {
64
- amount,
65
- recipient,
66
- msg,
67
- } ,
100
+ return await secretjs . execute ( address , {
101
+ send : {
102
+ amount,
103
+ recipient,
104
+ msg,
68
105
} ,
69
- ) ;
70
-
71
- }
106
+ } ) ;
107
+ } ;
0 commit comments