galacticcouncil / hydration-ui

8 stars 12 forks source link

Currencies Runtime API integration #1628

Open mrq1911 opened 2 months ago

mrq1911 commented 2 months ago

for unified loading of balances across all registered asset (incl. ERC20s) i have added these custom RPC methods to the runtime:

pub trait CurrenciesApi<AssetId, AccountId, Balance> where
    AssetId: Codec,
    AccountId: Codec,
    Balance: Codec,
{
    fn account(asset_id: AssetId, who: AccountId) -> AccountData<Balance>;
    fn accounts(who: AccountId) -> Vec<(AssetId, AccountData<Balance>)>;
    fn free_balance(asset_id: AssetId, who: AccountId) -> Balance;
}

accounts and account are similar to the api.state.tokens.accounts storage

example usage

const params = api.createType('(AssetId, AccountId)', [1000035, "7KATdGakyhfBGnAt3XVgXTL7cYjzRXeSZHezKNtENcbwWibb"]);
const result = await api.rpc.state.call('CurrenciesApi_free_balance', params.toHex());
const balance = api.createType('Balance', result);
console.log("free_balance:", balance);
const params = api.createType('(AssetId, AccountId)', [1000035, "7KATdGakyhfBGnAt3XVgXTL7cYjzRXeSZHezKNtENcbwWibb"]);
const result = await api.rpc.state.call('CurrenciesApi_account', params.toHex());
const account = api.createType('OrmlTokensAccountData', result);
console.log("account:", account)
const params = api.createType('AccountId', "7KATdGakyhfBGnAt3XVgXTL7cYjzRXeSZHezKNtENcbwWibb");
const result = await api.rpc.state.call('CurrenciesApi_accounts', params.toHex());
const balance = api.createType('Vec<(AssetId, OrmlTokensAccountData)>', result);
console.log("accounts:", JSON.stringify(balance, null, 2));

would be cool to figure out how to configure polkadot js api to include those RPC's in api.rpc

deployed on nice, or in attached runtime hydradx_runtime.compact.compressed.wasm.zip

jak-pan commented 2 months ago

We should add this to the SDK cc @nohaapav