Transferring an asset
So now, let’s imagine I sent you some BlockchainProgramming Coins. How can you send me back the coins? You need to build a ColoredCoin.
In the sample above, let’s say I want to spend the 10 assets I received on the address “nico”. Here is the coin I want to spend:
1
{
2
"transactionId": "fa6db7a2e478f3a8a0d1a77456ca5c9fa593e49fd0cf65c7e349e5a4cbe58842",
3
"index": 0,
4
"value": 600,
5
"scriptPubKey": "76a914356facdac5f5bcae995d13e667bb5864fd1e7d5988ac",
6
"redeemScript": null,
7
"assetId": "AVAVfLSb1KZf9tJzrUVpktjxKUXGxUTD4e",
8
"quantity": 10
9
}
Copied!
Here is how to instantiate such Colored Coin in code:
1
var coin = new Coin(
2
fromTxHash: new uint256("fa6db7a2e478f3a8a0d1a77456ca5c9fa593e49fd0cf65c7e349e5a4cbe58842"),
3
fromOutputIndex: 0,
4
amount: Money.Satoshis(600),
5
scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914356facdac5f5bcae995d13e667bb5864fd1e7d5988ac")));
6
BitcoinAssetId assetId = new BitcoinAssetId("AVAVfLSb1KZf9tJzrUVpktjxKUXGxUTD4e");
7
ColoredCoin colored = coin.ToColoredCoin(assetId, 10);
Copied!
We will show you later how you can use some web services or custom code to get the coins more easily. I also needed another coin (forFees), to pay the fees. The asset transfer is actually very easy with the TransactionBuilder.
1
var book = BitcoinAddress.Create("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");
2
var nicoSecret = new BitcoinSecret("??????????", Network.Mainnet);
3
var nico = nicoSecret.GetAddress(); //15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe
4
5
var forFees = new Coin(
6
fromTxHash: new uint256("7f296e96ec3525511b836ace0377a9fbb723a47bdfb07c6bc3a6f2a0c23eba26"),
7
fromOutputIndex: 0,
8
amount: Money.Satoshis(4425000),
9
scriptPubKey: new Script(Encoders.Hex.DecodeData("76a914356facdac5f5bcae995d13e667bb5864fd1e7d5988ac")));
10
11
var builder = Network.Main.CreateTransactionBuilder();
12
var tx = builder
13
.AddKeys(nicoSecret)
14
.AddCoins(colored, forFees)
15
.SendAsset(book, new AssetMoney(assetId, 10))
16
.SetChange(nico)
17
.SendFees(Money.Coins(0.0001m))
18
.BuildTransaction(true);
19
Console.WriteLine(tx);
Copied!
1
{
2
.
3
"out": [
4
{
5
"value": "0.00000000",
6
"scriptPubKey": "OP_RETURN 4f410100010a00"
7
},
8
{
9
"value": "0.00000600",
10
"scriptPubKey": "OP_DUP OP_HASH160 c81e8e7b7ffca043b088a992795b15887c961592 OP_EQUALVERIFY OP_CHECKSIG"
11
},
12
{
13
"value": "0.04415000",
14
"scriptPubKey": "OP_DUP OP_HASH160 356facdac5f5bcae995d13e667bb5864fd1e7d59 OP_EQUALVERIFY OP_CHECKSIG"
15
}
16
]
17
}
Copied!
Which basically succeed:
Last modified 2yr ago
Copy link