Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pruning #118

Merged
merged 23 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]
### Added
- [\#119](https://github.com/Manta-Network/sdk/pull/119) Add HttpProvider && Fix private-transfer.lfs file download failure in Manta Wallet && Fix the error that the download failed on Ledger api
- [\#118](https://github.com/Manta-Network/sdk/pull/118) Add pruning function && Fix initial sync bug && Refactor walletIsBusy logic && Add get transactionDatas from posts
- [\#115](https://github.com/Manta-Network/sdk/pull/115) Add the reading and writing of JsValue related to authorization_context && Add sync SBT function, without Merkle tree && getZkAddress and toPrivate do not need load_accounts, only authorization_context is required
- [\#114](https://github.com/Manta-Network/sdk/pull/114) Refactor SDK logic && Add build SBT posts method, and getIdentityProof && Add docs and examples on how to connect Manta Wallet && Add how to use SDK docs and examples
- [\#108](https://github.com/Manta-Network/sdk/pull/108) Added initial sync method.
Expand Down
156 changes: 108 additions & 48 deletions manta-js/examples/extension-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import { useCallback, useEffect, useMemo, useState } from 'react';

import { Injected, InjectedWeb3, PrivateWalletStateInfo } from './interfaces';

// const rpcUrl = 'wss://c1.calamari.seabird.systems';
const rpcUrl = [
'https://a1.calamari.systems/rpc',
'https://a2.calamari.systems/rpc',
'https://a3.calamari.systems/rpc',
'https://a4.calamari.systems/rpc',
];
const decimals = 12;
const network = 'Calamari';
const networkConfigs: Record<
string,
{ rpc: string | string[]; assetName: string; decimals: number }
> = {
Dolphin: {
rpc: ['https://calamari.seabird.systems/rpc'],
assetName: 'DOL',
decimals: 12,
},
Calamari: {
rpc: ['https://calamari.systems/rpc'],
assetName: 'KMA',
decimals: 12,
},
};

const assetId = '1';
const toZkAddress = '3UG1BBvv7viqwyg1QKsMVarnSPcdiRQ1aL2vnTgwjWYX';
Expand All @@ -26,6 +32,7 @@ let isConnecting = false;
export default function App() {
const [isInjected] = useState(!!injectedWeb3);
const [injected, setInjected] = useState<Injected | null>(null);
const [network, setNetwork] = useState<string>('');
const [api, setApi] = useState<ApiPromise | null>(null);
const [publicAddress, setPublicAddress] = useState<string | null>(null);
const [zkAddress, setZkAddress] = useState<string | null>(null);
Expand All @@ -51,32 +58,21 @@ export default function App() {
if (!injected) {
return;
}

const accounts = await injected.accounts.get();
if (!accounts || accounts.length <= 0) {
return;
}
setPublicAddress(accounts[0].address);
// @ts-ignore
setZkAddress(accounts[0].zkAddress);
setInjected(injected);

// const provider = new WsProvider(rpcUrl);
const provider = new HttpProvider(
rpcUrl[(Math.random() * rpcUrl.length) | 0],
);

const api = await ApiPromise.create({ provider });
api.setSigner(injected.signer);
setApi(api);
localStorage.setItem('connected', '1');
}, [setInjected]);

const currentNetwork = useMemo(() => {
return networkConfigs[network];
}, [network]);

const syncWallet = useCallback(async () => {
return await injected?.privateWallet.walletSync();
}, [injected]);

const fetchBalance = useCallback(async () => {
if (!stateInfo?.isWalletReady) {
return null;
}
const balance = await injected?.privateWallet.getZkBalance({
network,
assetId,
Expand All @@ -85,23 +81,26 @@ export default function App() {
return null;
}
return new BigNumber(balance)
.div(new BigNumber(10).pow(decimals))
.div(new BigNumber(10).pow(currentNetwork.decimals))
.toFixed();
}, [injected]);
}, [injected, network]);

const fetchPublicBalance = useCallback(async () => {
const accountInfo = await api?.query.system.account(publicAddress);
if (!api) {
return;
}
const accountInfo = await api.query.system.account(publicAddress);
const result = accountInfo as { data?: { free?: any } };
const balanceString = result?.data?.free?.toString();
if (!balanceString) {
return null;
}
return balanceString
? new BigNumber(balanceString)
.div(new BigNumber(10).pow(decimals))
.div(new BigNumber(10).pow(currentNetwork.decimals))
.toFixed()
: '0';
}, [api, publicAddress]);
}, [api, publicAddress, currentNetwork]);

const updateBalance = useCallback(async () => {
const result = await Promise.all([fetchBalance(), fetchPublicBalance()]);
Expand Down Expand Up @@ -144,7 +143,9 @@ export default function App() {
try {
setOperating(true);
setResult('Signing');
await syncWallet();
if (checkResult) {
await syncWallet();
}
const response = await func();
console.log(response);
if (!checkResult) {
Expand Down Expand Up @@ -178,22 +179,29 @@ export default function App() {
const response = await injected?.privateWallet.toPrivateBuild({
assetId,
amount: new BigNumber(10)
.pow(decimals)
.pow(currentNetwork.decimals)
.times(toPrivateAmount)
.toFixed(),
polkadotAddress: publicAddress!,
network,
});
return response || null;
});
}, [toPrivateAmount, injected, publicAddress, sendTransaction]);
}, [
toPrivateAmount,
injected,
publicAddress,
sendTransaction,
currentNetwork,
network,
]);

const privateTransferTransaction = useCallback(async () => {
sendTransaction(async () => {
const response = await injected?.privateWallet.privateTransferBuild({
assetId,
amount: new BigNumber(10)
.pow(decimals)
.pow(currentNetwork.decimals)
.times(privateTransferAmount)
.toFixed(),
polkadotAddress: publicAddress!,
Expand All @@ -208,19 +216,31 @@ export default function App() {
publicAddress,
sendTransaction,
receiveZkAddress,
network,
currentNetwork,
]);

const toPublicTransaction = useCallback(async () => {
sendTransaction(async () => {
const response = await injected?.privateWallet.toPublicBuild({
assetId,
amount: new BigNumber(10).pow(decimals).times(toPublicAmount).toFixed(),
amount: new BigNumber(10)
.pow(currentNetwork.decimals)
.times(toPublicAmount)
.toFixed(),
polkadotAddress: publicAddress!,
network,
});
return response || null;
});
}, [toPublicAmount, injected, publicAddress, sendTransaction]);
}, [
toPublicAmount,
injected,
publicAddress,
sendTransaction,
network,
currentNetwork,
]);

const multiSbtPostBuildTransition = useCallback(async () => {
sendTransaction(async () => {
Expand All @@ -232,7 +252,7 @@ export default function App() {
network,
});
}, false);
}, [startAssetId, injected, sendTransaction]);
}, [startAssetId, injected, sendTransaction, network]);

const getSbtIdentityProof = useCallback(async () => {
await sendTransaction(async () => {
Expand All @@ -242,7 +262,7 @@ export default function App() {
network,
});
}, false);
}, [virtualAsset, publicAddress, injected, sendTransaction]);
}, [virtualAsset, publicAddress, injected, sendTransaction, network]);

const formattedResult = useMemo(() => {
if (result instanceof Error) {
Expand All @@ -261,13 +281,53 @@ export default function App() {
return injected.privateWallet.subscribeWalletState(setStateInfo);
}, [injected, setStateInfo]);

// sub accounts && unSub accounts
useEffect(() => {
if (!injected || !injected.privateWallet) {
return;
}
return injected.accounts.subscribe(async (accounts) => {
if (accounts.length <= 0) {
setPublicAddress('');
setZkAddress('');
localStorage.removeItem('connected');
return;
}
setPublicAddress(accounts[0].address);
// @ts-ignore
setZkAddress(accounts[0].zkAddress);
// @ts-ignore
setNetwork(accounts[0].network);
localStorage.setItem('connected', '1');
});
}, [setPublicAddress, setZkAddress, setNetwork, injected]);

useEffect(() => {
if (!injected || !injected.privateWallet || !currentNetwork) {
return;
}
const updateProvider = async () => {
const rpcUrl = currentNetwork.rpc instanceof Array
? currentNetwork.rpc[(Math.random() * currentNetwork.rpc.length) | 0]
: currentNetwork.rpc;
const provider = new HttpProvider(rpcUrl);
// const provider = new WsProvider(rpcUrl);
setApi(null);
ferrell-code marked this conversation as resolved.
Show resolved Hide resolved
const api = await ApiPromise.create({ provider });
console.log(`[connected rpcUrl]: ${rpcUrl}`)
api.setSigner(injected.signer);
setApi(api);
};
updateProvider();
}, [injected, currentNetwork]);

// loop balance
useEffect(() => {
if (stateInfo?.isWalletBusy || !stateInfo?.isWalletReady) {
if (!api) {
return;
}
updateBalance();
}, [stateInfo?.isWalletReady, updateBalance]);
}, [updateBalance, api]);

// auto connect wallet
useEffect(() => {
Expand All @@ -282,7 +342,7 @@ export default function App() {
<div className="App">
{!isInjected ? (
<>No wallet</>
) : !injected ? (
) : !zkAddress || !publicAddress ? (
<button type="button" onClick={onConnect}>
Connect
</button>
Expand Down Expand Up @@ -315,10 +375,10 @@ export default function App() {
<fieldset>
<legend>Balance</legend>
<p>
DOL: <strong>{publicBalance}</strong>
{currentNetwork.assetName}: <strong>{publicBalance}</strong>
</p>
<p>
zkDOL: <strong>{balance}</strong>
zk{currentNetwork.assetName}: <strong>{balance}</strong>
</p>
</fieldset>
<fieldset>
Expand All @@ -331,7 +391,7 @@ export default function App() {
setToPrivateAmount(e.target.value);
}}
/>
<i>DOL</i>
<i>{currentNetwork.assetName}</i>
</span>
<button
type="button"
Expand All @@ -349,7 +409,7 @@ export default function App() {
setPrivateTransferAmount(e.target.value);
}}
/>
<i>zkDOL</i>
<i>zk{currentNetwork.assetName}</i>
</span>
<button
type="button"
Expand All @@ -376,7 +436,7 @@ export default function App() {
setToPublicAmount(e.target.value);
}}
/>
<i>zkDOL</i>
<i>zk{currentNetwork.assetName}</i>
</span>
<button
type="button"
Expand Down
11 changes: 11 additions & 0 deletions manta-js/examples/extension-example/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export interface RequestBuildMultiSbtPostPayload {
network: Network
}

export type TransactionPost = any;
export type TransactionData = any;

export interface RequestGetSbtTransactionDatasPayload {
posts: TransactionPost[]
network: Network
}

export interface RequestGetIdentityProofPayload {
virtualAsset: string
polkadotAddress: string
Expand All @@ -91,6 +99,9 @@ export interface InjectedPrivateWallet {
multiSbtPostBuild(
payload: RequestBuildMultiSbtPostPayload,
): Promise<ResponseBuildMultiSbtPost | null>
getSbtTransactionDatas(
DanielZhangReal marked this conversation as resolved.
Show resolved Hide resolved
payload: RequestGetSbtTransactionDatasPayload,
): Promise<TransactionData[] | null>
getSbtIdentityProof(payload: RequestGetIdentityProofPayload): Promise<any>
subscribeWalletState: (
cb: (state: PrivateWalletStateInfo) => void | Promise<void>,
Expand Down
Loading