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

Scry Oracle Support #1809

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

/// @title Interface an aggregator needs to adhere.
interface IScryMetaMorph {

function getFeedPortal(
uint256 ID
)
external
view
returns (
uint256 value,
uint256 decimals,
string memory valStr,
bytes memory valBytes,
uint timestamp
);

}
25 changes: 25 additions & 0 deletions protocol/oracle-manager/contracts/mocks/MockScryMetamorph.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: SCRY
pragma solidity 0.8.6;

contract MockMetaMorph {
function getFeedPortal(
uint256 ID
)
external
view
returns (
uint256 value,
uint decimals,
string memory valStr,
bytes memory valBytes,
uint timestamp
)
{if (ID==0){
value=100*10*18;
decimals=18;
timestamp=block.timestamp();}
else{value=100*10*2;
decimals=2;
timestamp=block.timestamp();}
}
}
7 changes: 6 additions & 1 deletion protocol/oracle-manager/contracts/modules/NodeModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "../nodes/ReducerNode.sol";
import "../nodes/ExternalNode.sol";
import "../nodes/PythNode.sol";
import "../nodes/ChainlinkNode.sol";
import "../nodes/ScryMetaMorphNode.sol.sol";
import "../nodes/PriceDeviationCircuitBreakerNode.sol";
import "../nodes/StalenessCircuitBreakerNode.sol";
import "../nodes/UniswapNode.sol";
Expand Down Expand Up @@ -179,7 +180,9 @@ contract NodeModule is INodeModule {
} else if (nodeDefinition.nodeType == NodeDefinition.NodeType.CHAINLINK) {
return ChainlinkNode.process(nodeDefinition.parameters);
} else if (nodeDefinition.nodeType == NodeDefinition.NodeType.UNISWAP) {
return UniswapNode.process(nodeDefinition.parameters);
return UniswapNode.process(nodeDefinition.parameters);
else if (nodeDefinition.nodeType == NodeDefinition.NodeType.SCRY) {
return ScryMetaMorph.process(nodeDefinition.parameters);
} else if (nodeDefinition.nodeType == NodeDefinition.NodeType.PYTH) {
return PythNode.process(nodeDefinition.parameters);
} else if (
Expand Down Expand Up @@ -225,6 +228,8 @@ contract NodeModule is INodeModule {
return ChainlinkNode.isValid(nodeDefinition);
} else if (nodeDefinition.nodeType == NodeDefinition.NodeType.UNISWAP) {
return UniswapNode.isValid(nodeDefinition);
} else if (nodeDefinition.nodeType == NodeDefinition.NodeType.SCRY) {
return ScryMetaMorph.isValid(nodeDefinition);
} else if (nodeDefinition.nodeType == NodeDefinition.NodeType.PYTH) {
return PythNode.isValid(nodeDefinition);
} else if (
Expand Down
56 changes: 56 additions & 0 deletions protocol/oracle-manager/contracts/nodes/ScryMetaMorphNode.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.11 <0.9.0;

import "@synthetixio/core-contracts/contracts/utils/SafeCast.sol";
import "@synthetixio/core-contracts/contracts/utils/DecimalMath.sol";

import "../storage/NodeDefinition.sol";
import "../storage/NodeOutput.sol";
import "../interfaces/external/IScryMetaMorph.sol";

library ScryMetaMorphNode {
using SafeCastU256 for uint256;
using SafeCastI256 for int256;
using DecimalMath for int256;

uint256 public constant PRECISION = 18;

function process(
bytes memory parameters
) internal view returns (NodeOutput.Data memory nodeOutput) {
(address addrs, uint256 ID) = abi.decode(
parameters,
(address, uint256)
);
IScryMetaMorph metamorph = IScryMetaMorph(addrs);
(int256 price, uint256 decimals,,,uint256 timestamp) = metamorph.getFeedPortal(ID);

price = decimals > PRECISION
? price / 10 ** (decimals-PRECISION)
: price * 10 ** (PRECISION-decimals);

return NodeOutput.Data(price, timestamp, 0, 0);
}

function isValid(NodeDefinition.Data memory nodeDefinition) internal view returns (bool valid) {
// Must have no parents
if (nodeDefinition.parents.length > 0) {
return false;
}

// Must have correct length of parameters data
if (nodeDefinition.parameters.length != 64) {
return false;
}

(address addrs, uint256 ID) = abi.decode(
nodeDefinition.parameters,
(address, uint256)
);
IScryMetaMorph metamorph = IScryMetaMorph(addrs);
// Must successfully execute getFeedPortal, the custom portal enforces the users desired quorums among custom oracle sets and data already
(int256 price, uint256 decimals,,,uint256 timestamp) = metamorph.getFeedPortal(ID);

return true;
}
}
3 changes: 2 additions & 1 deletion protocol/oracle-manager/contracts/storage/NodeDefinition.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ library NodeDefinition {
PYTH,
PRICE_DEVIATION_CIRCUIT_BREAKER,
STALENESS_CIRCUIT_BREAKER,
CONSTANT
CONSTANT,
SCRY
}

struct Data {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default {
PRICE_DEVIATION_CIRCUIT_BREAKER: 6,
STALENESS_CIRCUIT_BREAKER: 7,
CONSTANT: 8,
SCRY:9
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import assertBn from '@synthetixio/core-utils/utils/assertions/assert-bignumber';
import { BigNumber, ethers, utils } from 'ethers';
import hre from 'hardhat';
import { bootstrap } from '../bootstrap';
import NodeTypes from '../mixins/Node.types';

describe('ScryMetaMorphNode', () => {
const { getSigners, getContract } = bootstrap();

let metamorph: ethers.Contract;

const abi = utils.defaultAbiCoder;

let owner: ethers.Signer;

let NodeModule: ethers.Contract;

before('prepare environment', async () => {
NodeModule = getContract('NodeModule');
});

before('identify owner', async () => {
[owner] = await getSigners();
});

before('deploy mock MM', async () => {
const factory = await hre.ethers.getContractFactory('MockMetaMorph');
metamorph = await factory.connect(owner).deploy();
});

describe('process()', () => {
describe('check value = 100', async () => {
it('returns latest price', async () => {
const encodedParams = abi.encode(
['address', 'uint256'],
[metamorph.address, 0]
);

const nodeId = await registerNode(encodedParams);
const [price] = await NodeModule.process(nodeId);
assertBn.equal(price, ethers.utils.parseUnits('100',18));
});
});

describe('when upscale is needed', async () => {
it('returns price with 18 decimals', async () => {
const encodedParams = abi.encode(
['address', 'uint256'],
[metamorph.address, 1]
);

const nodeId = await registerNode(encodedParams);
const [price] = await NodeModule.process(nodeId);
assertBn.equal(price, ethers.utils.parseUnits('100', 18));
});
});
});

const registerNode = async (params: string) => {
const tx = await NodeModule.registerNode(NodeTypes.SCRY, params, []);
await tx.wait();
return await NodeModule.getNodeId(NodeTypes.SCRY, params, []);
};
});