Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #48: predictoor.ai 5m feed is down often, bc pdr-websocket fails to renew subscription #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/datafeed/providerListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let latestEpoch = 0;
type TProviderListenerArgs = {
io: Server<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>;
contractAddresses: string[];
epochEmitterName: EEpochEmitterNames
epochEmitterName: EEpochEmitterNames;
};

export type TProviderListenerEmitData = Array<{
Expand Down Expand Up @@ -63,8 +63,7 @@ export const providerListener = async ({
const block = await provider.getBlock(currentBlock);
const currentTs = block.timestamp;

const SPE =
await predictoorContracts[0]?.getSecondsPerEpoch();
const SPE = await predictoorContracts[0]?.getSecondsPerEpoch();

const subscribedPredictoors = await checkAndSubscribe({
predictoorContracts,
Expand Down Expand Up @@ -137,6 +136,6 @@ export const providerListener = async ({
}));

predValDataHolder.setFixedMessage(epochEmitterName, result);
io.emit('newEpoch', result);
io.emit("newEpoch", result);
});
};
51 changes: 41 additions & 10 deletions src/utils/contracts/Predictoor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Predictoor {

const providerFeeToken = ethers.constants.AddressZero;
const providerFeeAmount = 0;
const providerValidUntil = 0;
const providerValidUntil = Math.floor(Date.now() / 1000 + 3600);
// Create message to sign
const message = ethers.utils.solidityKeccak256(
["bytes", "address", "address", "uint256", "uint256"],
Expand Down Expand Up @@ -110,6 +110,7 @@ class Predictoor {
// Get order parameters
async getOrderParams(user: ethers.Wallet) {
const providerFee = await this.getCalculatedProviderFee(user);
console.log("providerFee", providerFee);
return {
consumer: user.address,
serviceIndex: 0,
Expand All @@ -128,19 +129,30 @@ class Predictoor {
freParams: TFreParams
): Promise<BigNumber> {
const isBarge = process.env.ENVIRONMENT === "barge";
return isBarge
? (await networkProvider.getProvider().getBlock("latest")).gasLimit
: this.instance
.connect(user)
.estimateGas.buyFromFreAndOrder(orderParams, freParams);
const latestGasLimit = (
await networkProvider.getProvider().getBlock("latest")
).gasLimit;
if (isBarge) return latestGasLimit;

const gasLimitEstimate = await this.instance
.connect(user)
.estimateGas.buyFromFreAndOrder(orderParams, freParams);

return BigNumber.from(Math.max(gasLimitEstimate.mul(2).toNumber(), latestGasLimit.toNumber()));
}

// Buy from Fixed Rate Exchange (FRE) and order
async buyFromFreAndOrder(
user: ethers.Wallet,
exchangeId: string,
baseTokenAmount: string
): Promise<ethers.ContractReceipt | Error> {
): Promise<
| {
receipt: ethers.ContractReceipt;
confirmation: ethers.providers.TransactionReceipt;
}
| Error
> {
try {
console.log("buyFromFreAndOrder");
const orderParams = await this.getOrderParams(user);
Expand All @@ -159,17 +171,24 @@ class Predictoor {
orderParams,
freParams
);

const nonce = await this.provider.getTransactionCount(user.address);

// Execute transaction and wait for receipt
const tx = await this.instance
.connect(user)
.buyFromFreAndOrder(orderParams, freParams, {
gasLimit,
gasPrice,
gasPrice: gasPrice.mul(2),
});

const receipt = await tx.wait();

return receipt;
const confirmation = await this.provider.waitForTransaction(
receipt.transactionHash
);

return { confirmation, receipt };
} catch (e: any) {
console.error(e);
return e;
Expand Down Expand Up @@ -198,11 +217,23 @@ class Predictoor {
ethers.utils.formatEther(baseTokenAmount),
this.provider
);
return await this.buyFromFreAndOrder(
const result = await this.buyFromFreAndOrder(
user,
this.exchangeId?.toString(),
formattedBaseTokenAmount
);

if (!(result instanceof Error)) {
const receipt = result.receipt;
const confirmation = result.confirmation;

console.log("confirmation", confirmation);
console.log("result", receipt);
console.log("success");
return receipt;
} else {
throw result;
}
} catch (e: any) {
console.error(e);
return null;
Expand Down