github.com/wormhole-foundation/wormhole-explorer/common@v0.0.0-20240604151348-09585b5b97c5/domain/chainid_test.go (about) 1 package domain 2 3 import ( 4 "testing" 5 6 "github.com/test-go/testify/assert" 7 sdk "github.com/wormhole-foundation/wormhole/sdk/vaa" 8 ) 9 10 func TestEncodeTrxHashByChainID(t *testing.T) { 11 var tests = []struct { 12 chainID sdk.ChainID 13 txHash []byte 14 want string 15 err error 16 }{ 17 { 18 chainID: sdk.ChainIDSolana, 19 txHash: []byte{0x23, 0xac, 0x49, 0x94, 0x37, 0xa8, 0xe6, 0x53, 0x3b, 0x79, 0x0d, 0x55, 0x78, 0xaf, 0x5d, 0x39, 0xb3, 0x49, 0x88, 0x31, 0x88, 0xec, 0xa5, 0x35, 0xb9, 0x57, 0xd8, 0x2a, 0x0e, 0x77, 0xeb, 0x03}, 20 want: "3QFeCHsG9WDXzMozWyck8RUxmw59jyj7MPnQd4w2mbDL", 21 err: nil, 22 }, 23 { 24 chainID: sdk.ChainIDAlgorand, 25 txHash: []byte{0xd3, 0x45, 0x59, 0x5e, 0x2a, 0x0f, 0xab, 0x5c, 0xde, 0x71, 0x20, 0xb6, 0xbe, 0xb6, 0xee, 0x0b, 0xb9, 0x4b, 0x57, 0x8a, 0xa5, 0x69, 0x95, 0x2d, 0x00, 0x0c, 0xe8, 0xbf, 0xef, 0x03, 0x2d, 0x22}, 26 want: "2NCVSXRKB6VVZXTREC3L5NXOBO4UWV4KUVUZKLIABTUL73YDFURA", 27 err: nil, 28 }, 29 { 30 chainID: sdk.ChainIDEthereum, 31 txHash: []byte{0xb9, 0x11, 0xcb, 0xfb, 0x0e, 0x42, 0xc5, 0x04, 0x77, 0x2b, 0xe9, 0x16, 0xbb, 0xeb, 0x8a, 0x46, 0xfc, 0xe7, 0x2b, 0xe5, 0xc6, 0x11, 0x28, 0xe7, 0x12, 0x93, 0x68, 0x26, 0x32, 0x88, 0xcc, 0x7d}, 32 want: "b911cbfb0e42c504772be916bbeb8a46fce72be5c61128e7129368263288cc7d", 33 err: nil, 34 }, 35 { 36 chainID: sdk.ChainIDNear, 37 txHash: []byte{0x02, 0xde, 0x67, 0xd0, 0x15, 0x34, 0x02, 0x1c, 0x0e, 0x5b, 0x17, 0x68, 0x6e, 0x1e, 0x70, 0xd4, 0x79, 0x39, 0x6d, 0xa2, 0x9d, 0x1e, 0xbc, 0xe4, 0x9a, 0x4c, 0xad, 0xda, 0x4b, 0xca, 0xa3, 0x2b}, 38 want: "CCWhFHoDg5eycFJC7EHbYXnNdXW1ed8tjdNHCLbYZEa", 39 err: nil, 40 }, 41 { 42 chainID: sdk.ChainIDSui, 43 txHash: []byte{0x29, 0xf4, 0xe6, 0xd8, 0xe0, 0xbf, 0x65, 0x21, 0xe5, 0xf3, 0x30, 0x28, 0x73, 0xa1, 0xf0, 0x08, 0x65, 0xb7, 0xcf, 0xe0, 0x48, 0x36, 0x73, 0x4d, 0x74, 0xed, 0x8c, 0x99, 0x6e, 0x7a, 0x07, 0x86}, 44 want: "3pnJrxdjJeDUSvAquDiidApuRLXp5jATdLPyLhjrJsv5", 45 err: nil, 46 }, 47 } 48 49 for _, test := range tests { 50 got, err := EncodeTrxHashByChainID(test.chainID, test.txHash) 51 assert.Equal(t, test.want, got, "EncodeTrxHashByChainID() = %v, want %v", got, test.want) 52 assert.Equal(t, test.err, err, "EncodeTrxHashByChainID() = %v, want %v", err, test.err) 53 } 54 } 55 56 // TestTranslateEmitterAddress contains a test harness for the `TranslateEmitterAddress` function. 57 func TestTranslateEmitterAddress(t *testing.T) { 58 59 // A table defining the test cases 60 tcs := []struct { 61 emitterChain sdk.ChainID 62 emitterAddress string 63 want string 64 err error 65 }{ 66 { 67 // Solana - Token Bridge emitter 68 emitterChain: sdk.ChainIDSolana, 69 emitterAddress: "ec7372995d5cc8732397fb0ad35c0121e0eaa90d26f828a534cab54391b3a4f5", 70 want: "Gv1KWf8DT1jKv5pKBmGaTmVszqa56Xn8YGx2Pg7i7qAk", 71 }, 72 { 73 // Ethereum - Token Bridge emitter 74 emitterChain: sdk.ChainIDEthereum, 75 emitterAddress: "0000000000000000000000003ee18b2214aff97000d974cf647e7c347e8fa585", 76 want: "0x3ee18b2214aff97000d974cf647e7c347e8fa585", 77 }, 78 { 79 // Terra - Token Bridge emitter 80 emitterChain: sdk.ChainIDTerra, 81 emitterAddress: "0000000000000000000000007cf7b764e38a0a5e967972c1df77d432510564e2", 82 want: "terra10nmmwe8r3g99a9newtqa7a75xfgs2e8z87r2sf", 83 }, 84 { 85 // BSC - Token Bridge emitter 86 emitterChain: sdk.ChainIDBSC, 87 emitterAddress: "000000000000000000000000b6f6d86a8f9879a9c87f643768d9efc38c1da6e7", 88 want: "0xb6f6d86a8f9879a9c87f643768d9efc38c1da6e7", 89 }, 90 { 91 // Polygon - Token Bridge emitter 92 emitterChain: sdk.ChainIDPolygon, 93 emitterAddress: "0000000000000000000000005a58505a96d1dbf8df91cb21b54419fc36e93fde", 94 want: "0x5a58505a96d1dbf8df91cb21b54419fc36e93fde", 95 }, 96 { 97 // Avalanche - Token Bridge emitter 98 emitterChain: sdk.ChainIDAvalanche, 99 emitterAddress: "0000000000000000000000000e082f06ff657d94310cb8ce8b0d9a04541d8052", 100 want: "0x0e082f06ff657d94310cb8ce8b0d9a04541d8052", 101 }, 102 { 103 // Oasis - Token Bridge emitter 104 emitterChain: sdk.ChainIDOasis, 105 emitterAddress: "0000000000000000000000005848c791e09901b40a9ef749f2a6735b418d7564", 106 want: "0x5848c791e09901b40a9ef749f2a6735b418d7564", 107 }, 108 { 109 // Algorand - Token Bridge emitter 110 emitterChain: sdk.ChainIDAlgorand, 111 emitterAddress: "67e93fa6c8ac5c819990aa7340c0c16b508abb1178be9b30d024b8ac25193d45", 112 want: "M7UT7JWIVROIDGMQVJZUBQGBNNIIVOYRPC7JWMGQES4KYJIZHVCRZEGFRQ", 113 }, 114 { 115 // Aurora - Token Bridge emitter 116 emitterChain: sdk.ChainIDAurora, 117 emitterAddress: "00000000000000000000000051b5123a7b0f9b2ba265f9c4c8de7d78d52f510f", 118 want: "0x51b5123a7b0f9b2ba265f9c4c8de7d78d52f510f", 119 }, 120 { 121 // Fantom - Token Bridge emitter 122 emitterChain: sdk.ChainIDFantom, 123 emitterAddress: "0000000000000000000000007c9fc5741288cdfdd83ceb07f3ea7e22618d79d2", 124 want: "0x7c9fc5741288cdfdd83ceb07f3ea7e22618d79d2", 125 }, 126 { 127 // Karura - Token Bridge emitter 128 emitterChain: sdk.ChainIDKarura, 129 emitterAddress: "000000000000000000000000ae9d7fe007b3327aa64a32824aaac52c42a6e624", 130 want: "0xae9d7fe007b3327aa64a32824aaac52c42a6e624", 131 }, 132 { 133 // Acala - Token Bridge emitter 134 emitterChain: sdk.ChainIDAcala, 135 emitterAddress: "000000000000000000000000ae9d7fe007b3327aa64a32824aaac52c42a6e624", 136 want: "0xae9d7fe007b3327aa64a32824aaac52c42a6e624", 137 }, 138 { 139 // Klaytn - Token Bridge emitter 140 emitterChain: sdk.ChainIDKlaytn, 141 emitterAddress: "0000000000000000000000005b08ac39eaed75c0439fc750d9fe7e1f9dd0193f", 142 want: "0x5b08ac39eaed75c0439fc750d9fe7e1f9dd0193f", 143 }, 144 { 145 // Celo - Token Bridge emitter 146 emitterChain: sdk.ChainIDCelo, 147 emitterAddress: "000000000000000000000000796dff6d74f3e27060b71255fe517bfb23c93eed", 148 want: "0x796dff6d74f3e27060b71255fe517bfb23c93eed", 149 }, 150 { 151 // NEAR - Token Bridge emitter 152 emitterChain: sdk.ChainIDNear, 153 emitterAddress: "148410499d3fcda4dcfd68a1ebfcdddda16ab28326448d4aae4d2f0465cdfcb7", 154 want: "contract.portalbridge.near", 155 }, 156 { 157 // Moonbeam - Token Bridge emitter 158 emitterChain: sdk.ChainIDMoonbeam, 159 emitterAddress: "000000000000000000000000b1731c586ca89a23809861c6103f0b96b3f57d92", 160 want: "0xb1731c586ca89a23809861c6103f0b96b3f57d92", 161 }, 162 { 163 // Terra2 - Token Bridge emitter 164 emitterChain: sdk.ChainIDTerra2, 165 emitterAddress: "a463ad028fb79679cfc8ce1efba35ac0e77b35080a1abe9bebe83461f176b0a3", 166 want: "terra153366q50k7t8nn7gec00hg66crnhkdggpgdtaxltaq6xrutkkz3s992fw9", 167 }, 168 { 169 // Injective - Token Bridge emitter 170 emitterChain: sdk.ChainIDInjective, 171 emitterAddress: "00000000000000000000000045dbea4617971d93188eda21530bc6503d153313", 172 want: "inj1ghd753shjuwexxywmgs4xz7x2q732vcnxxynfn", 173 }, 174 { 175 // Sui - Token Bridge emitter 176 emitterChain: sdk.ChainIDSui, 177 emitterAddress: "ccceeb29348f71bdd22ffef43a2a19c1f5b5e17c5cca5411529120182672ade5", 178 want: "0xc57508ee0d4595e5a8728974a4a93a787d38f339757230d441e895422c07aba9", 179 }, 180 { 181 // Aptos - Token Bridge emitter 182 emitterChain: sdk.ChainIDAptos, 183 emitterAddress: "0000000000000000000000000000000000000000000000000000000000000001", 184 want: "0x576410486a2da45eee6c949c995670112ddf2fbeedab20350d506328eefc9d4f", 185 }, 186 { 187 // Aptos - NFT Bridge emitter 188 emitterChain: sdk.ChainIDAptos, 189 emitterAddress: "0000000000000000000000000000000000000000000000000000000000000005", 190 want: "0x1bdffae984043833ed7fe223f7af7a3f8902d04129b14f801823e64827da7130", 191 }, 192 { 193 // Arbitrum - Token Bridge emitter 194 emitterChain: sdk.ChainIDArbitrum, 195 emitterAddress: "0000000000000000000000000b2402144bb366a632d14b83f244d2e0e21bd39c", 196 want: "0x0b2402144bb366a632d14b83f244d2e0e21bd39c", 197 }, 198 { 199 // Optimism - Token Bridge emitter 200 emitterChain: sdk.ChainIDOptimism, 201 emitterAddress: "0000000000000000000000001d68124e65fafc907325e3edbf8c4d84499daa8b", 202 want: "0x1d68124e65fafc907325e3edbf8c4d84499daa8b", 203 }, 204 { 205 // Xpla - Token Bridge emitter 206 emitterChain: sdk.ChainIDXpla, 207 emitterAddress: "8f9cf727175353b17a5f574270e370776123d90fd74956ae4277962b4fdee24c", 208 want: "xpla137w0wfch2dfmz7jl2ap8pcmswasj8kg06ay4dtjzw7tzkn77ufxqfw7acv", 209 }, 210 { 211 // Sei - Token Bridge emitter 212 emitterChain: sdk.ChainIDSei, 213 emitterAddress: "86c5fd957e2db8389553e1728f9c27964b22a8154091ccba54d75f4b10c61f5e", 214 want: "sei1smzlm9t79kur392nu9egl8p8je9j92q4gzguewj56a05kyxxra0qy0nuf3", 215 }, 216 } 217 218 // For each test case 219 for i := range tcs { 220 tc := &tcs[i] 221 222 // Make sure that the function returns the expected value 223 emitterNativeAddress, err := TranslateEmitterAddress(tc.emitterChain, tc.emitterAddress) 224 if err != tc.err { 225 t.Fatalf("TranslateEmitterAddress(%s,%s)=%v, want=%v", tc.emitterChain.String(), tc.emitterAddress, err, tc.err) 226 } 227 if emitterNativeAddress != tc.want { 228 t.Fatalf(`TranslateEmitterAddress(%s,%s)="%s", want="%s"`, tc.emitterChain.String(), tc.emitterAddress, emitterNativeAddress, tc.want) 229 } 230 } 231 }