e2e_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package e2e
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  7. "github.com/mr-tron/base58"
  8. "k8s.io/client-go/kubernetes"
  9. "github.com/certusone/wormhole/node/pkg/devnet"
  10. "github.com/certusone/wormhole/node/pkg/ethereum"
  11. "github.com/certusone/wormhole/node/pkg/vaa"
  12. "github.com/ethereum/go-ethereum/ethclient"
  13. )
  14. func setup(t *testing.T) (*kubernetes.Clientset, *ethclient.Client, *bind.TransactOpts, *TerraClient) {
  15. // List of pods we need in a ready state before we can run tests.
  16. want := []string{
  17. // Our test guardian set.
  18. "guardian-0",
  19. //"guardian-1",
  20. //"guardian-2",
  21. //"guardian-3",
  22. //"guardian-4",
  23. //"guardian-5",
  24. // Connected chains
  25. "solana-devnet-0",
  26. "terra-terrad-0",
  27. "terra-lcd-0",
  28. "eth-devnet-0",
  29. }
  30. c := getk8sClient()
  31. // Wait for all pods to be ready. This blocks until the bridge is ready to receive lockups.
  32. ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
  33. defer cancel()
  34. waitForPods(ctx, c, want)
  35. if ctx.Err() != nil {
  36. t.Fatal(ctx.Err())
  37. }
  38. // Ethereum client.
  39. ec, err := ethclient.Dial(devnet.GanacheRPCURL)
  40. if err != nil {
  41. t.Fatalf("dialing devnet eth rpc failed: %v", err)
  42. }
  43. kt := devnet.GetKeyedTransactor(context.Background())
  44. // Terra client
  45. tc, err := NewTerraClient()
  46. if err != nil {
  47. t.Fatalf("creating devnet terra client failed: %v", err)
  48. }
  49. return c, ec, kt, tc
  50. }
  51. // Careful about parallel tests - accounts on some chains like Ethereum cannot be
  52. // used concurrently as they have monotonically increasing nonces that would conflict.
  53. // Either use different Ethereum account, or do not run Ethereum tests in parallel.
  54. func TestEndToEnd_SOL_ETH(t *testing.T) {
  55. c, ec, kt, _ := setup(t)
  56. t.Run("[SOL] Native -> [ETH] Wrapped", func(t *testing.T) {
  57. testSolanaLockup(t, context.Background(), ec, c,
  58. // Source SPL account
  59. devnet.SolanaExampleTokenOwningAccount,
  60. // Source SPL token
  61. devnet.SolanaExampleToken,
  62. // Our wrapped destination token on Ethereum
  63. devnet.GanacheExampleERC20WrappedSOL,
  64. // Amount of SPL token value to transfer.
  65. 50*devnet.SolanaDefaultPrecision,
  66. // Same precision - same amount, no precision gained.
  67. 0,
  68. )
  69. })
  70. t.Run("[ETH] Wrapped -> [SOL] Native", func(t *testing.T) {
  71. testEthereumLockup(t, context.Background(), ec, kt, c,
  72. // Source ERC20 token
  73. devnet.GanacheExampleERC20WrappedSOL,
  74. // Destination SPL token account
  75. devnet.SolanaExampleTokenOwningAccount,
  76. // Amount (the reverse of what the previous test did, with the same precision because
  77. // the wrapped ERC20 is set to the original asset's 10**9 precision).
  78. 50*devnet.SolanaDefaultPrecision,
  79. // No precision loss
  80. 0,
  81. )
  82. })
  83. t.Run("[ETH] Native -> [SOL] Wrapped", func(t *testing.T) {
  84. testEthereumLockup(t, context.Background(), ec, kt, c,
  85. // Source ERC20 token
  86. devnet.GanacheExampleERC20Token,
  87. // Destination SPL token account
  88. devnet.SolanaExampleWrappedERCTokenOwningAccount,
  89. // Amount
  90. 0.000000012*devnet.ERC20DefaultPrecision,
  91. // We lose 9 digits of precision on this path, as the default ERC20 token has 10**18 precision.
  92. 9,
  93. )
  94. })
  95. t.Run("[SOL] Wrapped -> [ETH] Native", func(t *testing.T) {
  96. testSolanaLockup(t, context.Background(), ec, c,
  97. // Source SPL account
  98. devnet.SolanaExampleWrappedERCTokenOwningAccount,
  99. // Source SPL token
  100. devnet.SolanaExampleWrappedERCToken,
  101. // Our wrapped destination token on Ethereum
  102. devnet.GanacheExampleERC20Token,
  103. // Amount of SPL token value to transfer.
  104. 0.000000012*devnet.SolanaDefaultPrecision,
  105. // We gain 9 digits of precision on Eth.
  106. 9,
  107. )
  108. })
  109. }
  110. func TestEndToEnd_SOL_Terra(t *testing.T) {
  111. c, _, _, tc := setup(t)
  112. t.Run("[Terra] Native -> [SOL] Wrapped", func(t *testing.T) {
  113. testTerraLockup(t, context.Background(), tc, c,
  114. // Source CW20 token
  115. devnet.TerraTokenAddress,
  116. // Destination SPL token account
  117. devnet.SolanaExampleWrappedCWTokenOwningAccount,
  118. // Amount
  119. 2*devnet.TerraDefaultPrecision,
  120. // Same precision - same amount, no precision gained.
  121. 0,
  122. )
  123. })
  124. t.Run("[SOL] Wrapped -> [Terra] Native", func(t *testing.T) {
  125. testSolanaToTerraLockup(t, context.Background(), c,
  126. // Source SPL account
  127. devnet.SolanaExampleWrappedCWTokenOwningAccount,
  128. // Source SPL token
  129. devnet.SolanaExampleWrappedCWToken,
  130. // Wrapped
  131. false,
  132. // Amount of SPL token value to transfer.
  133. 2*devnet.TerraDefaultPrecision,
  134. // Same precision - same amount, no precision gained.
  135. 0,
  136. )
  137. })
  138. t.Run("[SOL] Native -> [Terra] Wrapped", func(t *testing.T) {
  139. testSolanaToTerraLockup(t, context.Background(), c,
  140. // Source SPL account
  141. devnet.SolanaExampleTokenOwningAccount,
  142. // Source SPL token
  143. devnet.SolanaExampleToken,
  144. // Native
  145. true,
  146. // Amount of SPL token value to transfer.
  147. 50*devnet.SolanaDefaultPrecision,
  148. // Same precision - same amount, no precision gained.
  149. 0,
  150. )
  151. })
  152. t.Run("[Terra] Wrapped -> [SOL] Native", func(t *testing.T) {
  153. tokenSlice, err := base58.Decode(devnet.SolanaExampleToken)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. wrappedAsset, err := waitTerraAsset(t, context.Background(), devnet.TerraBridgeAddress, vaa.ChainIDSolana, tokenSlice)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. testTerraLockup(t, context.Background(), tc, c,
  162. // Source wrapped token
  163. wrappedAsset,
  164. // Destination SPL token account
  165. devnet.SolanaExampleTokenOwningAccount,
  166. // Amount of Terra token value to transfer.
  167. 50*devnet.SolanaDefaultPrecision,
  168. // Same precision
  169. 0,
  170. )
  171. })
  172. }
  173. func TestEndToEnd_ETH_Terra(t *testing.T) {
  174. _, ec, kt, tc := setup(t)
  175. t.Run("[Terra] Native -> [ETH] Wrapped", func(t *testing.T) {
  176. testTerraToEthLockup(t, context.Background(), tc, ec,
  177. // Source CW20 token
  178. devnet.TerraTokenAddress,
  179. // Destination ETH token
  180. devnet.GanacheExampleERC20WrappedTerra,
  181. // Amount
  182. 2*devnet.TerraDefaultPrecision,
  183. // Same precision - same amount, no precision gained.
  184. 0,
  185. )
  186. })
  187. t.Run("[ETH] Wrapped -> [Terra] Native", func(t *testing.T) {
  188. testEthereumToTerraLockup(t, context.Background(), ec, kt,
  189. // Source Ethereum token
  190. devnet.GanacheExampleERC20WrappedTerra,
  191. // Wrapped
  192. false,
  193. // Amount of Ethereum token value to transfer.
  194. 2*devnet.TerraDefaultPrecision,
  195. // Same precision
  196. 0,
  197. )
  198. })
  199. t.Run("[ETH] Native -> [Terra] Wrapped", func(t *testing.T) {
  200. testEthereumToTerraLockup(t, context.Background(), ec, kt,
  201. // Source Ethereum token
  202. devnet.GanacheExampleERC20Token,
  203. // Native
  204. true,
  205. // Amount of Ethereum token value to transfer.
  206. 0.000000012*devnet.ERC20DefaultPrecision,
  207. // We lose 9 digits of precision on this path, as the default ERC20 token has 10**18 precision.
  208. 9,
  209. )
  210. })
  211. t.Run("[Terra] Wrapped -> [ETH] Native", func(t *testing.T) {
  212. paddedTokenAddress := ethereum.PadAddress(devnet.GanacheExampleERC20Token)
  213. wrappedAsset, err := waitTerraAsset(t, context.Background(), devnet.TerraBridgeAddress, vaa.ChainIDEthereum, paddedTokenAddress[:])
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. testTerraToEthLockup(t, context.Background(), tc, ec,
  218. // Source wrapped token
  219. wrappedAsset,
  220. // Destination ETH token
  221. devnet.GanacheExampleERC20Token,
  222. // Amount of Terra token value to transfer.
  223. 0.000000012*1e9, // 10**9 because default ETH precision is 18 and we lost 9 digits on wrapping
  224. // We gain 9 digits of precision on Eth.
  225. 9,
  226. )
  227. })
  228. }