How to call Web3 Cloud API
Unity SDK also provides access to the Web3 Cloud API. A configuration can be passed in when initializing to change the JSON-RPC Endpoint, set your API Key, and API Secret.
Create Web3 Cloud Client
You can create a Client to connect to Web3 Cloud through Gateway Service.
using ClientSdk = jp.co.ginco.ClientSdk;
ClientSdk.EthereumGatewayClient client = new ClientSdk.EthereumGatewayClient(string url, string apiKey, string apiSecret);
Wallet
Register Wallet
Here is an example to call the RegisterWallet API
.
await client.RegisterWallet(string name, string address, (result, error) =>
{
if (error != null)
{
UnityEngine.Debug.Log(string.Format("{0}: {1}", error.ErrorCode, error.Message));
}
else
{
// output: {"walletId": "xxxxx"}
UnityEngine.Debug.Log(result);
}
});
Get Wallet
Here is an example to call the GetWallet API
.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
await client.GetWalletByWalletId(string walletId, (result, error) =>
{
if (error != null)
{
UnityEngine.Debug.Log(string.Format("{0}: {1}", error.ErrorCode, error.Message));
}
else
{
// output: {"wallet":{"walletId":"xxxx", "name":"xxxx", "address":"xxxx", "createTime":"xxxx", "updateTime":"xxxxx"}}
UnityEngine.Debug.Log(result);
// parse result
JObject obj = JObject.Parse(result);
if (obj["wallet"] != null)
{
JObject walletJson = (JObject)obj["wallet"];
// This is for conversion from Sytem.String to Google.Protobuf.WellKnownTypes.Timestamp
var settings = new JsonSerializerSettings
{
ContractResolver = new TimeStampContractResolver();
};
Gincoinc.Web3cloud.Ethereum.GatewayClient.V1.Wallet wallet = JsonConvert.DeserializeObject<Gincoinc.Web3cloud.Ethereum.GatewayClient.V1.Wallet>(walletJson.ToString(), settings);
}
}
});
Transaction
Create Transaction
Here is an example to call the CreateTransaction API
for transfering native assets, which returns Transaction Id.
await client.CreateTransaction(
string walletId,
string fromAddress,
string toAddress,
string amount,
string gas,
string maxFeePerGas,
string maxPriorityFeePerGas,
string data,
ulong nonce,
(result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Here is an example to call the CreateTokenTransaction API
for transfering ERC20 assets, which returns Transaction Id.
Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload tokenPayload = new Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload
{
Erc20 = new Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload.Types.ERC20
{
ContractAddress = "contractAddress",
Value = "amount"
}
};
await client.CreateTokenTransaction(
string walletId,
Gincoinc.Web3cloud.Ethereum.Global.V1.ContractType.Erc20,
string fromAddress,
string toAddress,
string gas,
string maxFeePerGas,
string maxPriorityFeePerGas,
ulong nonce,
tokenPayload,
(result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Here is an example to call the CreateTokenTransaction API
for transfering ERC721 assets, which returns Transaction Id.
Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload tokenPayload = new Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload
{
Erc721 = new Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload.Types.ERC721
{
ContractAddress = "contract address",
TokenId = "token id"
}
};
await client.CreateTokenTransaction(
string walletId,
Gincoinc.Web3cloud.Ethereum.Global.V1.ContractType.Erc721,
string fromAddress,
string toAddress,
string gas,
string maxFeePerGas,
string maxPriorityFeePerGas,
ulong nonce,
tokenPayload,
(result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Here is an example to call the CreateTokenTransaction API
for transfering ERC1155 assets, which returns Transaction Id.
Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload tokenPayload = new Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload
{
Erc1155 = new Gincoinc.Web3cloud.Ethereum.Global.V1.TokenPayload.Types.ERC1155
{
ContractAddress = "contract address",
TokenId = "token id",
Value = "amount"
}
};
await client.CreateTokenTransaction(
string walletId,
Gincoinc.Web3cloud.Ethereum.Global.V1.ContractType.Erc1155,
string fromAddress,
string toAddress,
string gas,
string maxFeePerGas,
string maxPriorityFeePerGas,
ulong nonce,
tokenPayload,
(result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Send Transaction
Here is an example to call the SendTransaction API
for sending the signed transaction by Transaction Id, which returns Transaction Hash.
await client.SendTransaction(string txId, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Get Transaction
Here is an example to call the GetTransaction API
for obraning the transaction status by Transaction Id.
await client.GetTransactionByTransactionId(string txId, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Cancel Transaction
Here is an example to call the CancelTransaction API
for canceling the transaction status by Transaction Id.
await client.CancelTransaction(string txId, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
}
Asset
ListAssetsByAddress
Here is an example to call the ListAssets API
for obtaining your Assets by Wallet Address
.
await client.ListAssetsByAddress(string address, bool isAggregated, string pageToken, bool excludeZeroBalance, Gincoinc.Web3cloud.Ethereum.Global.V1.AssetType.Eth, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Transfer
ListTransfersByAddress
Here is an example to call the ListTransfers API
for obtaining your Transfer history by Wallet Address
.
await client.ListTransfersByAddress(string address, ulong startTime, ulong endTime, uint pageSize, string pageToken, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Contract
RegisterContract
Here is an example to call the RegisterContract API
for registering your Contract on Web3Cloud by Contract Address and Contract Type (ERC20, ERC721, or ERC1155)
.
await client.RegisterContract(string contractAddress, Gincoinc.Web3cloud.Ethereum.Global.V1.ContractType.Erc20, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
RemoveContract
Here is an example to call the RemoveContract API
for removing your Contract by Contract Address.
await client.RemoveContract(string contractAddress, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Webhook
RegisterWebhook
Here is an example to call the RegisterWebhook API
for registering your webhook endpoint by the endpoint URL and Block Confirmation Number, which returns webhookId
.
await client.RegisterWebhook(string walletId, string endpoint, ulong confiramtion, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
RemoveWebhook
Here is an example to call the RemoveWebhook API
for removing your webhook endpoint by webhookId
.
await client.RemoveWebhook(string webhookId, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
});
Handle cancellation
If there are cases where http communication takes a long time, you can pass a CancellationTokenSource token at the time of request and cancel the communication at any time.
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMilliseconds(10000));
// To be cancelled after 10 seconds
await client.GetWallet(string walletId, (result, error) =>
{
if (error != null)
{
...
}
else
{
...
}
}, cts.Token);
Interface is still being actively developed and might be subject to change