Skip to content
Merged
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
28 changes: 28 additions & 0 deletions scripts/oracle-setup/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const endpoint = "";
export const priceFeed = ""
export const nodes = [
{
name: "Alice",
address: "5vJz7BmhVnVb6MK2P5d7KHYfR3QvWc58REf59G6tWH5UrFHD",
publicKey:
"0x4cc5f1b01d843ede8cca27dd4b9be4408ca9459aca36d2240666a2479a900a1d",
mnemonic:
"recall inhale trick wasp core hat problem season february buzz juice cloth",
},
{
name: "Bob",
address: "5uopFp298TRfWvLp9e3ad12Q1G2geqAr9zzGjBiZkGZbMid8",
publicKey:
"0x3686aa3aa5161a6b8e2316b24b3d521519df8f00f9c013b31a8290446a901606",
mnemonic:
"toddler weather gas game rotate idea mule liberty identify receive world cream",
},
{
name: "Charlie",
address: "5z7CdwanLZss4sijCMqupE8ECiksoFw22C683WaAgBvNgc8N",
publicKey:
"0xf4ba1d1a7dadcdbc2e836953aac3c86e0de7d736776c586b79ea21e76c73a502",
mnemonic:
"goddess ridge radio field syrup noodle indoor section lounge chase crime recall",
},
];
63 changes: 63 additions & 0 deletions scripts/oracle-setup/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ApiPromise, SubmittableResult, WsProvider } from "@polkadot/api";
import { AddressOrPair, SubmittableExtrinsic } from "@polkadot/api/types";
import { Keyring } from "@polkadot/keyring";

export const getApi = async (nodeName: string, endpoint?: string): Promise<ApiPromise> => {
const wsProvider = endpoint
? new WsProvider(endpoint + nodeName)
: new WsProvider("ws://127.0.0.1:9944");

const api = await ApiPromise.create({ provider: wsProvider });
await api.isReady;
return api;
};


export const getKeypair = (mneumonic: string): AddressOrPair => {
try {
const keyring = new Keyring({ type: "sr25519" });
return keyring.addFromUri("//" + mneumonic);
} catch (e: any) {
console.log("error setting up keypair");
throw new Error(e.message);
}
};

/**
* Signs and sends the given `call` from `sender` and waits for the transaction to be included in a block.
* @param api api object
* @param call a call that can be submitted to the chain
* @param sender the sender of the transaction
*/
export const sendAndWait = (
api: ApiPromise,
call: SubmittableExtrinsic<"promise">,
sender: AddressOrPair
): Promise<undefined> => {
return new Promise<undefined>((resolve, reject) => {
call
.signAndSend(sender, (res: SubmittableResult) => {
const { dispatchError, status } = res;

if (dispatchError) {
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(dispatchError.asModule);
const { name, section } = decoded;

reject(Error(dispatchError.toString()));
} else {
reject(Error(dispatchError.toString()));
}
}

if (status.isInBlock || status.isFinalized) {
resolve(undefined);
}
})
.catch((e) => {
reject(Error(e.message));
});
});
}

107 changes: 107 additions & 0 deletions scripts/oracle-setup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { ApiPromise } from "@polkadot/api";
import { stringToHex } from "@polkadot/util/string";
import { getApi, sendAndWait, getKeypair } from "./helpers";
import { endpoint, nodes, priceFeed } from "./config";
import { AddressOrPair } from "@polkadot/api/types";

// Inserts Keys
const insertKeys = async (api: ApiPromise, seed: string, publicKey: string) => {
const insert = await api.rpc.author.insertKey("orac", seed, publicKey);
console.log(" Insert Keys ");
console.log(` ${publicKey}`);
console.log(" Successful");
};

// Bonds the controller account
const bond = async (
api: ApiPromise,
address: string,
keyring: AddressOrPair
) => {
const bondTx = api.tx.oracle.setSigner(address);
const message = await sendAndWait(api, bondTx, keyring);
console.log({ message });
};

const setURL = async (api: ApiPromise) => {
const url = priceFeed;
const key = "0x6f63772d75726c";
const value = stringToHex(url);
await api.rpc.offchain.localStorageSet("PERSISTENT", key, value);
console.log(" Set Feed " + ` ${url}` + " Successful");
};

// Registers an asset ID with a threshold from `sudo` origin
const addAsset = async (api: ApiPromise, keyring: AddressOrPair) => {
const asset = 0;
const threshold = 10;
const minAnswers = 2;
const maxAnswers = 3;
const blockInterval = 10;
const reward = 10;
const slash = 10;

const tx = api.tx.oracle.addAssetAndInfo(
asset,
threshold,
minAnswers,
maxAnswers,
blockInterval,
reward,
slash
);
const su = api.tx.sudo.sudo(tx);
const message = await sendAndWait(api, su, keyring);

console.log({ message });
console.log(
" Register Asset " +
`ID: ${asset} ` +
"with threshold" +
`${threshold}% ` +
" Successful"
);
};

// Sends funds to the given account
const sendBalance = async (
api: ApiPromise,
keyring: AddressOrPair,
destinationAddress: string,
amount: string
) => {
const tx = await api.tx.assets.transfer(
"1",
destinationAddress,
amount,
false
);
const message = await sendAndWait(api, tx, keyring);
console.log({ message });
};

const main = async () => {
const api = await getApi("alice", endpoint);
const keyring = await getKeypair("Alice");
await addAsset(api, keyring);
await setURL(api);
await api.disconnect();

for (let i = 0; i <= nodes.length; i++) {
const api = await getApi(nodes[i].name.toLocaleLowerCase(), endpoint);
const keyring = await getKeypair(nodes[i].name);
await insertKeys(api, nodes[i].mnemonic, nodes[i].publicKey);
try {
await bond(api, nodes[i].address, keyring);
} catch (e) {
console.log("error with bond, could be already bonded", nodes[i].address);
}
console.log("bond done");
await sendBalance(api, keyring, nodes[i].address, "1000000000000");
console.log("balance issue");
await api.disconnect();
console.log({ i });
}
};

main();
20 changes: 20 additions & 0 deletions scripts/oracle-setup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "oraclesetup",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@polkadot/api": "^7.3.1",
"@polkadot/keyring": "^8.3.1",
"@polkadot/util": "^8.3.1",
"@polkadot/util-crypto": "^8.3.1",
"@types/chalk": "^2.2.0",
"chalk": "^5.0.0"
}
}
Loading