Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivRaiGithub committed Jul 30, 2024
1 parent 217ec9c commit 9cb1f85
Show file tree
Hide file tree
Showing 17 changed files with 5,728 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ docs/

# Dotenv file
.env
node_modules
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/chainlink-brownie-contracts"]
path = lib/chainlink-brownie-contracts
url = https://github.com/smartcontractkit/chainlink-brownie-contracts
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# About the project

This is a project based on 'Tokenization of RWA' by Patrick Collins. Video link : https://www.youtube.com/watch?v=KNUchSEtQV0
It works with chainlink functions and alpaca in order to allow a user to mint and redeem dTSLA tokens which are the tokens based on Tesla stocks, which are available in user's alpaca account.



## env
```
export ALPACA_API_KEY=
export ALPACA_SECRET_KEY=
```


## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**
Expand Down
8 changes: 8 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@
src = "src"
out = "out"
libs = ["lib"]
remappings=[
'@chainlink/contracts/=lib/chainlink-brownie-contracts/contracts/',
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",

]
fs_permissions = [{access = "read", path = "./"}]

[[contracts]]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
17 changes: 17 additions & 0 deletions functions/configs/alpacaMintConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');
const {Location, ReturnType, CodeLanguage} = require('@chainlink/functions-toolkit');

const requestConfig = {
source : fs.readFileSync('./functions/sources/alpacaBalance.js').toString(),
codeLocation: Location.Inline,
secrets:{alpacaKey: process.env.ALPACA_KEY, alpacaSecret: process.env.ALPACA_SECRET},
secretsLocation: Location.DONHosted,
arges: [],
codeLanguage: CodeLanguage.JavaScript,
expectedReturnType: ReturnType.uint256
}

module.exports = requestConfig;



16 changes: 16 additions & 0 deletions functions/simulators/alpacaMintSimulator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const requestConfig = require("../configs/alpacaMintConfig.js")
const {simulateScript, decodeResult} = require("@chainlink/functions-toolkit")

async function main(){
const{responseByetsHexString, errorString, capturedTerminalOutput} = await simulateScript(requestConfig);
if(responseByetsHexString){
console.log("Response: ", decodeResult(responseByetsHexString, requestConfig.expectedReturnType))
}
if(errorString){
console.error("Error: ", errorString)
}
}
main().catch((error)=>{
console.error(error)
process.exit(1)
})
21 changes: 21 additions & 0 deletions functions/sources/alpacaBalance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
if(
secrets.alpacaKey == "" ||
secrets.alpacaSecret == ""
){
throw Error("Alpaca API Key and Secret are required");
}

const alpacaRequest = Functions.Http.makeRequest({
url : "https://paper-api.alpaca.markets/v2/account",
headers : {
accept : "application/json",
"APCA-API-KEY-ID" : secrets.alpacaKey,
"APCA-API-SECRET-KEY" : secrets.alpacaSecret
}
});

const [response] = await Promise.all({alpacaRequest});
const portfolioBalance = response.data.portfolio_value;
console.log("Alpaca portfolio balance: ", portfolioBalance);

return Functions.encodeUint256(Math.round(portfolioBalance * 100));
47 changes: 47 additions & 0 deletions functions/uploadSecrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const{
SecretsManager
} = require("@chainlink/functions-toolkit");
const ethers = require("ethers");


async function uploadSecrets(){
const routerAddress="0xb83E47C2bC239B3bf370bc41e1459A34b41238D0";
const donId = "fun-ethereum-sepolia-1";
const gatewayUrls = ["https://02.functions-gateway.testnet.chain.link/", "https://01.functions-gateway.testnet.chain.link/"];

const privateKey = process.env.PRIVATE_KEY;
const rpcUrl = process.env.SEPOLIA_RPC_URL;
const secrets = {alpacaKey: process.env.ALPACA_KEY, alpacaSecret: process.env.ALPACA_SECRET}

const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const wallet = new ethers.Wallet(privateKey);
const signer = wallet.connect(provider);

const secretsManager = new SecretsManager(signer, routerAddress, donId);
await secretsManager.initialize();

const encryptedSecrets = await secretsManager.encryptSecrets(secrets);
const slotIdNumber = 0;
const expirationTineMinutes=1440;
const uploadResult = await secretsManager.uploadEncryptedSecretsToDON({
encryptedSecretsHexstring: encryptedSecrets.encryptedSecrets,
gatewayUrls: gatewayUrls,
slotId: slotIdNumber,
minutesUntilExpiration: expirationTineMinutes

})

if(!uploadResult.success){
throw new Error("Failed to upload secrets to DON", uploadResult.errorMessage)
}

console.log("Secrets uploaded successfully", uploadResult);
const donHostedSecretsVersion = parseInt(uploadResult.version);
console.log("Secrets version", donHostedSecretsVersion);

}

uploadSecrets().catch((error)=>{
console.error(error)
process.exit(1)
})
1 change: 1 addition & 0 deletions lib/chainlink-brownie-contracts
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
Loading

0 comments on commit 9cb1f85

Please sign in to comment.