|
| 1 | +const {ApiPromise, Keyring, WsProvider} = require('@polkadot/api'); |
| 2 | +const {cryptoWaitReady} = require('@polkadot/util-crypto'); |
| 3 | +const feedConfigs = require('./feeds.json'); |
| 4 | +const types = require('../substrate-node-example/types.json'); |
| 5 | + |
| 6 | + |
| 7 | +async function fundAccountIfNeeded(api, senderAccount, receiverAddress) { |
| 8 | + return new Promise(async (resolve) => { |
| 9 | + const balance = await api.query.system.account(receiverAddress); |
| 10 | + console.log(`Free balance of ${receiverAddress} is: ${balance.data.free}`); |
| 11 | + if (parseInt(balance.data.free) === 0) { |
| 12 | + await api.tx.balances.transfer(receiverAddress, 123456666000).signAndSend(senderAccount, async ({status}) => { |
| 13 | + if (status.isFinalized) { |
| 14 | + console.log(`Account ${receiverAddress} funded`); |
| 15 | + resolve(); |
| 16 | + } |
| 17 | + }); |
| 18 | + } else { |
| 19 | + resolve(); |
| 20 | + } |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +async function registerFeedCreatorIfNeeded(api, aliceAccount, operatorAccount) { |
| 25 | + console.log(`Registering feed creator ${operatorAccount.address}`); |
| 26 | + return new Promise(async (resolve) => { |
| 27 | + const feedCreator = await api.query.chainlinkFeed.feedCreators(operatorAccount.address); |
| 28 | + if (feedCreator.isNone) { |
| 29 | + await api.tx.chainlinkFeed.setFeedCreator(operatorAccount.address).signAndSend(aliceAccount, async ({status}) => { |
| 30 | + if (status.isFinalized) { |
| 31 | + console.log('Feed creator registered'); |
| 32 | + resolve(); |
| 33 | + } |
| 34 | + }); |
| 35 | + } else { |
| 36 | + console.log('Feed creator already registered'); |
| 37 | + resolve(); |
| 38 | + } |
| 39 | + }); |
| 40 | +} |
| 41 | +async function createFeed(api, sender) { |
| 42 | + console.log(`Creating feed with config: ${JSON.stringify(feedConfig, null, 4)}`); |
| 43 | + return new Promise(async (resolve) => { |
| 44 | + await api.tx.chainlinkFeed.createFeed(feedConfig.payment, feedConfig.timeout, feedConfig.submissionValueBounds, feedConfig.minSubmissions, feedConfig.decimals, feedConfig.description, feedConfig.restartDelay, feedConfig.oracles,feedConfig.pruningWindow,feedConfig.maxDebt).signAndSend(sender, ({ status, events }) => { |
| 45 | + if (status.isInBlock || status.isFinalized) { |
| 46 | + events |
| 47 | + // find/filter for failed events |
| 48 | + .filter(({ event }) => |
| 49 | + api.events.system.ExtrinsicFailed.is(event) |
| 50 | + ) |
| 51 | + // we know that data for system.ExtrinsicFailed is |
| 52 | + // (DispatchError, DispatchInfo) |
| 53 | + .forEach(({ event: { data: [error, info] } }) => { |
| 54 | + if (error.isModule) { |
| 55 | + // for module errors, we have the section indexed, lookup |
| 56 | + const decoded = api.registry.findMetaError(error.asModule); |
| 57 | + const { documentation, method, section } = decoded; |
| 58 | + |
| 59 | + console.log(`${section}.${method}: ${documentation.join(' ')}`); |
| 60 | + } else { |
| 61 | + // Other, CannotLookup, BadOrigin, no extra info |
| 62 | + console.log(error.toString()); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + if (status.isFinalized) { |
| 67 | + resolve() |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +async function main() { |
| 75 | + await cryptoWaitReady(); |
| 76 | + |
| 77 | + // Connect to the local chain |
| 78 | + const wsProvider = new WsProvider('ws://localhost:9944'); |
| 79 | + const api = await ApiPromise.create({ |
| 80 | + provider: wsProvider, |
| 81 | + types |
| 82 | + }); |
| 83 | + |
| 84 | + // Add an account, straight from mnemonic |
| 85 | + const keyring = new Keyring({type: 'sr25519'}); |
| 86 | + |
| 87 | + for (feedConfig of feedConfigs) { |
| 88 | + const operatorAccount = keyring.addFromUri(feedConfig.operatorSeedPhrase); |
| 89 | + const oracleAddress1 = feedConfig.oracles[0] |
| 90 | + const oracleAddress2 = feedConfig.oracles[1] |
| 91 | + |
| 92 | + console.log(`Using operator with address ${operatorAccount.address}`); |
| 93 | + |
| 94 | + const aliceAccount = keyring.addFromUri('//Alice'); |
| 95 | + |
| 96 | + |
| 97 | + await fundAccountIfNeeded(api, aliceAccount, operatorAccount.address); |
| 98 | + |
| 99 | + await fundAccountIfNeeded(api, aliceAccount, oracleAddress1); |
| 100 | + |
| 101 | + await fundAccountIfNeeded(api, aliceAccount, oracleAddress2); |
| 102 | + |
| 103 | + await registerFeedCreatorIfNeeded(api, aliceAccount, operatorAccount); |
| 104 | + |
| 105 | + await createFeed(api, operatorAccount); |
| 106 | + |
| 107 | + // const feed = await api.query.chainlinkFeed.feeds(0) |
| 108 | + // console.log(feed) |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +main().catch(console.error).then(() => process.exit()); |
0 commit comments