| 1 | import "package:cw_core/payment_uris.dart"; |
| 2 | import "package:flutter_test/flutter_test.dart"; |
| 3 | |
| 4 | void main() { |
| 5 | group("TronURI", () { |
| 6 | const address = "TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL"; |
| 7 | const contract = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; |
| 8 | |
| 9 | test("includes the TRC20 contract as the token param", () { |
| 10 | final uri = TronURI(amount: "1.5", address: address, contractAddress: contract); |
| 11 | |
| 12 | expect(uri.toString(), "tron:$address?amount=1.5&token=$contract"); |
| 13 | }); |
| 14 | |
| 15 | test("omits the token param for native TRX", () { |
| 16 | final uri = TronURI(amount: "1.5", address: address); |
| 17 | |
| 18 | expect(uri.toString(), "tron:$address?amount=1.5"); |
| 19 | expect(uri.toString().contains("token="), false); |
| 20 | }); |
| 21 | |
| 22 | test("includes the token param when there is no amount", () { |
| 23 | final uri = TronURI(amount: "", address: address, contractAddress: contract); |
| 24 | |
| 25 | expect(uri.toString(), "tron:$address?token=$contract"); |
| 26 | }); |
| 27 | |
| 28 | test("converts a comma decimal separator in the amount", () { |
| 29 | final uri = TronURI(amount: "1,5", address: address); |
| 30 | |
| 31 | expect(uri.toString(), "tron:$address?amount=1.5"); |
| 32 | }); |
| 33 | |
| 34 | test("emits a bare address without amount or contract", () { |
| 35 | final uri = TronURI(amount: "", address: address); |
| 36 | |
| 37 | expect(uri.toString(), "tron:$address"); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | group("SolanaURI", () { |
| 42 | const address = "4Nd1mYvNQyJ8BDVXLgkvSGpVdQMZ3hxwVFkfwXNq6Wgk"; |
| 43 | const mint = "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"; |
| 44 | |
| 45 | test("includes the SPL mint as the spl-token param", () { |
| 46 | final uri = SolanaURI(amount: "2", address: address, contractAddress: mint); |
| 47 | |
| 48 | expect(uri.toString(), "solana:$address?amount=2&spl-token=$mint"); |
| 49 | }); |
| 50 | |
| 51 | test("omits the spl-token param for native SOL", () { |
| 52 | final uri = SolanaURI(amount: "2", address: address); |
| 53 | |
| 54 | expect(uri.toString(), "solana:$address?amount=2"); |
| 55 | expect(uri.toString().contains("spl-token="), false); |
| 56 | }); |
| 57 | |
| 58 | test("includes the spl-token param when there is no amount", () { |
| 59 | final uri = SolanaURI(amount: "", address: address, contractAddress: mint); |
| 60 | |
| 61 | expect(uri.toString(), "solana:$address?spl-token=$mint"); |
| 62 | }); |
| 63 | |
| 64 | test("converts a comma decimal separator in the amount", () { |
| 65 | final uri = SolanaURI(amount: "0,5", address: address); |
| 66 | |
| 67 | expect(uri.toString(), "solana:$address?amount=0.5"); |
| 68 | }); |
| 69 | |
| 70 | test("emits a bare address without amount or mint", () { |
| 71 | final uri = SolanaURI(amount: "", address: address); |
| 72 | |
| 73 | expect(uri.toString(), "solana:$address"); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | group("ERC681URI", () { |
| 78 | const recipient = "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6"; |
| 79 | const contract = "0xdAC17F958D2ee523a2206206994597C13D831ec7"; |
| 80 | |
| 81 | test("includes the mainnet chainId on a plain native transfer", () { |
| 82 | final uri = ERC681URI(chainId: 1, address: recipient, amount: "", contractAddress: null); |
| 83 | |
| 84 | expect(uri.toString(), "ethereum:$recipient@1"); |
| 85 | }); |
| 86 | |
| 87 | test("appends the chainId for native transfers", () { |
| 88 | final uri = ERC681URI(chainId: 137, address: recipient, amount: "", contractAddress: null); |
| 89 | |
| 90 | expect(uri.toString(), "ethereum:$recipient@137"); |
| 91 | }); |
| 92 | |
| 93 | test("emits the native amount in ERC-681 scientific notation", () { |
| 94 | final uri = ERC681URI(chainId: 1, address: recipient, amount: "1", contractAddress: null); |
| 95 | |
| 96 | expect(uri.toString(), "ethereum:$recipient@1?value=1.0e18"); |
| 97 | }); |
| 98 | |
| 99 | test("includes the mainnet chainId in the transfer form", () { |
| 100 | final uri = ERC681URI( |
| 101 | chainId: 1, |
| 102 | address: recipient, |
| 103 | amount: "200.172148", |
| 104 | contractAddress: contract, |
| 105 | tokenDecimals: 6, |
| 106 | ); |
| 107 | |
| 108 | expect( |
| 109 | uri.toString(), |
| 110 | "ethereum:$contract@1/transfer?address=$recipient&amount=200.172148", |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | test("builds the transfer form for a token", () { |
| 115 | final uri = |
| 116 | ERC681URI(chainId: 137, address: recipient, amount: "", contractAddress: contract); |
| 117 | |
| 118 | expect(uri.toString(), "ethereum:$contract@137/transfer?address=$recipient"); |
| 119 | }); |
| 120 | |
| 121 | test("parses the transfer form back into recipient, contract and chainId", () { |
| 122 | final parsed = ERC681URI.fromUri( |
| 123 | Uri.parse("ethereum:$contract@1/transfer?address=$recipient&uint256=1000000"), |
| 124 | ); |
| 125 | |
| 126 | expect(parsed.address, recipient); |
| 127 | expect(parsed.contractAddress, contract); |
| 128 | expect(parsed.chainId, 1); |
| 129 | expect(parsed.rawTokenAmount, "1000000"); |
| 130 | expect(parsed.amount, ""); |
| 131 | }); |
| 132 | |
| 133 | test("keeps the uint256 raw so the caller can apply the token decimals", () { |
| 134 | final parsed = ERC681URI.fromUri( |
| 135 | Uri.parse( |
| 136 | "ethereum:0xdAC17F958D2ee523a2206206994597C13D831ec7@1/transfer?address=$recipient&uint256=1000000", |
| 137 | ), |
| 138 | ); |
| 139 | |
| 140 | expect(parsed.rawTokenAmount, "1000000"); |
| 141 | expect(parsed.amount, ""); |
| 142 | }); |
| 143 | |
| 144 | test("prefers the amount param over the uint256 when both are present", () { |
| 145 | final parsed = ERC681URI.fromUri( |
| 146 | Uri.parse( |
| 147 | "ethereum:$contract@1/transfer?address=$recipient&uint256=1000000000000000000&amount=1", |
| 148 | ), |
| 149 | ); |
| 150 | |
| 151 | expect(parsed.amount, "1"); |
| 152 | expect(parsed.rawTokenAmount, "1000000000000000000"); |
| 153 | }); |
| 154 | |
| 155 | test("treats a uint256 with a decimal point as a display amount", () { |
| 156 | final parsed = ERC681URI.fromUri( |
| 157 | Uri.parse("ethereum:$contract@1/transfer?address=$recipient&uint256=1.5"), |
| 158 | ); |
| 159 | |
| 160 | expect(parsed.amount, "1.5"); |
| 161 | expect(parsed.rawTokenAmount, null); |
| 162 | }); |
| 163 | |
| 164 | test("parses a native transfer with chainId", () { |
| 165 | final parsed = ERC681URI.fromUri( |
| 166 | Uri.parse("ethereum:$recipient@137?value=2000000000000000000"), |
| 167 | ); |
| 168 | |
| 169 | expect(parsed.address, recipient); |
| 170 | expect(parsed.contractAddress, null); |
| 171 | expect(parsed.chainId, 137); |
| 172 | expect(parsed.amount, "2"); |
| 173 | }); |
| 174 | |
| 175 | test("emits the uint256 and the amount param for an 18 decimal token", () { |
| 176 | final uri = |
| 177 | ERC681URI(chainId: 137, address: recipient, amount: "1", contractAddress: contract); |
| 178 | |
| 179 | expect( |
| 180 | uri.toString(), |
| 181 | "ethereum:$contract@137/transfer?address=$recipient&uint256=1000000000000000000&amount=1", |
| 182 | ); |
| 183 | }); |
| 184 | |
| 185 | test("omits the uint256 for tokens that do not use 18 decimals", () { |
| 186 | final uri = ERC681URI( |
| 187 | chainId: 137, |
| 188 | address: recipient, |
| 189 | amount: "1.5", |
| 190 | contractAddress: contract, |
| 191 | tokenDecimals: 6, |
| 192 | ); |
| 193 | |
| 194 | expect(uri.toString(), "ethereum:$contract@137/transfer?address=$recipient&amount=1.5"); |
| 195 | }); |
| 196 | |
| 197 | test("emits a stored raw token amount verbatim", () { |
| 198 | final uri = ERC681URI( |
| 199 | chainId: 1, |
| 200 | address: recipient, |
| 201 | amount: "", |
| 202 | contractAddress: contract, |
| 203 | tokenDecimals: 6, |
| 204 | rawTokenAmount: "1000000", |
| 205 | ); |
| 206 | |
| 207 | expect(uri.toString(), "ethereum:$contract@1/transfer?address=$recipient&uint256=1000000"); |
| 208 | }); |
| 209 | |
| 210 | test("parses a scientific notation value", () { |
| 211 | final parsed = ERC681URI.fromUri( |
| 212 | Uri.parse("ethereum:$recipient@1?value=2.014e18"), |
| 213 | ); |
| 214 | |
| 215 | expect(parsed.amount, "2.014"); |
| 216 | }); |
| 217 | |
| 218 | test("parses a Cake style amount param", () { |
| 219 | final parsed = ERC681URI.fromUri( |
| 220 | Uri.parse("ethereum:$recipient?amount=1.5"), |
| 221 | ); |
| 222 | |
| 223 | expect(parsed.address, recipient); |
| 224 | expect(parsed.amount, "1.5"); |
| 225 | expect(parsed.chainId, 1); |
| 226 | }); |
| 227 | |
| 228 | test("defaults to mainnet when the chainId is absent", () { |
| 229 | final parsed = ERC681URI.fromUri(Uri.parse("ethereum:$recipient")); |
| 230 | |
| 231 | expect(parsed.address, recipient); |
| 232 | expect(parsed.contractAddress, null); |
| 233 | expect(parsed.chainId, 1); |
| 234 | expect(parsed.amount, ""); |
| 235 | }); |
| 236 | }); |
| 237 | } |