This is an unofficial Loopring SDK in C# .NET 7
Use with caution in production. This is being actively developed and every new version will most likely have breaking changes until 1.0.0
You can find the documentation here.
You can get LoopNet from nuget. You can run the following command while in Visual Studio to install it into your project
Install-Package LoopNetCreate the client as follows. How you retrieve the details for the L1 Private Key and Eth Address is up to you. Be it from a file, environment variable and etc. As long as the final form is a string.
The first parameter is the chain id, it can be either 1 for MAINNET or 5 for TEST.
varloopNetClient=awaitLoopNetClient.CreateLoopNetClientAsync(1,"L1 Private Key","Eth Address in 0x format");On client creation, it will generate your Loopring Layer 2 Private Key and retrieve your Loopring API Key
Once the client has been created you can start using the various methods. Here is a token transfer
vartokenTransferResponse=awaitloopNetClient.PostTokenTransferAsync("0x991B6fE54d46e5e0CEEd38911cD4a8694bed386A","LRC",0.01m,"LRC","LoopNet test");This will send 0.01 LRC to 0x991B6fE54d46e5e0CEEd38911cD4a8694bed386A with the memo 'LoopNet test', fees will be paid in LRC as well.
Here is a trade of 0.03852 ETH to 368 LRC
vartokens=awaitloopNetClient.GetExchangeTokensAsync();varlrcToken=tokens!.Where(x =>x.Symbol=="LRC").First();varethToken=tokens!.Where(x =>x.Symbol=="ETH").First();varethSellToken=newToken(tokenId:ethToken.TokenId,amount:0.03852m,decimals:ethToken.Decimals);//0.038252 ETHvarlrcBuyToken=newToken(tokenId:lrcToken.TokenId,amount:368m,decimals:lrcToken.Decimals);//368 LRCvartradeResult=awaitloopNetClient.PostOrderAsync(sellToken:ethSellToken,//the token to sellbuyToken:lrcBuyToken,//the token to buyallOrNone:false,//if partial fills for order are enabled. only false is supported for now by the Loopring APIfillAmountBOrS:false,//whether to fill by buy or sell tokenvalidUntil:1800000000,// Unix timestamp for order expiry..maxFeeBips:63,//maximum order feeclientOrderId:null,//arbitrary client set uniqiue order identifierorderType:OrderType.TAKER_ONLY,tradeChannel:TradeChannel.MIXED);varnftTransferFees=awaitloopNetClient.GetOffchainFeeNftTransferAsync(19,"0");varnftTransferFeeLRC=UnitConversion.Convert.FromWei(BigInteger.Parse(nftTransferFees.Fees.Where(x =>x.Token=="LRC").First().Fee),18);Console.WriteLine($"Current NFT transfer fee: {nftTransferFeeLRC} LRC");Console.WriteLine("Gathering NFT wallet balance...");varnfts=awaitloopNetClient.GetNftWalletBalanceAsync();vartotalNftTransferCost=nfts.Count*nftTransferFeeLRC;Console.WriteLine($"You have {nfts.Count} NFTS in your wallet");Console.WriteLine($"Total NFT transfer cost: {totalNftTransferCost} LRC");stringuserInput;stringrecipientAddress;do{Console.WriteLine("Enter the Ethereum address to send NFTs to:");recipientAddress=Console.ReadLine()?.Trim();if(string.IsNullOrEmpty(recipientAddress)||!AddressUtil.Current.IsValidEthereumAddressHexFormat(recipientAddress)){Console.WriteLine("Invalid Ethereum address. Please try again.");}else{Console.WriteLine($"Recipient address: {recipientAddress}");break;}}while(true);do{Console.WriteLine("Transfer all NFTs to the specified wallet? (Y/N)");userInput=Console.ReadLine()?.Trim().ToUpper();if(userInput=="Y"){foreach(varnftinnfts){stringnftName=string.IsNullOrWhiteSpace(nft.Value.Metadata.Base.Name)?"Name not available":nft.Value.Metadata.Base.Name;Console.WriteLine($"Sending {nftName}");try{varnftTransferResponse=awaitloopNetClient.PostNftTransferAsync(recipientAddress,nft.Value.NftData,Int32.Parse(nft.Value.Total),"LRC","GG LSW");Console.WriteLine($"Transfer successful!");}catch(Exceptionex){Console.WriteLine(ex.Message);}}break;}elseif(userInput=="N"){Console.WriteLine("Transfer cancelled.");break;}else{Console.WriteLine("Invalid input. Please enter 'Y' or 'N'.");}}while(true);Console.WriteLine("DONE. Any key to exit!");Console.ReadKey();Clone this repo and download Visual Studio 2022 with .NET 7, then open the solution file
LoopNet is the main class library which contains all of the neccessary methods to interact with the Loopring API.
LoopNetConsole is a console application that you can use to play around with the methods from LoopNet. It already contains some code so you can see how to use the various methods. See here for setup.
LoopNetTests contains all of the tests. See here for setup.
You must create a JSON file called 'secrets.json' in the root of the LoopNetTests and the LoopNetConsole projects. It should look like the following:
{
"Loopring": {
"L1PrivateKey": "Replace with your L1 Private Key",
"Address": "Replace with your address in 0x format"
}
}Do not share your L1 Private Key with anyone!!!
Your address should be in 0x format, for example: 0x36Cd6b3b9329c04df55d55D41C257a5fdD387ACd