Skip to content

Commit 17ac00f

Browse files
author
justin j. moses
authored
Fixing caches of Binary Option Markets (Synthetixio#1146)
1 parent b36f3ca commit 17ac00f

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

publish/src/commands/deploy.js

+126-1
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,7 @@ const deploy = async ({
12181218
const poolFee = w3utils.toWei('0.008'); // 0.8% of the market's value goes to the pool in the end.
12191219
const creatorFee = w3utils.toWei('0.002'); // 0.2% of the market's value goes to the creator.
12201220
const refundFee = w3utils.toWei('0.05'); // 5% of a bid stays in the pot if it is refunded.
1221-
await deployer.deployContract({
1221+
const binaryOptionMarketManager = await deployer.deployContract({
12221222
name: 'BinaryOptionMarketManager',
12231223
args: [
12241224
account,
@@ -1567,6 +1567,131 @@ const deploy = async ({
15671567
}
15681568
}
15691569

1570+
// Now do binary option market cache rebuilding
1571+
if (binaryOptionMarketManager) {
1572+
console.log(gray('Checking all binary option markets have rebuilt caches'));
1573+
let binaryOptionMarkets = [];
1574+
// now grab all possible binary option markets to rebuild caches as well
1575+
const binaryOptionsFetchPageSize = 100;
1576+
for (const marketType of ['Active', 'Matured']) {
1577+
const numBinaryOptionMarkets = Number(
1578+
await binaryOptionMarketManager.methods[`num${marketType}Markets`]().call()
1579+
);
1580+
console.log(
1581+
gray('Found'),
1582+
yellow(numBinaryOptionMarkets),
1583+
gray(marketType, 'binary option markets')
1584+
);
1585+
1586+
if (numBinaryOptionMarkets > binaryOptionsFetchPageSize) {
1587+
console.log(
1588+
redBright(
1589+
'⚠⚠⚠ Warning: cannot fetch all',
1590+
marketType,
1591+
'binary option markets as there are',
1592+
numBinaryOptionMarkets,
1593+
'which is more than page size of',
1594+
binaryOptionsFetchPageSize
1595+
)
1596+
);
1597+
} else {
1598+
// fetch the list of markets
1599+
const marketAddresses = await binaryOptionMarketManager.methods[
1600+
`${marketType.toLowerCase()}Markets`
1601+
](0, binaryOptionsFetchPageSize).call();
1602+
1603+
// wrap them in a contract via the deployer
1604+
const markets = marketAddresses.map(
1605+
binaryOptionMarket =>
1606+
new deployer.web3.eth.Contract(compiled['BinaryOptionMarket'].abi, binaryOptionMarket)
1607+
);
1608+
1609+
binaryOptionMarkets = binaryOptionMarkets.concat(markets);
1610+
}
1611+
}
1612+
1613+
// now figure out which binary option markets need their caches rebuilt
1614+
const binaryOptionMarketsToRebuildCacheOn = [];
1615+
for (const market of binaryOptionMarkets) {
1616+
try {
1617+
const isCached = await market.methods.isResolverCached().call();
1618+
if (!isCached) {
1619+
binaryOptionMarketsToRebuildCacheOn.push(addressOf(market));
1620+
}
1621+
console.log(
1622+
gray('Binary option market'),
1623+
yellow(addressOf(market)),
1624+
gray('is newer and cache status'),
1625+
yellow(isCached)
1626+
);
1627+
} catch (err) {
1628+
// the challenge being that some used an older MixinResolver API
1629+
const oldBinaryOptionMarketABI = [
1630+
{
1631+
constant: true,
1632+
inputs: [
1633+
{
1634+
internalType: 'contract AddressResolver',
1635+
name: '_resolver',
1636+
type: 'address',
1637+
},
1638+
],
1639+
name: 'isResolverCached',
1640+
outputs: [
1641+
{
1642+
internalType: 'bool',
1643+
name: '',
1644+
type: 'bool',
1645+
},
1646+
],
1647+
payable: false,
1648+
stateMutability: 'view',
1649+
type: 'function',
1650+
signature: '0x631e1444',
1651+
},
1652+
];
1653+
1654+
const oldBinaryOptionMarket = new deployer.web3.eth.Contract(
1655+
oldBinaryOptionMarketABI,
1656+
addressOf(market)
1657+
);
1658+
1659+
const isCached = await oldBinaryOptionMarket.methods
1660+
.isResolverCached(addressOf(readProxyForResolver))
1661+
.call();
1662+
if (!isCached) {
1663+
binaryOptionMarketsToRebuildCacheOn.push(addressOf(market));
1664+
}
1665+
1666+
console.log(
1667+
gray('Binary option market'),
1668+
yellow(addressOf(market)),
1669+
gray('is older and cache status'),
1670+
yellow(isCached)
1671+
);
1672+
}
1673+
}
1674+
1675+
console.log(
1676+
gray('In total'),
1677+
yellow(binaryOptionMarketsToRebuildCacheOn.length),
1678+
gray('binary option markets need their caches rebuilt')
1679+
);
1680+
1681+
const addressesChunkSize = useOvm ? 7 : 20;
1682+
for (let i = 0; i < binaryOptionMarketsToRebuildCacheOn.length; i += addressesChunkSize) {
1683+
const chunk = binaryOptionMarketsToRebuildCacheOn.slice(i, i + addressesChunkSize);
1684+
await runStep({
1685+
gasLimit: useOvm ? OVM_MAX_GAS_LIMIT : 7e6,
1686+
contract: `BinaryOptionMarketManager`,
1687+
target: binaryOptionMarketManager,
1688+
publiclyCallable: true, // does not require owner
1689+
write: 'rebuildMarketCaches',
1690+
writeArg: [chunk],
1691+
});
1692+
}
1693+
}
1694+
15701695
// Now perform a sync of legacy contracts that have not been replaced in Shaula (v2.35.x)
15711696
// EtherCollateral, EtherCollateralsUSD
15721697
console.log(gray('Checking all legacy contracts with setResolverAndSyncCache() are rebuilt...'));

0 commit comments

Comments
 (0)