Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Last active May 8, 2020 01:16
Show Gist options
  • Save PatrickAlphaC/e061fc1037cf0692e7236e29af56df51 to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/e061fc1037cf0692e7236e29af56df51 to your computer and use it in GitHub Desktop.
Start using Alpha Vantage data in your smart contracts
// For documentation, check out:
// https://alphavantage.co
// More Chainlink documentation on solidity
// https://github.com/alphavantage/alpha-vantage-chainlink
pragma solidity >= 0.4.20;
// MAKE SURE TO USE A 0.4.24 COMPILER IN REMIX
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/ChainlinkClient.sol";
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/vendor/Ownable.sol";
contract AlphaVantageSimple is ChainlinkClient, Ownable {
// This is the LINK payment, currently set to 1 LINK
// Get free testnet LINK here: https://ropsten.chain.link/
// Get free testnet ETH here: https://faucet.ropsten.be/
uint256 constant private ORACLE_PAYMENT = 1 * LINK;
uint256 public ticker_price;
// The addresses of the Alpha Vantage nodes
address ALPHA_VANTAGE_ADDRESS_ROPSTEN = 0xB36d3709e22F7c708348E225b20b13eA546E6D9c;
address ALPHA_VANTAGE_ADDRESS_MAINNET = 0x72f3dFf4CD17816604dd2df6C2741e739484CA62;
// The address of the jobs for ropsten(testnet) and mainnet.
// These return an unsigned int
string constant private UINT_TICKER_JOB_ROPSTEN = "f9528decb5c64044b6b4de54ca7ea63e";
string constant private UINT_TICKER_JOB_MAINNET = "1169fe8eb7dd40b683bf33b09ab3a4ee";
// Blockchain can't understand decimals, so we multiply by a certain number to get all numbers as integers
int256 constant private TIMES = 10000;
// The main funciton that happens when you request data
// onlyOwner means only the owner of the ETH wallet can call this function
function requestTickerPrice(string _ticker, string _function) public onlyOwner
{
// We initialize the request, and begin building it
Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(UINT_TICKER_JOB_ROPSTEN), this, this.fulfillTickerPrice.selector);
// We add the parameters from the API
req.add("function", _function);
req.add("symbol", _ticker);
req.addInt("times", 10000);
// This is the location in the JSON return to get
string[] memory copyPath = new string[](2);
copyPath[0] = "Global Quote";
copyPath[1] = "05. price";
req.addStringArray("copyPath", copyPath);
// Then we make the request
sendChainlinkRequestTo(ALPHA_VANTAGE_ADDRESS_ROPSTEN, req, ORACLE_PAYMENT);
}
// Once the data comes back, we allow the user to access the data with this function
function fulfillTickerPrice(bytes32 _requestId, uint256 _price)
public
recordChainlinkFulfillment(_requestId)
{
emit RequestTickerPriceFulfilled(_requestId, _price);
ticker_price = _price;
}
// Understands when the API call is completed
event RequestTickerPriceFulfilled(
bytes32 indexed requestId,
uint256 indexed price
);
// Additional helper functions
function getChainlinkToken() public view returns (address) {
return chainlinkTokenAddress();
}
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
}
constructor() public Ownable() {
setPublicChainlinkToken();
}
function stringToBytes32(string memory source) private pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly { // solhint-disable-line no-inline-assembly
result := mload(add(source, 32))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment