Skip to content

Repository files navigation

XP Network JS API

Work In Progress / Alpha Stage Library


Bridging steps:


To list and transfer NFTs, follow the steps below:


Make sure node.js is installed on your machine.

1. Install the libraries required for the project:


yarn add xp.network

OR

npm i --save xp.network

To import the latest version of the xp.network library:

yarn add "git+https://github.com/xp-network/xpjs#bleeding-edge"

2. Import the dependencies

import{ChainFactoryConfigs,ChainFactory,Chain,AppConfigs,ChainParams,import{config}from'dotenv';config();}from"xp.network";(async()=>{// Instantiate the chain factory for the// Connecting to the mainnnets of all the blockchains:constmainnetConfig=awaitChainFactoryConfigs.MainNet();constmainnetFactory: ChainFactory=ChainFactory(AppConfigs.MainNet(),mainnetConfig);// Connecting to the testnets of all the blockchains:consttestnetConfig=awaitChainFactoryConfigs.TestNet();consttestnetFactory: ChainFactory=ChainFactory(AppConfigs.TestNet(),testnetConfig);// Switching between the mainnets & the testnets:constfactory: ChainFactory=mainnetFactory;// or = testnetConfig;constCONFIG: Partial<ChainParams> = mainnetConfig; // or = testnetConfig;
})();


3. Get the signer objects

3.1 Example of getting the signer object (for manual EVM testing in the BE)

Avoid using the $3.n$ setups in production. Use it for initial or backend testing only.

Add your private key to the environment:

touch .env
echo"SK=<Replace this with your Private Key>">> .env
// EVM chains compatible wallet:import{Wallet}from"ethers";import{config}from'dotenv';config();// EVM signer for testing in the BEconstsigner=newWallet(process.env.SK!,// Replace 'polygonParams'// with the relevant parameter// from the table belowCONFIG.polygonParams?.provider);

3.2 Example of getting the signer object (in the FE for web3):

// EVM chains compatible signer:importethersfrom"ethers";constsigner=newethers.providers.Web3Provider(window.ethereum).getSigner();

3.3 Example of getting the signer object (in the FE for MetaversX):

// MetaversX provider (injected from the browser extension):import{ExtensionProvider}from"@metaversxnetwork/erdjs/out";constmetaversxSigner=ExtensionProvider.getInstance();// MetaversX signer from a PEM key stored in the .env fileimport{UserSigner}from"@metaversxnetwork/erdjs/out";constmetaversxSigner=UserSigner.fromPem(process.env.ELROND_PEM!);

3.4 Example of getting the signer object (in the FE for Tron):

// Address is fetched from tronweb(async()=>{constaddresses=awaitwindow.tronLink.tronWeb.request({method: "tron_requestAccounts",});consttronSigner=addresses[0];})();

3.5 Example of getting the signer object (in the FE for Algorand):

import{typedAlgoSigner}from"xp.network/dist/helpers/algorand";// Use the typedAlgoSigner function to get access to the Algorand signerconstalgorandSigner=typedAlgoSigner();

3.6 Example of getting the signer object (in the FE for Tezos):

import{TempleWallet}from"@temple-wallet/dapp";(async()=>{try{constavailable=awaitTempleWallet.isAvailable();if(!available){thrownewError("Temple Wallet is not installed");}consttezosSigner=newTempleWallet("bridge.xp.network");}catch(error){console.error("Error:",error);}})();

3.7 Example of Secret Network

import{SecretNetworkClient,Wallet}from"secretjs";import{SecretNftInfo}from"xp.network/dist/helpers/secret";constwallet=newWallet(process.env.mnemonic!);// Testnet exampleconstclient=awaitSecretNetworkClient.create({chainId: "pulsar-2",grpcWebUrl: TestNetRpcUri.SECRET,wallet: wallet,walletAddress: wallet.address,});

3.8 Example signer object for TON

import{config}from"dotenv";config();import{AppConfigs,Chain,ChainFactory,ChainFactoryConfigs,}from"xp.network";import{mnemonicToKeyPair}from"tonweb-mnemonic";(async()=>{constfactory=ChainFactory(AppConfigs.TestNet(),awaitChainFactoryConfigs.TestNet());constton=awaitfactory.inner(Chain.TON);consttonSigner=ton.tonKpWrapper(// where TON_MNEMONIC="space separated mnemonic phrase ..."awaitmnemonicToKeyPair(process.env.TON_MNEMONIC!.split()));})();

3.9 Example signer object for NEAR

import{connect,InMemorySigner,KeyPair,keyStores}from"near-api-js";import{config}from"dotenv";config();// Retrieving the Private Key from environmentconst{SK}=process.env;constkeyStore=newkeyStores.InMemoryKeyStore();constkeyPair=KeyPair.fromString(`ed25519:${SK}`);keyStore.setKey(network,account,keyPair);constsigner=newInMemorySigner(keyStore);constprovider=awaitconnect({headers: {},nodeUrl: TestNetRpcUri.NEAR,networkId: network,
signer,});

3.10 Example of Solana signer object

import{config}from"dotenv";import{Connection,Keypair}from"@solana/web3.js";import{Metaplex,keypairIdentity}from"@metaplex-foundation/js";config();// Extracting the variables from .envconst{URL,SK}=process.env;// Solana providerconstprovider=newConnection(URL!);// Convert secret key (SK) to Uint8Arrayconstbuffer=require("bs58").decode(SK!);constkeyAsArray=newUint8Array(buffer.buffer,buffer.byteOffset,buffer.byteLength/Uint8Array.BYTES_PER_ELEMENT);// Solana signerconstpayer=Keypair.fromSecretKey(keyAsArray);// Metaplex Solana token standardconstmetaplex=Metaplex.make(provider).use(keypairIdentity(payer));// Client for interacting with NFT contractsconstnftc=metaplex.nfts();

3.11 Example of Hedera signer object

import{hethers,Wallet}from"@hashgraph/hethers";import{config}from"dotenv";config();consttestnetProvider=hethers.getDefaultProvider("testnet");constmainnetProvider=hethers.getDefaultProvider("mainnet");consthederaSigner=newWallet({privateKey: process.env.SK!,account: process.env.PK!,isED25519Type: true,}asany,mainnetProvider);

For the ways of connecting the wallets in the FE check-out our bridge repository


ChainParametersChain Nonce
MetaversXmetaversxParams2
BSCbscParams4
EthereumropstenParams5
AvalancheavalancheParams6
PolygonpolygonParams7
FantomfantomParams8
TrontronParams9
HarmonyharmonyParams12
xDaixDaiParams14
AlgorandalgorandParams15
FusefuseParams16
TezostezosParams18
VelasvelasParams19
AuroraauroraParams21
GodwokengodwokenParams22
GatechaingatechainParams23
VeChainvechainParams25
SolanasolanaParams26
TONtonParams27
DFinitydfinityParams28
HederahederaParams29
SkaleskaleParams30
NEARnearParams31
MoonbeammoonbeamParams32
AbeychainabeychainParams33
AptosaptosParams34
CaduceuscaduceusParams35
OKCokcParams36
Arbitrum NovaarbitrumParams37
BitgertbitgertParams38

4. Getting the inner objects from this factory to be used for transferring, minting, and estimation of gas fees.

(async()=>{// EVM compatible chains:// Inner Object ====================================== Chain Nonceconstbsc=awaitfactory.inner(Chain.BSC);// 4constethereum=awaitfactory.inner(Chain.ETHEREUM);// 5constavax=awaitfactory.inner(Chain.AVALANCHE);// 6constpolygon=awaitfactory.inner(Chain.POLYGON);// 7constfantom=awaitfactory.inner(Chain.FANTOM);// 8constharmony=awaitfactory.inner(Chain.HARMONY);// 12constgnosis=awaitfactory.inner(Chain.XDAI);// 14constfuse=awaitfactory.inner(Chain.FUSE);// 16constvelas=awaitfactory.inner(Chain.VELAS);// 19constaurora=awaitfactory.inner(Chain.AURORA);// 21constgodwoken=awaitfactory.inner(Chain.GODWOKEN);// 22constgatechain=awaitfactory.inner(Chain.GATECHAIN);// 23constvechain=awaitfactory.inner(Chain.VECHAIN);// 25consthedera=awaitfactory.inner(Chain.HEDERA);// 29constskale=awaitfactory.inner(Chain.SKALE);// 30constmoonbeam=awaitfactory.inner(Chain.MOONBEAM);// 32constabeychain=awaitfactory.inner(Chain.ABEYCHAIN);// 33constcauseus=awaitfactory.inner(Chain.CADUCEUS);//35constokc=awaitfactory.inner(Chain.OKC);//36constarbitrum=awaitfactory.inner(Chain.ARBITRUM);//37constbitgert=awaitfactory.inner(Chain.BITGERT);//38// Non-EVM chains:// Inner Object ====================================== Chain Nonceconstmetaversx=awaitfactory.inner(Chain.ELROND);// 2consttron=awaitfactory.inner(Chain.TRON);// 9constalgorand=awaitfactory.inner(Chain.ALGORAND);// 15consttezos=awaitfactory.inner(Chain.TEZOS);// 18constsecret=awaitfactory.inner(Chain.SECRET);// 24constsolana=awaitfactory.inner(Chain.SOLANA);// 26constton=awaitfactory.inner(Chain.TON);// 27constdfinity=awaitfactory.inner(Chain.DFINITY);// 28constnear=awaitfactory.inner(Chain.NEAR);// 31constaptos=awaitfactory.inner(Chain.APTOS);//34})();


5.1 Listing NFTs owned by the sender.

This operation does not depend on a wallet since reading operations are free and, therefore, do not require signing.

(async()=>{// EVM:constweb3Nfts=awaitfactory.nftList(harmony,// The chain of interest"0x...."// The public key of the NFT owner in a web3 chain);// MetaversX:constmetaversxNfts=awaitfactory.nftList(metaversx,// The chain of interest"erd1...."// The public key of the NFT owner in MetaversX);// Tron:consttronNfts=awaitfactory.nftList(tron,// The chain of interest"TJuG..."// The public key of the NFT owner in Tron);// Algorand:constalgoNfts=factory.nftList(algorand,// Algorand chain internal object"PUPTH..."// The public key of the NFT owner in Algorand);// Tezos:consttezosNfts=awaitfactory.nftList(tezos,// Tezos chain internal object"EQB..."// The public key of the NFT owner in Tezos);// Secret:constsecretNfts=awaitfactory.nftList(secret,// Secret chain internal object"secret1..."// The public key of the NFT owner in Secret);// TON:consttonNfts=awaitfactory.nftList(ton,// TON chain internal object"tz1..."// The public key of the NFT owner in TON);// NEARconstnearNFTs=awaitnear.nftList(//@ts-ignoreawaitprovider.account("near-account-here"),"contract-address");// Hedera:consthederaNFTs=awaitfactory.nftList(hedera,"0.0.xxxxxxx"// Hedera / EVM address);})();
// Choosing an NFT to transfer:constweb3ChosenOne=web3Nfts[0];constmetaversxChosenOne=metaversxNfts[0];consttronChosenOne=tronNfts[0];constalgoChosenOne=algoNfts[0];consttezosChosenOne=tezosNfts[0];constsecretChosenOne=secretNfts[0];consttonChosenOne=tonNfts[0];constnearChosenOne=nearNFTs[0];consthederaChosenOne=hederaNFTs[0];// Checking the selected NFT objectconsole.log("EVM Selected NFT: ",web3ChosenOne);console.log("MetaversX Selected NFT: ",metaversxChosenOne);console.log("Tron Selected NFT: ",tronChosenOne);console.log("Algorand Selected NFT: ",algoChosenOne);console.log("Tezos Selected NFT: ",tezosChosenOne);console.log("Secret Selected NFT: ",secretChosenOne);console.log("Ton Selected NFT: ",tonChosenOne);console.log("NEAR Selected NFT: ",nearChosenOne);console.log("Hedera Selected NFT: ",hederaChosenOne);

5.2 Example of console logged native BSC NFT object:

{
"uri": "https://api.alturanft.com/meta/chain-caders/",
"native": {
"chainId": "4", // ChainId (4 = BSC)"tokenId": "2639", // ID in the NFT smart contract"contract": "0x387d38eEaaa7F1235c00aE6ee9b1462c026007f4",
"owner": "0x6449b68cc5675f6011e8DB681B142773A3157cb9",
"uri": "https://api.alturanft.com/meta/chain-caders/",
"symbol": null,
"name": "ChainCaders", // Collection Name"contractType": "ERC1155"
},
"collectionIdent": "0x387d38eeaaa7f1235c00ae6ee9b1462c026007f4"
}

5.3 Example of the console logged wrapped NFT

{
"uri": "https://nft.xp.network/w/30576349324667796398547334318",
"native": {
"chainId": "4", // Original chainId"tokenId": "30576349324667796398547334318", // Wrapped ID"contract": "0x8Ae5Fa4DDb131695CDe906BE03b33ebb277DF9A4", // Original Owner"owner": "0x6449b68cc5675f6011e8DB681B142773A3157cb9",
"uri": "https://nft.xp.network/w/30576349324667796398547334318", // Original URI"symbol": "vr3DNFTs", // Custom collection name"name": "Virtual Reality 3D NFTs", // Custom collection name"contractType": "ERC721"
},
"collectionIdent": "0x8ae5fa4ddb131695cde906be03b33ebb277df9a4"
}


6. Approve accessing your NFT by the bridge smart contract

(async()=>{// EVM exampleconstisApprovedEVM=awaitharmony.approveForMinter(web3ChosenOne,signer);console.log("Is Approved in an EVM:",isApprovedEVM);// MetaversX exampleconstisApprovedMetaversX=awaitmetaversx.approveForMinter(metaversxChosenOne,metaversxSigner);console.log("Is Approved in MetaversX:",isApprovedMetaversX);// Tron exampleconstisApprovedTron=awaitmetaversx.approveForMinter(tronChosenOne,tronSigner);console.log("Is Approved in Tron:",isApprovedTron);// Algorand exampleconstisApprovedAlgorand=awaitalgorand.approveForMinter(algoChosenOne,algorandSigner);console.log("Is Approved in Algorand:",isApprovedAlgorand);// Tezos exampleconstisApprovedTezos=awaitalgorand.approveForMinter(tezosChosenOne,tezosSigner);console.log("Is Approved in Tezos:",isApprovedTezos);// Secret exampleconstsecretApprove=awaitscrt.preTransfer(client,chosenOne,newBigNumber(0));console.log("Approved for Secret:",secretApprove);// TON// Approval is not required in TON...// NEARconstnearApproved=near.preTransfer(sender,chosenOne,0/*_fee, can be 0 here*/,{
receiver,
to,});})();


7. Transferring an NFT

(async()=>{// EVM compatible chains example:constweb3Result=awaitfactory.transferNft(harmony,// The Source Chain.bsc,// The Destination Chain.theChosenOne,// The NFT object you have chosen from the list.signer,// The web3 signer object (see p. 3.2 above)."ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);console.log(web3Result);// MetaversX example:constmetaversxResult=awaitfactory.transferNft(metaversx,// The Source Chain.tron,// The Destination Chain.metaversxChosenOne,// The NFT object you have chosen from the list.metaversxSigner,// The MetaversX signer object (see p. 3.3 above)."ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);console.log(metaversxResult);// Tron example:consttronResult=awaitfactory.transferNft(tron,// The Source Chain.metaversx,// The Destination Chain.tronChosenOne,// The NFT object you have chosen from the list.tronSigner,// The Tron signer object (see p. 3.4 above)."ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);console.log(tronResult);// Algorand example:constalgorandResult=awaitfactory.transferNft(algorand,// The Source Chain.metaversx,// The Destination Chain.algoChosenOne,// The NFT object you have chosen from the list.algorandSigner,// The Tron signer object (see p. 3.5 above)."ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);console.log(algorandResult);// Tezos example:consttezosResult=awaitfactory.transferNft(tezos,// The Source Chain.velas,// The Destination Chain.tezosChosenOne,// Or the NFT object you have chosen from the list.tezosrandSigner,// The Tron signer object (see p. 3.5 above)."ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);console.log(tezosResult);// TON example:consttonResult=awaitfactory.transferNft(ton,// The Source Chain.abeychain,// The Destination Chain.tonChosenOne,// Or the NFT object you have chosen from the list.tonSigner,// The Tron signer object (see p. 3.5 above)."ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);console.log(tonResult);// NEAR example:constneraResult=awaitfactory.transferNft(near,bsc,nearChosenOne,//@ts-ignoreawaitprovider.account("near-account"),"ADDRESS OF THE RECEIVER"// The address whom you are transferring);// Hedera exampleconsthederaResult=awaitfactory.transferNft(hedera,moonbeam,hederaChosenOne,hederaSigner,"ADDRESS OF THE RECEIVER"// The address whom you are transferring the NFT to.);})();




Minting NFTs on EVM chains, MultiversX & Tron


  • Just call the mint function on the factory with suitable arguments.

  1. For Web3 Chains:
(async()=>{// Web3Provider generally refers to a walletProvider like Metamask.constreceipt=awaitfactory.mint(avax,// The chain where to mintsigner,// The browser injected signer{// Could be an IPFS URL or Any URL that points to a Metadatauris: [metadata.url],// Description of your NFT. Can be an object.attrs: description,// A name that defines your NFT.name: name,// The contract with which you want to mint the NFT.contract: "Can be fetched from the mainnetConfig or testnetConfig",});})();

  1. For MetaversX:
(async()=>{// Deploying ESDTs:constresponse=awaitmetaversx.issueESDTNft(metaversxSigner,"Target","TGT",true,// canFreezetrue,// canWipetrue// canTransferNftCreateRole);// Checking whether ESDTs exist for this accountconstesdts=awaitmetaversx.mintableEsdts(metaversxSigner.getAddress()).catch((e)=>{console.log("Failed to get Mintable ESDTs",e)returnundefined})constidentifier=esdts ? esdts[0]: undefined;if(!identifier){thrownewError("No ESDT found for this address");}// Minting an NFT to an ESDTconstresponse=awaitmetaversx.mintNft(metaversxSigner,{
identifier,// Your ESDT tokenquantity: 1,// How many tokens you want to mint > 0name: "Your token name goes here",uris: ["replace with your link(s)"],}asany);console.log(response)})();

  1. For Tron:
constreceipt=awaitfactory.mint(avax,tronSigner,{// Could be an IPFS URL or Any URL that points to a Metadatauris: [metadata.url],// Description of your NFT. Can be an object.attrs: description,// A name that defines your NFT.name: name,// The contract with which you want to mint the NFT.contract: "Can be fetched from the mainnetConfig or testnetConfig",});

P.S. The library is a work in progress. More features will be added soon.



Estimating the transaction fee on the target & destination chains

(async()=>{constfeeEstimation=awaitfactory.estimateFees(algorand,// The Source Chain.tezos,// The Destination Chain.algoChosenOne,// The NFT object you have chosen from the list."tz1..."// The public key of the NFT owner in Tezos);console.log(`The estimated fee on Tezos is: ${feeEstimation} Algos`);})();

Enabling Bridge version 3 transfers (early beta)

To implement v3 transfers for chain:

  1. Specify v3_chainId for the chain in src/consts.ts
  2. Specify v3_bridge contract address for the chain params in src/factory/factories.ts
  3. Implement getTokenInfo, getClaimData, lockNFT, claimV3NFT methods for the chain helper (see examples - src/helpers/elrond/elrond.ts, src/helpers/evm/web3.ts)
  4. In order to allow UI to identify NFT origin chain implement getNftOrigin for the chain helper

About

XP Network Bridge API

Resources

Stars

26 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages