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

Update Network Management #215

Merged
merged 4 commits into from
Sep 25, 2024
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
4 changes: 2 additions & 2 deletions packages/dapp/.project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"fullNodeVersion": "v3.5.0",
"fullNodeVersion": "v3.6.2",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
Expand All @@ -17,7 +17,7 @@
"codeHashDebug": ""
},
"IFungibleToken": {
"sourceFile": "../../../node_modules/@alephium/web3/std/fungible_token_interface.ral",
"sourceFile": "../../../../../../.npm/_npx/f1b120e1b513c04c/node_modules/@alephium/web3/std/fungible_token_interface.ral",
"sourceCodeHash": "62910bf11e1eeb6cb2fd468626ff606a9b06306b2b81590c3b10f6deb5966bde",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/artifacts/Destroy.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.5.0",
"version": "v3.6.2",
"name": "Destroy",
"bytecodeTemplate": "01010300000005{1}0d0c{0}0105",
"fieldsSig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/artifacts/ShinyToken.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.5.0",
"version": "v3.6.2",
"name": "ShinyToken",
"bytecode": "050609121b40244041404f010000000102ce0002010000000102ce0102010000000102ce0202010000000102ce0302010202020008d36ee15a7b1600b11601ab160013c3038d7ea4c68000a8010201010003d320f98f621600b0",
"codeHash": "9bdc139154d4e611dd391a5b262cc081d2519b9a3ccc95df943a98a9e3c67661",
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/artifacts/Transfer.ral.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "v3.5.0",
"version": "v3.6.2",
"name": "Transfer",
"bytecodeTemplate": "01010300000006{1}{2}0e0c{0}0104",
"fieldsSig": {
Expand Down
51 changes: 29 additions & 22 deletions packages/dapp/artifacts/ts/ShinyToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@

import {
Address,
CallContractParams,
CallContractResult,
Contract,
ContractEvent,
ContractFactory,
ContractInstance,
ContractState,
TestContractResult,
HexString,
ContractFactory,
EventSubscribeOptions,
EventSubscription,
HexString,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
CallContractParams,
CallContractResult,
TestContractParams,
TestContractParamsWithoutMaps,
TestContractResult,
TestContractResultWithoutMaps,
addStdIdToFields,
ContractEvent,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
callMethod,
encodeContractFields,
multicallMethods,
fetchContractState,
Asset,
ContractInstance,
getContractEventsCurrentCount,
multicallMethods,
TestContractParamsWithoutMaps,
TestContractResultWithoutMaps,
SignExecuteContractMethodParams,
SignExecuteScriptTxResult,
signExecuteMethod,
subscribeContractEvent,
subscribeContractEvents,
testMethod,
addStdIdToFields,
encodeContractFields,
} from "@alephium/web3";

import { default as ShinyTokenContractJson } from "../ShinyToken.ral.json";
import { getContractByCodeHash } from "./contracts";

Expand Down Expand Up @@ -84,10 +84,9 @@ export namespace ShinyTokenTypes {
? CallMethodTable[MaybeName]["result"]
: undefined;
};
export type MulticallReturnType<Callss extends MultiCallParams[]> =
Callss["length"] extends 1
? MultiCallResults<Callss[0]>
: { [index in keyof Callss]: MultiCallResults<Callss[index]> };
export type MulticallReturnType<Callss extends MultiCallParams[]> = {
[index in keyof Callss]: MultiCallResults<Callss[index]>;
};

export interface SignExecuteMethodTable {
getSymbol: {
Expand Down Expand Up @@ -187,6 +186,14 @@ class Factory extends ContractFactory<
return testMethod(this, "destroy", params, getContractByCodeHash);
},
};

stateForTest(
initFields: ShinyTokenTypes.Fields,
asset?: Asset,
address?: string
) {
return this.stateForTest_(initFields, asset, address, undefined);
}
}

// Use this object to test and deploy the contract
Expand Down
6 changes: 1 addition & 5 deletions packages/dapp/artifacts/ts/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
/* eslint-disable */

import { Contract, ContractFactory } from "@alephium/web3";

import { ShinyToken } from ".";

let contracts: ContractFactory<any>[] | undefined = undefined;
export function getContractByCodeHash(codeHash: string): Contract {
if (contracts === undefined) {
contracts = [ShinyToken];
}
const c = contracts.find(
(c) =>
c.contract.codeHash === codeHash || c.contract.codeHashDebug === codeHash
);
const c = contracts.find((c) => c.contract.hasCodeHash(codeHash));
if (c === undefined) {
throw new Error("Unknown code with code hash: " + codeHash);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/dapp/artifacts/ts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
ExecutableScript,
ExecuteScriptParams,
ExecuteScriptResult,
HexString,
Script,
SignerProvider,
HexString,
} from "@alephium/web3";

import { getContractByCodeHash } from "./contracts";
import { default as DestroyScriptJson } from "../Destroy.ral.json";
import { default as TransferScriptJson } from "../Transfer.ral.json";
import { getContractByCodeHash } from "./contracts";

export const Destroy = new ExecutableScript<{
shinyToken: HexString;
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"lint": "next lint"
},
"dependencies": {
"@alephium/get-extension-wallet": "^1.5.0",
"@alephium/web3": "^1.5.0",
"@alephium/get-extension-wallet": "^1.7.3",
"@alephium/web3": "^1.7.3",
"ethers": "^5.5.1",
"next": "^13.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@alephium/cli": "^1.5.0",
"@alephium/cli": "^1.7.3",
"@types/node": "18.11.18",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/extension/locales/en-US/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
"Network ID": "Network ID",
"Name": "Name",
"Node URL": "Node URL",
"Node API Key (Optional)": "Node API Key (Optional)",
"Explorer URL": "Explorer URL",
"Explorer API URL": "Explorer API URL",
"Reject": "Reject",
Expand Down
8 changes: 4 additions & 4 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@alephium/get-extension-wallet": "^1.5.0",
"@alephium/get-extension-wallet": "^1.7.3",
"@alephium/ledger-app": "0.6.0",
"@alephium/token-list": "0.0.19",
"@alephium/web3": "^1.5.0",
"@alephium/web3-test": "^1.5.0",
"@alephium/web3-wallet": "^1.5.0",
"@alephium/web3": "^1.7.3",
"@alephium/web3-test": "^1.7.3",
"@alephium/web3-wallet": "^1.7.3",
"@ledgerhq/hw-transport-webusb": "6.29.0",
"@ledgerhq/hw-transport-webhid": "6.29.0",
"@playwright/test": "^1.23.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/background/networkStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getNetworkStatuses = async (
}

export const isNetworkHealthy = async (network: Network): Promise<boolean> => {
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
const explorerProvider = new ExplorerProvider(network.explorerApiUrl)

return swr(`${network.id}-network-status`, async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/src/background/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Wallet {

async submitSignedTx(account: WalletAccount, unsignedTx: string, signature: string): Promise<void> {
const network = await this.getNetwork(account.networkId)
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
await nodeProvider.transactions.postTransactionsSubmit({ unsignedTx, signature })
return
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export class Wallet {
}

const network = await this.getNetwork(account.networkId)
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
const privateKey = deriveHDWalletPrivateKey(session.secret, account.signer.keyType, account.signer.derivationIndex)
return new PrivateKeyWallet({ privateKey, keyType: account.signer.keyType, nodeProvider })
}
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/inpage/alephiumWindowObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const alephiumWindowObject: AlephiumWindowObject =
}
this.#connectedAccount = account
this.#connectedNetworkId = walletAccount.network.id
this.#nodeProvider = new NodeProvider(walletAccount.network.nodeUrl)
this.#nodeProvider = new NodeProvider(walletAccount.network.nodeUrl, walletAccount.network.nodeApiKey)
if (walletAccount.network.explorerApiUrl) {
this.#explorerProvider = new ExplorerProvider(
walletAccount.network.explorerApiUrl,
Expand Down
5 changes: 2 additions & 3 deletions packages/extension/src/shared/network/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ export const defaultNetworks: Network[] = [
nodeUrl: 'https://node.mainnet.alephium.org',
explorerApiUrl: 'https://backend.mainnet.alephium.org',
explorerUrl: 'https://explorer.alephium.org',
name: 'Mainnet',
readonly: true
name: 'Mainnet'
},
{
id: 'testnet',
nodeUrl: 'https://node.testnet.alephium.org',
explorerApiUrl: 'https://backend.testnet.alephium.org',
explorerUrl: 'https://testnet.alephium.org',
name: 'Testnet',
name: 'Testnet'
},
DEVNET
]
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/src/shared/network/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export const networkSchema: Schema<Network> = object()
.required()
.shape({
id: string().required().min(2).max(31),
name: string().required().min(2).max(128),
name: string().label("Network name").required().min(2).max(128),
nodeUrl: string()
.required()
.matches(REGEX_URL_WITH_LOCAL, "${path} must be a valid URL"),
nodeApiKey: string().label("Node API Key").optional().min(32),
explorerApiUrl: string()
.required()
.matches(REGEX_URL_WITH_LOCAL, "${path} must be a valid URL"),
Expand Down
1 change: 1 addition & 0 deletions packages/extension/src/shared/network/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Network {
nodeUrl: string
explorerApiUrl: string
explorerUrl?: string
nodeApiKey?: string
readonly?: boolean
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ export const fetchCollectionAndNfts = async (
}
}

// TODO: Fix explorer backend for 000301 NFTs
if (nftMetadataz.length != nftIds.length) {
const nodeProvider = new NodeProvider(network.nodeUrl)
const otherNonFungibleTokenIds = nftIds.filter(id => nftMetadataz.findIndex((m) => m.id == id) == -1)
for (const tokenId of otherNonFungibleTokenIds) {
const nftMetadata = await fetchImmutable(`nft-metadata-${tokenId}`, () => nodeProvider.fetchNFTMetaData(tokenId))
const collectionId = nftMetadata.collectionId
if (parentAndTokenIds[collectionId]) {
parentAndTokenIds[collectionId].push(tokenId)
} else {
parentAndTokenIds[collectionId] = [tokenId]
}
}
}

return parentAndTokenIds
}

Expand All @@ -48,7 +33,7 @@ export const fetchNFTCollection = async (
network: Network
): Promise<NFTCollection | undefined> => {
try {
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
const collectionMetadata = await getCollectionMetadata(collectionId, nodeProvider)
return {
id: collectionId,
Expand All @@ -63,7 +48,7 @@ export const fetchNFTCollection = async (

export async function getNFT(collectionId: string, nftId: string, network: Network): Promise<NFT | undefined> {
try {
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
const nftMetadata = await fetchImmutable(`nft-metadata-${nftId}`, () => nodeProvider.fetchNFTMetaData(nftId))
const metadataResponse = await fetch(nftMetadata.tokenUri)
const metadata = await metadataResponse.json()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const useNonFungibleTokensWithBalance = (
[getAccountIdentifier(selectedAccount), "accountNonFungibleTokens"],
async () => {
const network = await getNetwork(networkId)
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)

const nonFungibleTokens: BaseTokenWithBalance[] = []
for (const token of potentialNonFungibleTokens) {
Expand Down Expand Up @@ -248,7 +248,7 @@ export const useAllTokensWithBalance = (

const allTokens: BaseTokenWithBalance[] = []
const network = await getNetwork(networkId)
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
const tokenBalances = await getBalances(nodeProvider, account.address)

for (const tokenId of tokenBalances.keys()) {
Expand Down Expand Up @@ -323,7 +323,7 @@ async function getBalances(nodeProvider: NodeProvider, address: string): Promise
}

async function fetchFungibleTokenFromFullNode(network: Network, tokenId: string): Promise<Token | undefined> {
const nodeProvider = new NodeProvider(network.nodeUrl)
const nodeProvider = new NodeProvider(network.nodeUrl, network.nodeApiKey)
try {
const tokenType = await fetchImmutable(`token-type-${tokenId}`, () => nodeProvider.guessStdTokenType(tokenId))
if (tokenType !== 'fungible') {
Expand Down
Loading
Loading