-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
113 lines (100 loc) · 4.3 KB
/
Copy pathmain.js
File metadata and controls
113 lines (100 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Include connections
const BigchainDB = require('bigchaindb-driver')
const API_PATH = 'https://test.ipdb.io/api/v1/'
const conn = new BigchainDB.Connection(API_PATH)
// instead of 10k tokens, i made 10m tokens
const nTokens = 10000000
// init the basics
let tokensLeft
const tokenCreator = new BigchainDB.Ed25519Keypair(bip39.mnemonicToSeed('seedPhrase').slice(0,32))
let createTxId
//Launches the token in the network
function tokenLaunch() {
// Construct a transaction payload, wrapping it
const tx = BigchainDB.Transaction.makeCreateTransaction({
token: 'TT (tutorial Tokens)',
number_tokens: nTokens
},
// Metadata field, contains information about the transaction itself
// Can be 'null' if not needed
{
datetime: new Date().toDateString()
},
// Output: Divisible asset, include nTokens as parameter
[BigchainDB.Transaction.makeOutput(BigchainDB.Transaction.makeEd25519Condition(tokenCreator.publicKey), nTokens.toString())],
tokenCreator.publicKey
)
// Sign the transaction with the private key of the token creator
const txSigned = BigchainDB.Transaction.signTransaction(tx, tokenCreator.privateKey)
// Send the transaction off to bigchaindb
conn.postTransactionCommit(txSigned).then(res => {
createTxId = res.id
tokensLeft = nTokens
document.body.innerHTML = '<h3>Transaction created successfuly</h3>';
//txSigned.id corresponds to the asset id of the tokens
document.body.innerHTML += txSigned.id
})
}
// Distribution of tokens
// Sending 2000 tokens to a test user
const amountToSend = 2000
const newUser = new BigchainDB.Ed25519Keypair(bip39.mnemonicToSeed('newUserseedPhrase').slice(0, 32))
// Actual function that transfers the tokens
function transferTokens() {
// User who will receive the 2000 tokens
const newUser = new BigchainDB.Ed25519Keypair()
// Search outputs of the transactions belonging the token creator
// False argument to retrieve unspent outputs
conn.getTransaction(createTxId).then((txOutputs) => {
// Create transfer transaction
const createTranfer = BigchainDB.Transaction.makeTransferTransaction(
[{
tx: txOutputs,
output_index: 0
}],
// Transaction output: Two outputs, because the whole input
// must be spent
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(tokenCreator.publicKey),
(tokensLeft - amountToSend).toString()),
BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(newUser.publicKey),
amountToSend)
],
// Metadata (optional)
{
transfer_to: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
tokens_left: tokensLeft
}
)
// Sign the transaction with the tokenCreator key
const signedTransfer = BigchainDB.Transaction.signTransaction(createTranfer, tokenCreator.privateKey)
return conn.postTransactionCommit(signedTransfer)
}).then(res => {
// Update tokensLeft
tokensLeft -= amountToSend
document.body.innerHTML += '<h3>Transfer transaction created</h3>'
document.body.innerHTML += res.id
})
}
// Combination of different BigChainDB transactions
const bestFriend = new driver.Ed25519Keypair()
function combineTokens(transaction1, outputIndex1, transaction2, outputIndex2, totalTokens) {
const combineTranfer = BigchainDB.Transaction.makeTransferTransaction(
[{
tx: transaction1,
output_index: outputIndex1
}, {
tx: transaction2,
output_index: outputIndex2
}],
// Output
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(bestFriend.publicKey),(totalTokens).toString())], {
transfer_to: 'my best friend'
}
)
// Sign the transaction with the newUser key
const signedTransfer = BigchainDB.Transaction.signTransaction(combineTranfer, newUser.privateKey)
return conn.postTransactionCommit(signedTransfer)
}