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 1 commit
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
Prev Previous commit
test
  • Loading branch information
pr0toshi committed Sep 5, 2023
commit 01903273e0fa4c5243a08b5978e72bf2bcef9a1f
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();}
}
}
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;
}
}
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, []);
};
});