diff --git a/web3.js/bin/localnet.sh b/web3.js/bin/localnet.sh index 8085e7e6ab..a06d1c3ba5 100755 --- a/web3.js/bin/localnet.sh +++ b/web3.js/bin/localnet.sh @@ -10,7 +10,7 @@ channel=$( } catch (err) { p = require("../package.json"); } - p["solana-channel"] + p["testnetDefaultChannel"] ' ) diff --git a/web3.js/module.flow.js b/web3.js/module.flow.js index 6fda94fbf9..c000b541d1 100644 --- a/web3.js/module.flow.js +++ b/web3.js/module.flow.js @@ -262,4 +262,7 @@ declare module '@solana/web3.js' { connection: Connection, wireTransaction: Buffer, ): Promise; + + // === src/util/testnet.js === + declare export function testnetChannelEndpoint(channel?: string): string; } diff --git a/web3.js/package.json b/web3.js/package.json index ed3aa3bab7..c684b68c57 100644 --- a/web3.js/package.json +++ b/web3.js/package.json @@ -24,7 +24,7 @@ "bin": { "solana-localnet": "bin/localnet.sh" }, - "solana-channel": "beta", + "testnetDefaultChannel": "beta", "files": [ "/bin", "/doc", @@ -37,7 +37,7 @@ "scripts": { "bpf-sdk:install": "set -ex; npm run bpf-sdk:fetch; npm run bpf-sdk:extract", "bpf-sdk:extract": "test -d bpf-sdk || tar jxvf bpf-sdk.tar.bz2", - "bpf-sdk:fetch": "test -f bpf-sdk.tar.bz2 || curl http://solana-sdk.s3.amazonaws.com/$npm_package_solana_channel/bpf-sdk.tar.bz2 -o bpf-sdk.tar.bz2", + "bpf-sdk:fetch": "test -f bpf-sdk.tar.bz2 || curl http://solana-sdk.s3.amazonaws.com/$npm_package_testnetDefaultChannel/bpf-sdk.tar.bz2 -o bpf-sdk.tar.bz2", "bpf-sdk:remove-symlinks": "find bpf-sdk -type l -print -exec cp {} {}.tmp \\; -exec mv {}.tmp {} \\;", "build": "cross-env NODE_ENV=production rollup -c", "clean": "rimraf ./coverage ./lib", @@ -55,8 +55,8 @@ "lint:watch": "watch 'npm run lint:fix' . --wait=1 --ignoreDirectoryPattern=/doc/", "localnet:down": "bin/localnet.sh down", "localnet:logs": "bin/localnet.sh logs -f", - "localnet:up": "bin/localnet.sh up $npm_package_solana_channel", - "localnet:update": "bin/localnet.sh update $npm_package_solana_channel", + "localnet:up": "bin/localnet.sh up $npm_package_testnetDefaultChannel", + "localnet:update": "bin/localnet.sh update $npm_package_testnetDefaultChannel", "ok": "set -ex; npm run lint; npm run flow; npm run test; npm run doc", "prepare": "set -ex; npm run clean; npm run bpf-sdk:install; npm run build; rm -rf bpf-sdk; npm rum bpf-sdk:extract; npm run bpf-sdk:remove-symlinks", "pretty": "prettier --write '{,{examples,src,test}/**/}*.js'", diff --git a/web3.js/src/index.js b/web3.js/src/index.js index 6c67c54334..dcf627ef3d 100644 --- a/web3.js/src/index.js +++ b/web3.js/src/index.js @@ -13,3 +13,4 @@ export {sendAndConfirmTransaction} from './util/send-and-confirm-transaction'; export { sendAndConfirmRawTransaction, } from './util/send-and-confirm-raw-transaction'; +export {testnetChannelEndpoint} from './util/testnet'; diff --git a/web3.js/src/transaction.js b/web3.js/src/transaction.js index 876fd7ca4a..9ac3353794 100644 --- a/web3.js/src/transaction.js +++ b/web3.js/src/transaction.js @@ -168,11 +168,13 @@ export class Transaction { programIds.push(programId); } - instruction.keys.map(key => key.toString()).forEach(key => { - if (!keys.includes(key)) { - keys.push(key); - } - }); + instruction.keys + .map(key => key.toString()) + .forEach(key => { + if (!keys.includes(key)) { + keys.push(key); + } + }); }); const instructions = this.instructions.map(instruction => { diff --git a/web3.js/src/util/testnet.js b/web3.js/src/util/testnet.js new file mode 100644 index 0000000000..f10c7ba991 --- /dev/null +++ b/web3.js/src/util/testnet.js @@ -0,0 +1,27 @@ +//@flow + +import {testnetDefaultChannel} from '../../package.json'; + +/** + * @private + */ +const endpoint = { + edge: 'https://api.edge.testnet.solana.com', + beta: 'https://api.beta.testnet.solana.com', + stable: 'https://api.testnet.solana.com', +}; + +/** + * Retrieves the RPC endpoint URL for the specified testnet release + * channel + */ +export function testnetChannelEndpoint(channel?: string): string { + if (!channel) { + return endpoint[testnetDefaultChannel]; + } + + if (endpoint[channel]) { + return endpoint[channel]; + } + throw new Error(`Unknown channel: ${channel}`); +} diff --git a/web3.js/test/testnet.test.js b/web3.js/test/testnet.test.js new file mode 100644 index 0000000000..4a7dc9c309 --- /dev/null +++ b/web3.js/test/testnet.test.js @@ -0,0 +1,18 @@ +// @flow +import {testnetChannelEndpoint} from '../src/util/testnet'; + +test('invalid', () => { + expect(() => { + testnetChannelEndpoint('abc123'); + }).toThrow(); +}); + +test('edge', () => { + expect(testnetChannelEndpoint('edge')).toEqual( + 'https://api.edge.testnet.solana.com', + ); +}); + +test('default', () => { + testnetChannelEndpoint(); // Should not throw +}); diff --git a/web3.js/test/url.js b/web3.js/test/url.js index a26789d4c4..59bef47d5d 100644 --- a/web3.js/test/url.js +++ b/web3.js/test/url.js @@ -6,8 +6,12 @@ export const url = 'http://localhost:8899/'; -//export const url = 'http://edge.testnet.solana.com:8899/'; -//export const url = 'http://beta.edge.testnet.solana.com:8899/'; +/* +export const url = 'http://edge.testnet.solana.com:8899/'; +export const url = 'http://beta.testnet.solana.com:8899/'; +export const url = 'http://testnet.solana.com:8899/'; -//export const url = 'http://testnet.solana.com:8899/'; -//export const url = 'https://api.testnet.solana.com/'; +export const url = 'https://api.edge.testnet.solana.com/'; +export const url = 'https://api.beta.testnet.solana.com/'; +export const url = 'https://api.testnet.solana.com/'; +*/