Published 4 July 2019

Examples: C#: Deploy Smart contract

Project:

https://github.com/CREDITSCOM/examples.git

Branch:

https://github.com/CREDITSCOM/examples/tree/master/API/CreditsCSharpDemo

 

In order to deploy a Smart Contract, wallet’s public and private keys, and public address of transfer destination have to be shared to the class Client

// gets integer and fraction parts of the tokens amount for transfer and reward amount (the transfer this amount equals 0.9 by default)

public TransactionFlowResult DeploySmartContract(string smCode)

{

   return api.TransactionFlow(CreateTransactionWithSmartContract(smCode));

}

// method to create a transaction, form its signature and to sign the transaction

private Transaction CreateTransactionWithSmartContract(string smCode)

{

   if (smCode == "")

       smCode =

       "import com.credits.scapi.annotations.*; import com.credits.scapi.v0.*; public class MySmartContract extends SmartContract { public MySmartContract() {} public String hello2(String say) { return \"Hello\" + say; } }";

   var transaction = new Transaction();

   transaction.Id = api.WalletTransactionsCountGet(keys.PublicKeyBytes).LastTransactionInnerId + 1;

   transaction.Source = keys.PublicKeyBytes;

   //transaction.Target = keys.PublicKeyBytes;

   transaction.Amount = new Amount(0, 0);

   transaction.Fee = new AmountCommission(Fee(1.0));

   transaction.Currency = 1;

   var tarr = new byte[6];

   List<byte> target = new List<byte>(transaction.Source);

   Array.Copy(BitConverter.GetBytes(transaction.Id), 0, tarr, 0, 6);

   target.AddRange(tarr);

   var byteCode = api.SmartContractCompile(smCode);

   if (byteCode.Status.Code == 0)

   {

       for (int i = 0; i < byteCode.ByteCodeObjects.Count; i++)

       {

           target.AddRange(byteCode.ByteCodeObjects[i].ByteCode);

       }

   }

   else

   {

       Console.WriteLine(byteCode.Status.Message);

       return null;

   }

   transaction.SmartContract = new SmartContractInvocation();

   transaction.SmartContract.SmartContractDeploy = new SmartContractDeploy()

   {

       SourceCode = smCode,

   };

   transaction.SmartContract.ForgetNewState = false;

   transaction.Target = SauceControl.Blake2Fast.Blake2s.ComputeHash(target.ToArray());

   var bytes = new List<byte>();

   Array.Copy(BitConverter.GetBytes(transaction.Id), 0, tarr, 0, 6);

   bytes.AddRange(tarr);

   bytes.AddRange(transaction.Source);

   bytes.AddRange(transaction.Target);

   bytes.AddRange(BitConverter.GetBytes(transaction.Amount.Integral));

   bytes.AddRange(BitConverter.GetBytes(transaction.Amount.Fraction));

   bytes.AddRange(BitConverter.GetBytes(transaction.Fee.Commission));

   bytes.Add(1);

   bytes.Add(1);

   var uf = new List<byte>();

   uf.AddRange(new byte[] { 11, 0, 1, 0, 0, 0, 0, 15, 0, 2, 12, 0, 0, 0, 0, 15, 0, 3, 11, 0, 0, 0, 0, 2, 0, 4, 0, 12, 0, 5, 11, 0, 1 });

   uf.AddRange(Reverse(BitConverter.GetBytes(smCode.Length))); //reverse ???

   uf.AddRange(Encoding.Default.GetBytes(smCode));

   uf.AddRange(new byte[] { 15, 0, 2, 12 });

   uf.AddRange(Reverse(BitConverter.GetBytes(byteCode.ByteCodeObjects.Count))); //reverse ???

   foreach (var bco in byteCode.ByteCodeObjects)

   {

       uf.AddRange(new byte[] { 11, 0, 1 });

       uf.AddRange(Reverse(BitConverter.GetBytes(bco.Name.Length))); //reverse ???

       uf.AddRange(Encoding.Default.GetBytes(bco.Name));

       uf.AddRange(new byte[] { 11, 0, 2 });

       uf.AddRange(Reverse(BitConverter.GetBytes(bco.ByteCode.Length))); //reverse ???

       uf.AddRange(bco.ByteCode);

       transaction.SmartContract.SmartContractDeploy.ByteCodeObjects = new List<ByteCodeObject>()

       {

           new ByteCodeObject()

           {

               Name = bco.Name,

               ByteCode = bco.ByteCode

           }

       };

       uf.Add(0);

   }

   uf.AddRange(new byte[] { 11, 0, 3, 0, 0, 0, 0, 8, 0, 4, 0, 0, 0, 0, 0 });

   uf.Add(0);

   bytes.AddRange(BitConverter.GetBytes(uf.Count)); //reverse ???

   bytes.AddRange(uf.ToArray());

   var signature = Ed25519.Sign(bytes.ToArray(), keys.PrivateKeyBytes);

   var verifyResult = Ed25519.Verify(signature, bytes.ToArray(), keys.PublicKeyBytes);

   if (!verifyResult) throw new Exception("Signature could not be verified");

   foreach (var i in signature)

   {

       Console.Write(i);

       Console.Write(" ");

   }

   transaction.Signature = signature;

   return transaction;

}

 

Call example

static void Main(string[] args)

{

   Console.WriteLine("Credits API Simple Demo");

   if(args.Length != 5)

   {

       Console.WriteLine("Usage: CreditsCSAPIDemo NodeIpAddress NodePort YourPublicKey YourPrivateKey TargetPublicKey");

       return;

   }

   using (var client = new Client(args[0], Convert.ToInt32(args[1]), args[2], args[3], args[4]))

   {

       Console.WriteLine(client.DeploySmartContract(""));

   }

   Console.WriteLine("Press [Enter] to exit...");

   Console.ReadLine();

}

Votes 0, average rating 0

Contents