在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kinecosystem/kin-sdk-unity开源软件地址:https://github.com/kinecosystem/kin-sdk-unity开源编程语言:C# 63.9%开源软件介绍:Kin SDK for UnityKin SDK for Unity is responsible for providing access to the Kin native SDKs for managing Kin balance and transactions. Android SetupThe Kin plugin uses the Gradle build system on Android. See the Building with Gradle for Android section of Unity's documentation and the Providing a custom build.gradle template to enable the use of a custom gradle file. Open the ...
allprojects {
repositories {
...
jcenter()
google()
maven { url 'https://jitpack.io' }
}
}
...
dependencies {
...
implementation 'com.github.kinecosystem.kin-sdk-android:kin-sdk-lib:1.0.5'
implementation 'com.github.kinecosystem.kin-sdk-android:kin-backup-and-restore-lib:1.0.5'
**DEPS**}
...
android {
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
...
} iOS SetupPrerequisites:
To use the Kin SDK, after you finish building your game, do the following:
Get StartedConnecting to a service providerCreate a new
An optional parameter is The example below creates a kinClient = new KinClient( Environment.TEST, "1acd" ) Creating and retrieving a KIN accountThe first time you use KinAccount account;
try
{
if( !kinClient.HasAccount() )
account = kinClient.AddAccount();
}
catch( Exception e )
{
Debug.LogError( e );
} Calling if( kinClient.HasAccount() )
account = kinClient.GetAccount( 0 ); You can delete your account from the device using kinClient.DeleteAccount( int index ); OnboardingBefore an account can be used on the configured network, it must be created on the blockchain and funded with the native network asset. This step must be performed by a service, see the Kin SDK documentation for details. When working with the Kin test servers, you can use the friendbot service (see the Account InformationPublic AddressYour account can be identified via its public address. To retrieve the account public address use: account.GetPublicAddress(); Query Account StatusCurrent account status on the blockchain can be queried using
account.GetStatus( ( ex, status ) =>
{
if( ex == null )
Debug.Log( "Account status: " + status );
else
Debug.LogError( "Get Account Status Failed. " + ex );
}); Retrieving BalanceTo retrieve the balance of your account in KIN call the account.GetBalance( ( ex, balance ) =>
{
if( ex == null )
Debug.Log( "Balance: " + balance );
else
Debug.LogError( "Get Balance Failed. " + ex );
}); TransactionsTransferring Kin to another accountTo transfer Kin to another account, you need the public address of the account you want to transfer the KIN to. Also if your app is not in the Kin whitelist then you need to also apply a fee. Amount of 1 fee equals to 1/100000 KIN. If you are in the whitelist then look after the next example to see how you can send a whitelist transaction. The following code will transfer 20 Kin to the recipient account "GDIRGGTBE3H4CUIHNIFZGUECGFQ5MBGIZTPWGUHPIEVOOHFHSCAGMEHO". var toAddress = "GDIRGGTBE3H4CUIHNIFZGUECGFQ5MBGIZTPWGUHPIEVOOHFHSCAGMEHO";
var amountInKin = 20;
var fee = 100;
// we could use here some custom fee or we can can call the blockchain in order to retrieve
// the current minimum fee by calling kinClient.getMinimumFee(). Then when you get the minimum
// fee returned and you can start the 'send transaction flow' with this fee.
account.BuildTransaction( toAddress, amountInKin, fee, ( ex, transaction ) =>
{
if( ex == null )
{
// Here we already got a Transaction object before actually sending the transaction. This means
// that we can, for example, send the transaction id to our servers or save it locally
// in order to use it later. For example if we lose network just after sending
// the transaction then we will not know what happened with this transaction.
// So when the network is back we can check what is the status of this transaction.
Debug.Log( "Build Transaction result: " + transaction );
account.SendTransaction( transaction, ( ex, transactionId ) =>
{
if( ex == null )
Debug.Log( "Send Transaction result: " + transactionId );
else
Debug.LogError( "Send Transaction Failed. " + ex );
});
}
else
{
Debug.LogError( "Build Transaction Failed. " + ex );
}
}); Transferring Kin to another account using whitelist serviceThe flow is very similar to the above code but here there is a middle stage in which you get the whitelistable transaction details from the 'Transaction' object just after you build the transaction and you send it to the whitelist service (which is hosted on your own servers). Then you use the method 'sendWhitelistTransaction( string whitelist )' where the parameter account.BuildTransaction( toAddress, amountInKin, fee, ( ex, transaction ) =>
{
if( ex == null )
{
Debug.Log( "Build Transaction result: " + transaction );
var whitelistTransaction = YourWhitelistService.WhitelistTransaction( transaction );
account.SendWhitelistTransaction( transaction.Id, whitelistTransaction, ( ex, transactionId ) =>
{
if( ex == null )
Debug.Log( "Send Transaction result: " + transactionId );
else
Debug.LogError( "Send Transaction Failed. " + ex );
});
}
else
{
Debug.LogError( "Build Transaction Failed. " + ex );
}
}); MemoArbitrary data can be added to a transfer operation using the memo parameter, the memo can contain a utf-8 string up to 21 bytes in length. A typical usage is to include an order number that a service can use to verify payment. var memo = "arbitrary data";
account.BuildTransaction( toAddress, amountInKin, fee, memo, ( ex, transaction ) =>
{
if( ex == null )
{
// Here we already got a Transaction object before actually sending the transaction. This means
// that we can, for example, send the transaction id to our servers or save it locally
// in order to use it later. For example if we lose network just after sending
// the transaction then we will not know what happened with this transaction.
// So when the network is back we can check what is the status of this transaction.
Debug.Log( "Build Transaction result: " + transaction );
account.SendTransaction( transaction, ( ex, transactionId ) =>
{
if( ex == null )
Debug.Log( "Send Transaction result: " + transactionId );
else
Debug.LogError( "Send Transaction Failed. " + ex );
});
}
else
{
Debug.LogError( "Build Transaction Failed. " + ex );
}
}); account.SendTransaction( toAddress, amountInKin, memo, ( ex, transactionId ) => { if( ex == null ) Debug.Log( "Send Transaction: " + transactionId ); else Debug.LogError( "Send Transaction Failed. " + ex ); });
Listening to balance changesAccount balance changes, can be observed by adding balance listener: account.AddBalanceListener( this );
...
public void OnEvent( decimal balance )
{
Debug.Log( "On Balance: " + balance );
} Listening to account creationAccount creation on the blockchain network, can be observed, by adding create account listener: account.AddAccountCreationListener( this );
...
public void OnEvent()
{
Debug.Log( "On Account Created" );
} To unregister any listener use Account Backup & RestoreThe SDK comes with a built-in module that provides an easy way to back up and restore an account. The module's UI includes two flows, Backup and Restore. The UI wraps the native SDK's import and export functionalities, on which these flows are based. The UI uses a password to create a QR code, which is then used to back up the account and to restore it. BackupTo back up an account, all you need to do is call the account.BackupAccount(_client,
(KinException ex, BackupRestoreResult result) => {
switch (result)
{
case BackupRestoreResult.Success:
Debug.Log("Account backed up successfully");
break;
case BackupRestoreResult.Cancel:
Debug.Log("Account backup canceled");
break;
case BackupRestoreResult.Failed:
Debug.Log("Account backup failed");
Debug.LogError(ex);
break; RestoreTo restore a Kin account, you need to call the client.RestoreAccount(
(KinException ex, BackupRestoreResult result, KinAccount account) => {
switch (result)
{
case BackupRestoreResult.Success:
Debug.Log("Account successfully restored");
// Save the restored account
MyAccount = account;
break;
case BackupRestoreResult.Cancel:
Debug.Log("Account restoration canceled");
break;
case BackupRestoreResult.Failed:
Debug.Log("Account restoration failed");
Debug.LogError(ex);
break;
} Please note that the SDK launches a separate native activity to perform the backup/restore process. Import & ExportIf you wish to import and export an account without the UI that the previous methods provided, you can use the following: To export: exportedAccountJson = account.Export( importExportPassphrase ); To import: account = client.ImportAccount( exportedAccountJson, importExportPassphrase ); Error HandlingThe Kin Unity Plugin wraps Kin native exceptions in the C# KinException class. It provides a Common Errors
Demo SceneThe demo scene included with the Kin Unity Plugin covers the functionality of the plugin, and serves as a detailed example on how to use the it. ContributingPlease review our CONTRIBUTING.md guide before opening issues and pull requests. LicenseThe kin-unity-plugin is licensed under Kin Ecosystem SDK License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论