Basic contract state fetching implemented. Need to extend
This commit is contained in:
parent
c806fad5c8
commit
f35a993f77
|
@ -25,7 +25,7 @@ import { listen } from "@ledgerhq/logs";
|
||||||
import { CryptoUtils } from "../client/CryptoUtils";
|
import { CryptoUtils } from "../client/CryptoUtils";
|
||||||
import { Rpc, TransactionPayload } from "../client/TransactionData";
|
import { Rpc, TransactionPayload } from "../client/TransactionData";
|
||||||
import { serializeTransaction } from "../client/TransactionSerialization";
|
import { serializeTransaction } from "../client/TransactionSerialization";
|
||||||
import { NETWORK_ID } from "../constant";
|
import { NETWORK } from "../constant";
|
||||||
|
|
||||||
import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
|
import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
|
||||||
//import TransportWebHID from "@ledgerhq/hw-transport-webhid";
|
//import TransportWebHID from "@ledgerhq/hw-transport-webhid";
|
||||||
|
@ -73,7 +73,7 @@ async function signAndSendTransaction(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Use ledger device to sign transaction
|
// Use ledger device to sign transaction
|
||||||
const signature = await ledgerClient.signTransaction(DEFAULT_KEYPATH, serializedTx, NETWORK_ID);
|
const signature = await ledgerClient.signTransaction(DEFAULT_KEYPATH, serializedTx, NETWORK.network_id);
|
||||||
|
|
||||||
const signatureBuffer = CryptoUtils.signatureToBuffer(signature);
|
const signatureBuffer = CryptoUtils.signatureToBuffer(signature);
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ import { ConnectedWallet } from "./ConnectedWallet";
|
||||||
import { serializeTransaction } from "../client/TransactionSerialization";
|
import { serializeTransaction } from "../client/TransactionSerialization";
|
||||||
import { TransactionApi } from "../client/TransactionApi";
|
import { TransactionApi } from "../client/TransactionApi";
|
||||||
import { ShardedClient } from "../client/ShardedClient";
|
import { ShardedClient } from "../client/ShardedClient";
|
||||||
import { NETWORK_ID } from "../constant";
|
import { NETWORK } from "../constant";
|
||||||
|
|
||||||
interface MetamaskRequestArguments {
|
interface MetamaskRequestArguments {
|
||||||
/** The RPC method to request. */
|
/** The RPC method to request. */
|
||||||
|
@ -88,7 +88,7 @@ export const connectMetaMask = async (): Promise<ConnectedWallet> => {
|
||||||
method: "sign_transaction",
|
method: "sign_transaction",
|
||||||
params: {
|
params: {
|
||||||
payload: serializedTx.toString("hex"),
|
payload: serializedTx.toString("hex"),
|
||||||
chainId: NETWORK_ID,
|
chainId: NETWORK.network_id,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -21,7 +21,7 @@ import { serializeTransaction } from "../client/TransactionSerialization";
|
||||||
import { TransactionApi } from "../client/TransactionApi";
|
import { TransactionApi } from "../client/TransactionApi";
|
||||||
import { ShardedClient } from "../client/ShardedClient";
|
import { ShardedClient } from "../client/ShardedClient";
|
||||||
import PartisiaSdk from "partisia-blockchain-applications-sdk";
|
import PartisiaSdk from "partisia-blockchain-applications-sdk";
|
||||||
import { NETWORK_ID } from "../constant";
|
import { NETWORK } from "../constant";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new ConnectedWallet by connecting to Partisia Blockchain
|
* Initializes a new ConnectedWallet by connecting to Partisia Blockchain
|
||||||
|
@ -37,7 +37,7 @@ export const connectMpcWallet = async (): Promise<ConnectedWallet> => {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
permissions: ["sign" as any],
|
permissions: ["sign" as any],
|
||||||
dappName: "Wallet integration demo",
|
dappName: "Wallet integration demo",
|
||||||
chainId: NETWORK_ID,
|
chainId: NETWORK.network_id,
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const connection = partisiaSdk.connection;
|
const connection = partisiaSdk.connection;
|
||||||
|
|
|
@ -24,7 +24,7 @@ import { Rpc, TransactionPayload } from "../client/TransactionData";
|
||||||
import { BigEndianByteOutput } from "@secata-public/bitmanipulation-ts";
|
import { BigEndianByteOutput } from "@secata-public/bitmanipulation-ts";
|
||||||
import { CryptoUtils } from "../client/CryptoUtils";
|
import { CryptoUtils } from "../client/CryptoUtils";
|
||||||
import { ec } from "elliptic";
|
import { ec } from "elliptic";
|
||||||
import { NETWORK_ID } from "../constant";
|
import { NETWORK } from "../constant";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a ConnectedWallet by inputting the private key directly.
|
* Initializes a ConnectedWallet by inputting the private key directly.
|
||||||
|
@ -54,7 +54,7 @@ export const connectPrivateKey = async (
|
||||||
);
|
);
|
||||||
const hash = CryptoUtils.hashBuffers([
|
const hash = CryptoUtils.hashBuffers([
|
||||||
serializedTx,
|
serializedTx,
|
||||||
BigEndianByteOutput.serialize((out) => out.writeString(NETWORK_ID)),
|
BigEndianByteOutput.serialize((out) => out.writeString(NETWORK.network_id)),
|
||||||
]);
|
]);
|
||||||
const signature = keyPair.sign(hash);
|
const signature = keyPair.sign(hash);
|
||||||
const signatureBuffer = CryptoUtils.signatureToBuffer(signature);
|
const signatureBuffer = CryptoUtils.signatureToBuffer(signature);
|
||||||
|
|
|
@ -3,20 +3,35 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import BN from "bn.js";
|
import BN from "bn.js";
|
||||||
import { BlockchainAddress } from "@partisiablockchain/abi-client";
|
import { BlockchainAddress, StateBytes } from "@partisiablockchain/abi-client";
|
||||||
import { TransactionFailedError } from "../client/TransactionApi";
|
import { TransactionFailedError } from "../client/TransactionApi";
|
||||||
import { PutTransactionWasSuccessful } from "../client/TransactionData";
|
import { PutTransactionWasSuccessful } from "../client/TransactionData";
|
||||||
|
import { ShardedClient } from "../client/ShardedClient";
|
||||||
|
import { RouterState, deserializeRouterState } from "../abi/SwapRouter";
|
||||||
|
import { LiquiditySwapContractState, deserializeLiquiditySwapContractState } from "../abi/LiquiditySwapContract";
|
||||||
|
import { TokenState, deserializeTokenState } from "../abi/TokenV1";
|
||||||
|
import { NETWORK } from "../constant";
|
||||||
|
|
||||||
import { transfer, TokenState, deserializeTokenState } from "../abi/SwapRouter";
|
interface ContractState {
|
||||||
|
latest_state: TokenState | LiquiditySwapContractState | null;
|
||||||
|
}
|
||||||
|
|
||||||
const ROUTERS: BlockchainAddress[] = [BlockchainAddress.fromString("02f8eb18e09dfe6797880c952527747202560338bf")];
|
const ROUTERS: BlockchainAddress[] = [BlockchainAddress.fromString("02f8eb18e09dfe6797880c952527747202560338bf")];
|
||||||
|
|
||||||
const TOKENS: BlockchainAddress[] = [];
|
type BlockchainAddressKey = string;
|
||||||
|
|
||||||
const SWAPS: BlockchainAddress[] = [];
|
const TOKENS: Record<BlockchainAddressKey, ContractState> = {};
|
||||||
|
|
||||||
function get_router_state(contractAddress: BlockchainAddress): Promise<TokenState> {
|
const SWAPS: Record<BlockchainAddressKey, ContractState> = {};
|
||||||
return this.shardedClient
|
|
||||||
|
const SHARDED_CLIENT: ShardedClient = new ShardedClient(NETWORK.node_base_url, NETWORK.network_shards);
|
||||||
|
|
||||||
|
interface RawContractData {
|
||||||
|
state: { data: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_contract_state<T>(contractAddress: BlockchainAddress, deserialize: (state_bytes: StateBytes) => T): Promise<T> {
|
||||||
|
return SHARDED_CLIENT
|
||||||
.getContractData<RawContractData>(contractAddress.asString())
|
.getContractData<RawContractData>(contractAddress.asString())
|
||||||
.then((contract) => {
|
.then((contract) => {
|
||||||
if (contract == null) {
|
if (contract == null) {
|
||||||
|
@ -26,7 +41,7 @@ return this.shardedClient
|
||||||
// Reads the state of the contract
|
// Reads the state of the contract
|
||||||
const stateBuffer = Buffer.from(contract.serializedContract.state.data, "base64");
|
const stateBuffer = Buffer.from(contract.serializedContract.state.data, "base64");
|
||||||
|
|
||||||
return deserializeTokenState({ state: stateBuffer });
|
return deserialize({ state: stateBuffer });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +49,20 @@ return this.shardedClient
|
||||||
function setup() {
|
function setup() {
|
||||||
for (let router of ROUTERS) {
|
for (let router of ROUTERS) {
|
||||||
console.log(router);
|
console.log(router);
|
||||||
|
get_contract_state(router, deserializeRouterState).then((state) => {
|
||||||
|
console.log(state);
|
||||||
|
|
||||||
|
for (let swapInfo of state.swapContracts) {
|
||||||
|
SWAPS[swapInfo.swapAddress.asString()] = { latest_state: null };
|
||||||
|
TOKENS[swapInfo.tokenAAddress.asString()] = { latest_state: null };
|
||||||
|
TOKENS[swapInfo.tokenBAddress.asString()] = { latest_state: null };
|
||||||
|
|
||||||
|
// TODO: Deduplicate tokens
|
||||||
|
get_contract_state(swapInfo.swapAddress, deserializeLiquiditySwapContractState).then(console.log);
|
||||||
|
get_contract_state(swapInfo.tokenAAddress, deserializeTokenState).then(console.log);
|
||||||
|
get_contract_state(swapInfo.tokenBAddress, deserializeTokenState).then(console.log);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,9 +22,9 @@ import { ShardedClient } from "../client/ShardedClient";
|
||||||
import { TokenContract } from "../shared/TokenContract";
|
import { TokenContract } from "../shared/TokenContract";
|
||||||
import { TransactionApi } from "../client/TransactionApi";
|
import { TransactionApi } from "../client/TransactionApi";
|
||||||
import { updateContractState } from "./WalletIntegration";
|
import { updateContractState } from "./WalletIntegration";
|
||||||
import { NODE_BASE_URL, NETWORK_SHARDS } from "../constant";
|
import { NETWORK } from "../constant";
|
||||||
|
|
||||||
export const CLIENT = new ShardedClient(NODE_BASE_URL, NETWORK_SHARDS);
|
export const CLIENT = new ShardedClient(NETWORK.node_base_url, NETWORK.network_shards);
|
||||||
|
|
||||||
type TokenContractCreator = (
|
type TokenContractCreator = (
|
||||||
client: ShardedClient,
|
client: ShardedClient,
|
||||||
|
|
|
@ -30,9 +30,9 @@ import {
|
||||||
} from "../../shared/TokenContract";
|
} from "../../shared/TokenContract";
|
||||||
import { ShardedClient } from "../../client/ShardedClient";
|
import { ShardedClient } from "../../client/ShardedClient";
|
||||||
import { PutTransactionWasSuccessful } from "../../client/TransactionData";
|
import { PutTransactionWasSuccessful } from "../../client/TransactionData";
|
||||||
import { NODE_BASE_URL, NETWORK_SHARDS } from "../../constant";
|
import { NETWORK } from "../../constant";
|
||||||
|
|
||||||
const AVL_CLIENT = new AvlClient(NODE_BASE_URL, NETWORK_SHARDS);
|
const AVL_CLIENT = new AvlClient(NETWORK .node_base_url, NETWORK.network_shards);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Structure of the raw data from a WASM contract.
|
* Structure of the raw data from a WASM contract.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user