Skip to content

Commit

Permalink
extra rpc to search through on failure for ibc transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
dsudit01 committed Mar 12, 2023
1 parent a112dc3 commit 8d44c40
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/pages/bridge/BridgeOut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ const BridgeOut = (props: BridgeOutProps) => {
bridgeOutNetwork.latestBlockEndpoint,
ibcFee,
chain,
memo
memo,
bridgeOutNetwork.extraEndpoints
),
BridgeTransactionType.BRIDGE_OUT,
bridgeTxStore.setTransactionStatus,
Expand Down
2 changes: 2 additions & 0 deletions src/pages/bridge/config/bridgeOutNetworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface BridgeOutNetworkInfo {
channel: string;
endpoint: string;
latestBlockEndpoint?: string;
extraEndpoints?: string[];
addressBeginning: string;
checkAddress: (address: string) => boolean;
}
Expand Down Expand Up @@ -48,6 +49,7 @@ export const ALL_BRIDGE_OUT_NETWORKS: BridgeOutNetworkData = {
],
channel: "channel-0",
endpoint: "https://gravitychain.io:1317",
extraEndpoints: ["https://gravity-api.polkachu.com"],
addressBeginning: "gravity",
checkAddress: function (address) {
return addressCheck(address, this.addressBeginning);
Expand Down
103 changes: 77 additions & 26 deletions src/pages/bridge/utils/IBC/IBCTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export async function txIBCTransfer(
latestBlockEndpoint: any,
fee: any,
chain: any,
memo: any
memo: any,
extraEndpoints?: string[]
) {
// check metamask
//@ts-ignore
Expand All @@ -33,10 +34,10 @@ export async function txIBCTransfer(
const account = accounts[0];
const senderObj = await getSenderObj(account, nodeAddressIP);
//get revision number/height for construction of the timeout-height object (determines the last update of the counter-party IBC client)
const ibcData = await getIBCData(counterPartyChain);

const ibcData = await getIBCData(counterPartyChain, extraEndpoints);
let timeoutTimestamp = await getBlockTimestamp(
counterPartyChain,
extraEndpoints,
latestBlockEndpoint
);

Expand Down Expand Up @@ -70,37 +71,87 @@ export async function txIBCTransfer(
);
}

export async function getIBCData(nodeAddress: string) {
const resp = await fetch(nodeAddress + generateEndpointIBCChannels(), {
method: "GET",
headers: {
Accept: "application/json",
},
}).catch((error) => console.log("getIBCData::error: ", error));
//@ts-ignore
return resp.json();
async function tryEndpoint(endpoint: string) {
try {
const res = await fetch(endpoint, {
method: "GET",
headers: {
Accept: "application/json",
},
});
if (res.ok) {
return [await res.json(), true];
} else {
return [null, false];
}
} catch (err) {
console.error(err);
return [null, false];
}
}
async function tryMultipleEndpoints(
allEndpoints: string[],
endpointAddon: string
) {
let endpointUp = false;
let data = null;
let endpointAttempt = 0;
while (!endpointUp && endpointAttempt < allEndpoints.length) {
const [endpointData, okay] = await tryEndpoint(
allEndpoints[endpointAttempt] + endpointAddon
);
endpointAttempt++;
endpointUp = okay;
data = endpointData;
}
return data;
}

export async function getIBCData(
nodeAddress: string,
extraEndpoints?: string[]
) {
const allEndpoints = extraEndpoints
? [nodeAddress, ...extraEndpoints]
: [nodeAddress];
return await tryMultipleEndpoints(
allEndpoints,
generateEndpointIBCChannels()
);
}

/**
* @param {string} nodeAddress rest endpoint to request counter-party chain timestamp
*/
export async function getBlockTimestamp(
nodeAddress: string,
extraEndpoints?: string[],
latestBlockEndpoint?: string
) {
const urlEnding = latestBlockEndpoint ?? "";
const resp = await fetch(nodeAddress + urlEnding + "/blocks/latest", {
method: "GET",
headers: {
Accept: "application/json",
},
}).catch((error) => console.log("getBlockTimestamp::error: ", error));
//@ts-ignore
const obj = await resp.json();
// get iso formatted time stamp from latest block
const ts = obj["block"]["header"]["time"];
// parse string into microseconds UTC
const ms = Date.parse(ts);
// return as nano-seconds
return Number(ms * 1e7 + 600 * 1e9).toString();
const allEndpoints = extraEndpoints
? [nodeAddress, ...extraEndpoints]
: [nodeAddress];
const data = await tryMultipleEndpoints(
allEndpoints,
urlEnding + "/blocks/latest"
);
// const resp = await fetch(nodeAddress + urlEnding + "/blocks/latest", {
// method: "GET",
// headers: {
// Accept: "application/json",
// },
// }).catch((error) => console.log("getBlockTimestamp::error: ", error));
// //@ts-ignore
// const obj = await resp.json();

if (data) {
// get iso formatted time stamp from latest block
const ts = data["block"]["header"]["time"];
// parse string into microseconds UTC
const ms = Date.parse(ts);
// return as nano-seconds
return Number(ms * 1e7 + 600 * 1e9).toString();
}
throw Error("no timestamp");
}
3 changes: 2 additions & 1 deletion src/pages/bridge/walkthrough/managers/BridgeOutManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ export const BridgeOutManager = (props: BridgeOutManagerProps) => {
props.currentBridgeOutNetwork.latestBlockEndpoint,
ibcFee,
chain,
memo
memo,
props.currentBridgeOutNetwork.extraEndpoints
),
BridgeTransactionType.BRIDGE_OUT,
props.setCosmosTxStatus,
Expand Down

0 comments on commit 8d44c40

Please sign in to comment.