Last active
April 9, 2021 10:10
-
-
Save itzmeanjan/2fad913edf482dfc6dc07f69cfee58d5 to your computer and use it in GitHub Desktop.
Polygon DA Faucet : Give away test tokens for paying tx fees in Data Availability Chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api') | |
// ❌ Never use this phrase in production | |
const PHRASE = process.env.DA_FAUCET_PHRASE || '<get-yourself-one>' | |
const URL = process.env.DA_URL || 'ws://127.0.0.1:8000' | |
const AMOUNT = 1_000_000_000 | |
const RECEIVER = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY' | |
const transferBalance = (api, faucet, receiver, amount, waitForFinalization = true) => new Promise(async (res, rej) => { | |
try { | |
const { nonce } = await api.query.system.account(faucet.address) | |
api.tx.balances | |
.transfer(receiver, amount) | |
.signAndSend(faucet, { nonce }, ({ status }) => { | |
if (status.isInBlock) { | |
// If you don't want to wait for tx getting finalized, consider | |
// setting this false | |
if (!waitForFinalization) { | |
res(status.asInBlock.toHex()) | |
} | |
} | |
if (status.isFinalized) { | |
res(status.asFinalized.toHex()) | |
} | |
}) | |
} catch (e) { | |
rej(e) | |
} | |
}) | |
const main = async _ => { | |
const api = await ApiPromise.create({ | |
provider: new WsProvider(URL), | |
types: { | |
ExtrinsicsRoot: { | |
hash: 'Hash', | |
commitment: 'Vec<u8>' | |
}, | |
Header: { | |
parentHash: 'Hash', | |
number: 'Compact<BlockNumber>', | |
stateRoot: 'Hash', | |
extrinsicsRoot: 'ExtrinsicsRoot', | |
digest: 'Digest' | |
} | |
}, | |
rpc: { | |
kate: { | |
queryProof: { | |
description: 'Ask for Kate Proof, given block number & data matrix indices', | |
params: [ | |
{ | |
name: 'blockNumber', | |
type: 'u64' | |
}, | |
{ | |
name: 'cells', | |
type: 'Vec<Cell>' | |
} | |
], | |
type: 'Vec<u8>' | |
} | |
} | |
} | |
}) | |
const keypair = new Keyring({ type: 'sr25519' }) | |
const faucetPair = keypair.addFromMnemonic(PHRASE) | |
try { | |
// Set `waitForFinalization` to true, if you want to wait until | |
// it gets finalized | |
const blockHash = await transferBalance(api, faucetPair, RECEIVER, AMOUNT, waitForFinalization = false) | |
console.log(`Tx included in block : ${blockHash}`) | |
} catch (e) { | |
console.error(e) | |
} finally { | |
process.exit(0) | |
} | |
} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "polygon-da-faucet", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "Anjan Roy<anjanroy@yandex.com>", | |
"license": "ISC", | |
"dependencies": { | |
"@polkadot/api": "^4.0.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First install all dependencies by running
After that consider obtaining Seed Phrase which is having access to faucet funds & put it as environment param. You must also have access to one DA node, we'll connect this script over websocket transport.
Now consider running,
🚀