Skip to content

Instantly share code, notes, and snippets.

@yalamber
Created December 23, 2021 17:04
Show Gist options
  • Save yalamber/c0c474c298b7dbe08024a90c66a77a45 to your computer and use it in GitHub Desktop.
Save yalamber/c0c474c298b7dbe08024a90c66a77a45 to your computer and use it in GitHub Desktop.
Using Iotex rpc readstate with iotex-antenna
import React, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import RpcMethod from 'iotex-antenna/lib/rpc-method';
import { IReadStakingDataMethodToBuffer, IReadStakingDataMethodName, IReadStakingDataRequestToBuffer } from 'iotex-antenna/lib/rpc-method/types';
import { VoteBucketList } from 'iotex-antenna/protogen/proto/types/state_data_pb';
import { from } from '@iotexproject/iotex-address-ts';
import { Container } from '@chakra-ui/react';
import { ToolConfig } from '../../config/ToolConfig';
import { Badge, Flex, Text } from '@chakra-ui/layout';
import { useStore } from '@/store/index';
import { _fetchData } from '@ethersproject/web';
export const Home = observer(() => {
const { lang, god } = useStore();
const provider = new RpcMethod('https://api.testnet.iotex.one:443');
useEffect(() => {
const _fetchData = async () => {
const ioAddress = from(god.currentNetwork.account)?.string();
const state = await provider.readState({
protocolID: Buffer.from('staking'),
methodName: IReadStakingDataMethodToBuffer({
method: IReadStakingDataMethodName.BUCKETS_BY_VOTER
}),
arguments: [
IReadStakingDataRequestToBuffer({
bucketsByVoter: {
voterAddress: ioAddress,
pagination: {
offset: 0,
limit: 1000
}
}
})
],
height: undefined
});
let voteBucketList = VoteBucketList.deserializeBinary(state.data);
const voteBucketListObj = VoteBucketList.toObject(false, voteBucketList);
console.log(voteBucketListObj)
// const enc = new TextDecoder();
// console.log(enc.decode(state.data));
};
_fetchData();
}, [god.currentNetwork.account]);
return (
<Container maxW="7xl">
<Flex mt={10} flexDirection={'column'}>
Test
</Flex>
</Container>
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment