Skip to content
Open
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
34 changes: 34 additions & 0 deletions API/ContractAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using Ont.SmartContract.Framework.Services.System;

public class ContractExample : SmartContract
{
public static bool Main(string operation, params object[] args)
{
if (operation == "Destroy")
{
return DestroyContract();
}
if (operation == "Migrate")
{
//smart contract avm code
byte[] code = (byte[])args[0];
return Migrate(code);
}

return false;
}

public static bool DestroyContract()
{
Contract.Destroy();
return true;
}

public static bool Migrate(byte[] code)
{
Contract.Migrate(code, true, "", "", "", "", "");
return true;
}
}
40 changes: 40 additions & 0 deletions API/DynamicCallOep4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Ont.SmartContract.Framework;
using System;
using System.Numerics;

namespace Contract
{
public class Contract : SmartContract
{
public delegate object OEP4Contract(string method, object[] args);

public static Object Main(string operation, params object[] args)
{
if (operation == "CallOep4Contract")
{
if (args.Length != 4) return false;
byte[] from = (byte[])args[0];
byte[] to = (byte[])args[1];
ulong value = (ulong)args[2];
byte[] hash = (byte[])args[3];
return CallOep4Contract(from, to, value, hash);
}
return false;
}

public static bool CallOep4Contract(byte[] from, byte[] to, ulong value, byte[] contractHash)
{

if (!TransferOEP4(from, to, contractHash, value)) throw new Exception();
return true;
}

private static bool TransferOEP4(byte[] from, byte[] to, byte[] assetID, BigInteger amount)
{
var args = new object[] { from, to, amount };
var contract = (OEP4Contract)assetID.ToDelegate();
if (!(bool)contract("transfer", args)) return false;
return true;
}
}
}
27 changes: 27 additions & 0 deletions API/MapExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using Helper = Ont.SmartContract.Framework.Helper;

public class MapExample : SmartContract
{

public static void Main()
{
Map<string, int> m = new Map<string, int>();
m["k1"] = 100;
m["k2"] = 200;
Runtime.Notify(m["k1"]);
Runtime.Notify(m["k2"]);

// seriallize
byte[] b = Helper.Serialize(m);
Storage.Put(Storage.CurrentContext, "mapexample", b);
byte[] v = Storage.Get(Storage.CurrentContext, "mapexample");

// deserialize
Map<string, int> m2 = (Map<string, int>)Helper.Deserialize(v);
Runtime.Notify(m2["k1"]);
Runtime.Notify(m2["k2"]);
}

}
24 changes: 24 additions & 0 deletions API/RuntimeAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using Ont.SmartContract.Framework.Services.System;

public class RuntimeExample : SmartContract
{
public static void Main()
{
// get current block time
Runtime.Notify(Runtime.Time);

// check the caller authority, it's very useful function for check user authentication
byte[] user = { 248, 142, 51, 220, 214, 177, 110, 235, 27, 218, 59, 86, 23, 47, 140, 20, 114, 119, 159, 152 };
Runtime.CheckWitness(user);

// Notify the message to the blockchain,the arguments is a object array
Runtime.Notify("Hi blockchain");
Runtime.Notify("s1", "s2", 1, 2, 3);

// Runtime.Log() write the log message to the blockchain, the arguments can only be string
Runtime.Log("log message.");

}
}
21 changes: 21 additions & 0 deletions API/StorageExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using System.Numerics;

public class StorageExample : SmartContract
{ public static void Main()
{
// get the storage context
StorageContext context = Storage.CurrentContext;

Storage.Put(context, "key", 100);
BigInteger v = Storage.Get(context, "key").AsBigInteger();
Runtime.Notify(v);

Storage.Put(context, "key".AsByteArray(), "100");
Runtime.Notify(Storage.Get(context,"key".AsByteArray()).AsString());

Storage.Delete(context, "key");
Runtime.Notify(Storage.Get(context, "key".AsByteArray()));
}
}
22 changes: 22 additions & 0 deletions API/StorageMapExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;

namespace CGAS
{
public class StorageExample : SmartContract
{
public static void Main(string method, object[] args)
{
StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset));

asset.Put("key", 100);
Runtime.Notify(asset.Get("key").AsBigInteger());

asset.Get("key");
Runtime.Notify(asset.Get("key").AsBigInteger());

asset.Delete("key");
Runtime.Notify(asset.Get("key").AsBigInteger());
}
}
}
34 changes: 34 additions & 0 deletions API/StructExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using Helper = Ont.SmartContract.Framework.Helper;

public class StructExmple : SmartContract
{

public static void Main()
{
// create struct instance
Person p = new Person();
p.Name = "bob";
p.Age = 20;
Runtime.Notify(p.Name);
Runtime.Notify(p.Age);

// serialize struct instance to byte[]
byte[] b = Helper.Serialize(p);

// deserialize byte[] to struct
Person p2 = (Person)Helper.Deserialize(b);
Runtime.Notify(p2.Name);
Runtime.Notify(p2.Age);

// create struct array
Person[] array = new Person[] { new Person { Name = "tom", Age = 18 }, new Person { Name = "jack", Age = 20 } };
}

struct Person
{
public string Name;
public int Age;
}
}
59 changes: 59 additions & 0 deletions API/ThrowException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using System;
using System.Numerics;

namespace ExceptionTest
{
public class ExceptionTest : SmartContract
{

public static Object Main(string operation, params object[] args)
{
if (operation == "transferMulti")
{
return TransferMulti(args);
}
return false;
}

// smart contract throw excepiton example in multiple transfer
public static bool TransferMulti(object[] args)
{
for(int i=0;i<args.Length;i++)
{
State state = (State)args[i];
if(!Transfer(state.From, state.To, state.Amount)) throw new Exception("Transfer failed.");
}
return true;
}

public static bool Transfer(byte[] from, byte[] to, BigInteger value)
{
if (value <= 0) return false;
if (!Runtime.CheckWitness(from)) return false;
if (to.Length != 20) return false;
if (from == to) return true;

BigInteger from_value = Storage.Get(Storage.CurrentContext, from).AsBigInteger();
if (from_value < value) return false;
if (from_value == value)
Storage.Delete(Storage.CurrentContext, from);
else
Storage.Put(Storage.CurrentContext, from, from_value - value);

BigInteger to_value = Storage.Get(Storage.CurrentContext, to).AsBigInteger();
Storage.Put(Storage.CurrentContext, to, to_value + value);
Runtime.Notify(from, to, value);
return true;
}

public struct State
{
public byte[] From;
public byte[] To;
public BigInteger Amount;
}

}
}
32 changes: 32 additions & 0 deletions Asset/Asset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using System;

namespace Asset{

public class NativeAsset
{
public static readonly byte[] OntContract = "AFmseVrdL9f9oyCzZefL9tG6UbvhUMqNMV".ToScriptHash();
public static readonly byte[] OngContract = "AFmseVrdL9f9oyCzZefL9tG6UbvhfRZMHJ".ToScriptHash();

public static bool RecycleAsset(byte[] from, byte[] to, ulong ont, ulong ong)
{
byte[] ret;
Transfer transfer = new Transfer { From = from, To = to, Value = ont };
ret = Native.Invoke(0, OntContract, "transfer", new object[1] { transfer });
if (ret[0] != 1) throw new Exception("tansfer ont error.") ;

transfer.Value = ong;
ret = Native.Invoke(0, OngContract, "transfer", new object[1] { transfer });
if (ret[0] != 1) throw new Exception("transfer ong error.");
return true;
}

struct Transfer
{
public byte[] From;
public byte[] To;
public ulong Value;
}
}
}
1 change: 1 addition & 0 deletions CrowdSale/Auth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//TODO
24 changes: 24 additions & 0 deletions Crypto/encrypt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Ont.SmartContract.Framework;
using Ont.SmartContract.Framework.Services.Ont;
using System;

namespace Crypto
{
public class CryptoExample : SmartContract
{
public static void Main()
{
/// The input is hashed using SHA-1.
Sha1("123456789".AsByteArray());

/// The input is hashed using SHA-256.
Sha256("123456789".AsByteArray());

/// The input is hashed using Hash160: first with SHA-256 and then with RIPEMD-160.
Hash160("123456789".AsByteArray());

/// The input is hashed using Hash256: twice with SHA-256.
Hash256("123456789".AsByteArray());
}
}
}
1 change: 1 addition & 0 deletions NFT/OEP5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//TODO
Loading