@@ -34,6 +34,8 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
34
34
gatewayAddress : string ;
35
35
gateway : Contract ;
36
36
37
+ tbtcTokenAddr : string ;
38
+
37
39
constructor (
38
40
readonly network : N ,
39
41
readonly chain : C ,
@@ -48,6 +50,11 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
48
50
throw new Error ( 'TBTC contract address is required' ) ;
49
51
}
50
52
53
+ const tbtcToken = TBTCBridge . getNativeTbtcToken ( this . chain ) ;
54
+ if ( ! tbtcToken ) {
55
+ throw new Error ( 'Native tbtc token not found' ) ;
56
+ }
57
+
51
58
this . chainId = nativeChainIds . networkChainToNativeChainId . get (
52
59
network ,
53
60
chain ,
@@ -65,6 +72,8 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
65
72
] ,
66
73
provider ,
67
74
) ;
75
+
76
+ this . tbtcTokenAddr = canonicalAddress ( tbtcToken ) ;
68
77
}
69
78
70
79
static async fromRpc < N extends Network > (
@@ -87,11 +96,6 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
87
96
) : AsyncGenerator < EvmUnsignedTransaction < N , C > > {
88
97
const senderAddress = new EvmAddress ( sender ) . toString ( ) ;
89
98
90
- const tbtcToken = TBTCBridge . getNativeTbtcToken ( this . chain ) ;
91
- if ( ! tbtcToken ) {
92
- throw new Error ( 'Native tbtc token not found' ) ;
93
- }
94
-
95
99
const tx = await this . gateway . sendTbtc ! . populateTransaction (
96
100
amount ,
97
101
toChainId ( recipient . chain ) ,
@@ -101,12 +105,7 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
101
105
) ;
102
106
tx . value = await this . core . getMessageFee ( ) ;
103
107
104
- yield * this . approve (
105
- canonicalAddress ( tbtcToken ) ,
106
- senderAddress ,
107
- amount ,
108
- this . gatewayAddress ,
109
- ) ;
108
+ yield * this . approve ( senderAddress , amount , this . gatewayAddress ) ;
110
109
111
110
yield this . createUnsignedTransaction (
112
111
addFrom ( tx , senderAddress ) ,
@@ -118,6 +117,10 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
118
117
sender : AccountAddress < C > ,
119
118
vaa : TBTCBridge . VAA ,
120
119
) : AsyncGenerator < EvmUnsignedTransaction < N , C > > {
120
+ if ( vaa . payloadName !== 'GatewayTransfer' ) {
121
+ throw new Error ( 'Invalid VAA payload' ) ;
122
+ }
123
+
121
124
const address = new EvmAddress ( sender ) . toString ( ) ;
122
125
123
126
const tx = await this . gateway . receiveTbtc ! . populateTransaction (
@@ -131,14 +134,13 @@ export class EvmTBTCBridge<N extends Network, C extends EvmChains = EvmChains>
131
134
}
132
135
133
136
private async * approve (
134
- token : string ,
135
137
senderAddr : string ,
136
138
amount : bigint ,
137
139
contract : string ,
138
140
) : AsyncGenerator < EvmUnsignedTransaction < N , C > > {
139
141
const tokenContract = EvmPlatform . getTokenImplementation (
140
142
this . provider ,
141
- token ,
143
+ this . tbtcTokenAddr ,
142
144
) ;
143
145
const allowance = await tokenContract . allowance ( senderAddr , contract ) ;
144
146
if ( allowance < amount ) {
0 commit comments