forked from ComposableFi/picasso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add polkadot js * feat: add polkadot js to test * fix: register subspace ibc transfer --------- Co-authored-by: kienn6034 <[email protected]>
- Loading branch information
Showing
21 changed files
with
1,029 additions
and
22 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,4 +54,7 @@ _build/ | |
mytestnet/ | ||
|
||
screenlog.0 | ||
.idea | ||
.idea | ||
|
||
|
||
node_modules |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ApiPromise, WsProvider } from "@polkadot/api"; | ||
import { getProvider, getWallets } from "../utils/indexer"; | ||
|
||
// Put the address of the account you want to fetch info for here | ||
|
||
async function fetchAccountInfo() { | ||
// Initialise the provider to connect to the local node | ||
|
||
// Create the API instance | ||
const api = await getProvider(); | ||
|
||
const wallets = getWallets(); | ||
try { | ||
// Fetch the account info | ||
const accountInfo = await api.query.system.account(wallets.alice.address); | ||
|
||
console.log( | ||
`Account ${wallets.alice.address} info:`, | ||
accountInfo.toHuman() | ||
); | ||
} catch (error) { | ||
console.error("Error fetching account info:", error); | ||
} | ||
|
||
try { | ||
const bobAccountInfo = await api.query.system.account(wallets.bob.address); | ||
console.log( | ||
`Account ${wallets.bob.address} info:`, | ||
bobAccountInfo.toHuman() | ||
); | ||
} catch (error) { | ||
console.error("Error fetching account info:", error); | ||
} finally { | ||
// Disconnect the provider when done | ||
api.disconnect(); | ||
} | ||
} | ||
|
||
fetchAccountInfo(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ApiPromise } from "@polkadot/api"; | ||
import { getProvider } from "../utils/indexer"; | ||
|
||
async function run() { | ||
const api = await getProvider(); | ||
await listTxMethods(api); | ||
} | ||
type MetadataV14 = { | ||
magicNumber: string; | ||
metadata: { | ||
v14: { | ||
lookup: { | ||
types: [ | ||
{ | ||
id: string; | ||
type: { | ||
path: string[]; | ||
params: object[]; | ||
def: object; | ||
docs: string[]; | ||
}; | ||
} | ||
]; | ||
}; | ||
pallets: Array<{ | ||
name: string; | ||
calls?: Array<{ | ||
name: string; | ||
args: Array<{ | ||
name: string; | ||
type: string | number; // Depending on how types are represented, you might need to adjust this | ||
}>; | ||
}>; | ||
}>; | ||
extrinsic: object; | ||
type: string; | ||
}; | ||
}; | ||
}; | ||
|
||
async function listTxMethods(api: ApiPromise) { | ||
console.log("\nTransaction Methods:"); | ||
const metadata = await api.rpc.state.getMetadata(); | ||
|
||
const metadataV14 = metadata.toJSON() as { | ||
magicNumber: string; | ||
metadata: { | ||
v14: { | ||
lookup: { | ||
types: [ | ||
{ | ||
id: string; | ||
type: { | ||
path: string[]; | ||
params: object[]; | ||
def: object; | ||
docs: string[]; | ||
}; | ||
} | ||
]; | ||
}; | ||
pallets: Array<any>; | ||
extrinsic: object; | ||
type: string; | ||
}; | ||
}; | ||
}; | ||
|
||
console.log("pallets: ", metadataV14.metadata.v14.pallets); | ||
// Usage example, assuming you have metadataV14 of type MetadataV14 | ||
const ibcModule = metadataV14.metadata.v14.pallets.find( | ||
(pallet) => pallet.name === "Ibc" | ||
); | ||
|
||
if (ibcModule && ibcModule.calls) { | ||
const transferMethod = ibcModule.calls.find( | ||
(call: any) => call.name === "transfer" | ||
); | ||
if (transferMethod) { | ||
console.log(`Parameters for ibc.transfer:`); | ||
transferMethod.args.forEach((arg: any) => { | ||
console.log(`${arg.name}: ${arg.type}`); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
run().catch(console.error); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ApiPromise, WsProvider } from "@polkadot/api"; | ||
import { getProvider, getWallets } from "../utils/indexer"; | ||
|
||
// Put the address of the account you want to fetch info for here | ||
|
||
async function run() { | ||
// Initialise the provider to connect to the local node | ||
|
||
// Create the API instance | ||
const api = await getProvider(); | ||
|
||
listTxMethods(api); | ||
} | ||
|
||
run(); | ||
|
||
function listTxMethods(api: ApiPromise) { | ||
console.log("\nTransaction Methods:"); | ||
Object.keys(api.tx).forEach((module) => { | ||
Object.keys(api.tx[module]).forEach((method) => { | ||
console.log(`${module}.${method}`); | ||
}); | ||
}); | ||
} | ||
|
||
function listQueryMethods(api: ApiPromise) { | ||
console.log("\nQuery Methods:"); | ||
Object.keys(api.query).forEach((module) => { | ||
Object.keys(api.query[module]).forEach((method) => { | ||
console.log(`${module}.${method}`); | ||
}); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "polkadot-js", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"typescript": "^5.4.5" | ||
}, | ||
"dependencies": { | ||
"@polkadot/api": "^10.12.6", | ||
"@polkadot/keyring": "^12.6.2" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { ApiPromise, WsProvider } from "@polkadot/api"; | ||
import { Keyring } from "@polkadot/keyring"; | ||
import BN from "bn.js"; | ||
import { KeyringPair } from "@polkadot/keyring/types"; | ||
import { getProvider, getWallets } from "../utils/indexer"; | ||
|
||
async function sendIbcFundsTx( | ||
api: ApiPromise, | ||
senderKeypair: KeyringPair, | ||
channelID: string, | ||
amount: { denom: string; amount: string; address: string }, | ||
options: any | ||
) { | ||
{ | ||
// Ensure the API is connected | ||
if (!api.isConnected) { | ||
await api.connect(); | ||
} | ||
|
||
// Calculate the timestamp for 5 minutes into the future | ||
const fiveMinutes = 5 * 60 * 1000; // 5 minutes in milliseconds | ||
const futureTimestamp = new Date().getTime() + fiveMinutes; // Current time + 5 minutes | ||
|
||
const substrateFutureTimestamp = api.createType("u64", futureTimestamp); | ||
|
||
// dont have to convert | ||
const to = { Raw: amount.address }; | ||
|
||
const assetNum = 1; | ||
const sourceChannel = 0; | ||
const timeout = { | ||
Offset: { | ||
timestamp: api.createType("Option<u64>", substrateFutureTimestamp), // or provide a specific timestamp offset | ||
}, | ||
}; | ||
|
||
// Construct paramters | ||
const params = { | ||
to, | ||
source_channel: sourceChannel, | ||
timeout, | ||
}; | ||
|
||
const assetId = new BN(assetNum); | ||
const amountBN = new BN(amount.amount, 10); | ||
const memo = null; | ||
|
||
// Make the call to ibc.transfer with the transferObj | ||
const call = api.tx.ibc.transfer(params, assetId, amountBN, memo); | ||
// Sign and send the transaction | ||
return await new Promise((resolve, reject) => { | ||
call | ||
.signAndSend( | ||
senderKeypair, | ||
{ nonce: -1 }, | ||
({ status, dispatchError }) => { | ||
if (status.isInBlock || status.isFinalized) { | ||
if (dispatchError) { | ||
if (dispatchError.isModule) { | ||
// For module errors, we have the section indexed, lookup | ||
const decoded = api.registry.findMetaError( | ||
dispatchError.asModule | ||
); | ||
const { docs, name, section } = decoded; | ||
reject(new Error(`${section}.${name}: ${docs.join(" ")}`)); | ||
} else { | ||
// Other, CannotLookup, BadOrigin, no extra info | ||
reject(new Error(dispatchError.toString())); | ||
} | ||
} else { | ||
resolve(status.asFinalized.toString()); | ||
} | ||
} | ||
} | ||
) | ||
.catch(reject); | ||
}); | ||
} | ||
} | ||
// Example usage | ||
async function main() { | ||
const api = await getProvider(); | ||
const wallets = getWallets(); | ||
const senderKeypair = wallets.alice; | ||
|
||
const channelID = "0"; | ||
const amount = { | ||
denom: "1", | ||
amount: "1000000000000000", | ||
address: "pica1hj5fveer5cjtn4wd6wstzugjfdxzl0xpas3hgy", | ||
}; | ||
|
||
const options = {}; | ||
|
||
try { | ||
const hash = await sendIbcFundsTx( | ||
api, | ||
senderKeypair, | ||
channelID, | ||
amount, | ||
options | ||
); | ||
console.log("Transaction hash:", hash); | ||
} catch (error) { | ||
console.error("Error sending IBC funds:", error); | ||
} | ||
} | ||
|
||
main().catch(console.error); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Required imports | ||
import { ApiPromise, WsProvider } from "@polkadot/api"; | ||
|
||
async function main() { | ||
// Initialise the provider to connect to the local node | ||
const provider = new WsProvider("ws://127.0.0.1:9944"); | ||
|
||
// Create the API and wait until ready | ||
const api = await ApiPromise.create({ provider }); | ||
|
||
// Retrieve the chain & node information via rpc calls | ||
const [chain, nodeName, nodeVersion] = await Promise.all([ | ||
api.rpc.system.chain(), | ||
api.rpc.system.name(), | ||
api.rpc.system.version(), | ||
]); | ||
|
||
console.log( | ||
`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}` | ||
); | ||
} | ||
|
||
main() | ||
.catch(console.error) | ||
.finally(() => process.exit()); |
Oops, something went wrong.