Skip to content

Commit

Permalink
use fullnode and gateway service (MystenLabs#2697)
Browse files Browse the repository at this point in the history
* use fullnode and gateway service

* update

* use devnet as default

* remove default rpc endpoint

* .env.default update and lint fix
  • Loading branch information
Jibz1 authored Jun 24, 2022
1 parent bfd051d commit 36d3c32
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
3 changes: 3 additions & 0 deletions wallet/configs/environment/.env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

API_ENV=staging
API_ENDPOINT_LOCAL=http://127.0.0.1:5001/
API_ENDPOINT_LOCAL_FULLNODE=http://127.0.0.1:9000/
API_ENDPOINT_DEV_NET=https://gateway.devnet.sui.io/
API_ENDPOINT_DEV_NET_FULLNODE=https://fullnode.devnet.sui.io/
API_ENDPOINT_STAGING=https://gateway.staging.sui.io/
API_ENDPOINT_STAGING_FULLNODE=https://fullnode.staging.sui.io/
EXPLORER_URL_LOCAL=http://localhost:8080/
EXPLORER_URL_DEV_NET=https://explorer.devnet.sui.io/
EXPLORER_URL_STAGING=https://explorer.staging.sui.io/
4 changes: 2 additions & 2 deletions wallet/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 36 additions & 8 deletions wallet/src/ui/app/ApiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,30 @@ type EnvInfo = {
name: string;
color: string;
};

type ApiEndpoints = {
gateway: string;
fullNode: string;
};
export const API_ENV_TO_INFO: Record<API_ENV, EnvInfo> = {
[API_ENV.local]: { name: 'Local', color: '#9064ff' },
[API_ENV.devNet]: { name: 'DevNet', color: '#29b6af' },
[API_ENV.staging]: { name: 'Staging', color: '#ff4a8d' },
};

export const ENV_TO_API: Record<API_ENV, string | undefined> = {
[API_ENV.local]: process.env.API_ENDPOINT_LOCAL,
[API_ENV.devNet]: process.env.API_ENDPOINT_DEV_NET,
[API_ENV.staging]: process.env.API_ENDPOINT_STAGING,
export const ENV_TO_API: Record<API_ENV, ApiEndpoints> = {
[API_ENV.local]: {
gateway: process.env.API_ENDPOINT_LOCAL || '',
fullNode: process.env.API_ENDPOINT_LOCAL_FULLNODE || '',
},
[API_ENV.devNet]: {
gateway: process.env.API_ENDPOINT_DEV_NET || '',
fullNode: process.env.API_ENDPOINT_DEV_NET_FULLNODE || '',
},
[API_ENV.staging]: {
gateway: process.env.API_ENDPOINT_STAGING || '',
fullNode: process.env.API_ENDPOINT_STAGING_FULLNODE || '',
},
};

function getDefaultApiEnv() {
Expand All @@ -37,7 +51,11 @@ function getDefaultApiEnv() {

function getDefaultAPI(env: API_ENV) {
const apiEndpoint = ENV_TO_API[env];
if (!apiEndpoint) {
if (
!apiEndpoint ||
apiEndpoint.gateway === '' ||
apiEndpoint.fullNode === ''
) {
throw new Error(`API endpoint not found for API_ENV ${env}`);
}
return apiEndpoint;
Expand All @@ -48,18 +66,28 @@ export const DEFAULT_API_ENDPOINT = getDefaultAPI(DEFAULT_API_ENV);

export default class ApiProvider {
private _apiProvider: JsonRpcProvider;
private _apiFullNodeProvider: JsonRpcProvider;
private _signer: RawSigner | null = null;

constructor() {
this._apiProvider = new JsonRpcProvider(DEFAULT_API_ENDPOINT);
this._apiProvider = new JsonRpcProvider(DEFAULT_API_ENDPOINT.gateway);
this._apiFullNodeProvider = new JsonRpcProvider(
DEFAULT_API_ENDPOINT.fullNode
);
}

public setNewJsonRpcProvider(apiEnv: API_ENV) {
this._apiProvider = new JsonRpcProvider(getDefaultAPI(apiEnv));
this._apiProvider = new JsonRpcProvider(getDefaultAPI(apiEnv).gateway);
this._apiFullNodeProvider = new JsonRpcProvider(
getDefaultAPI(apiEnv).fullNode
);
}

public get instance() {
return this._apiProvider;
return {
gateway: this._apiProvider,
fullNode: this._apiFullNodeProvider,
};
}

public getSignerInstance(keypair: Ed25519Keypair): RawSigner {
Expand Down
7 changes: 3 additions & 4 deletions wallet/src/ui/app/redux/slices/sui-objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ export const fetchAllOwnedObjects = createAsyncThunk<
const address = getState().account.address;
const allSuiObjects: SuiObject[] = [];
if (address) {
const allObjectRefs = await api.instance.getObjectsOwnedByAddress(
`${address}`
);
const allObjectRefs =
await api.instance.gateway.getObjectsOwnedByAddress(`${address}`);
const objectIDs = allObjectRefs.map((anObj) => anObj.objectId);
const allObjRes = await api.instance.getObjectBatch(objectIDs);
const allObjRes = await api.instance.gateway.getObjectBatch(objectIDs);
for (const objRes of allObjRes) {
const suiObj = getObjectExistsResponse(objRes);
if (suiObj) {
Expand Down
4 changes: 2 additions & 2 deletions wallet/src/ui/app/redux/slices/txresults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ export const getTransactionsByAddress = createAsyncThunk<
}
// Get all transactions txId for address
const transactions: GetTxnDigestsResponse = (
await api.instance.getTransactionsForAddress(address)
await api.instance.fullNode.getTransactionsForAddress(address)
).filter((tx) => tx);

if (!transactions || !transactions.length) {
return [];
}
//getTransactionWithEffectsBatch
const resp = await api.instance
const resp = await api.instance.fullNode
.getTransactionWithEffectsBatch(deduplicate(transactions))
.then((txEffs: TransactionEffectsResponse[]) => {
return (
Expand Down

0 comments on commit 36d3c32

Please sign in to comment.