CityCoins
Search…
Mining
Examples of CityCoin contract functions related to mining.

Mining a Single Block

Requires:
  • @stacks/network
  • @stacks/transactions
  • @stacks/connect-react
1
// example: mining a single block with 10 STX
2
3
const NETWORK = new StacksMainnet(); // set network from @stacks/network
4
const { doContractCall } = useConnect(); // hook for Stacks Connect
5
6
let totalUstx = 10000000; // amount of uSTX to spend in block
7
let totalUstxCV = uintCV(totalUstx);
8
9
let memoCV = someCV(bufferCVFromString('an optional memo'));
10
// alternate if no memo:
11
// let memoCV = noneCV();
12
13
try {
14
await doContractCall({
15
contractAddress: 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27',
16
contractName: 'miamicoin-core-v1',
17
functionName: 'mine-tokens',
18
functionArgs: [totalUstxCV, memoCV],
19
postConditionMode: PostConditionMode.Deny,
20
postConditions: [
21
makeStandardSTXPostCondition(
22
ownerStxAddress,
23
FungibleConditionCode.Equal,
24
totalUstxCV.value
25
),
26
],
27
network: NETWORK,
28
onCancel: () => {
29
// what to do if tx is canceled / window is closed
30
console.log('Transaction canceled, please try again');
31
},
32
onFinish: result => {
33
// what to if tx is successfully broadcasted
34
console.log(`Transaction successfully broadcasted:\n${result.txId}`);
35
},
36
});
37
} catch (err) {
38
console.log(`Error: ${err.message}`);
39
}
Copied!

Mining for Multiple Blocks

Requires:
  • @stacks/network
  • @stacks/transactions
  • @stacks/connect-react
1
// example: mining 10 blocks with 5, 10, and 15 STX per block
2
3
const NETWORK = new StacksMainnet(); // set network from @stacks/network
4
const { doContractCall } = useConnect(); // hook for Stacks Connect
5
6
// initialize variables
7
let commitsUstx = [5000000, 10000000, 15000000, 5000000, 10000000, 15000000, 5000000, 10000000, 15000000, 5000000];
8
let totalUstx = 0;
9
let totalUstxCV = uintCV(0);
10
let mineManyArray = [];
11
let mineManyArrayCV = listCV([]);
12
13
// set up contract submission data
14
for (let i = 0; i < commits.length; i++) {
15
mineManyArray.push(uintCV(amount));
16
totalUstx += amount;
17
}
18
totalUstxCV = uintCV(totalUstx);
19
mineManyArrayCV = listCV(mineManyArray);
20
21
try {
22
await doContractCall({
23
contractAddress: 'SP466FNC0P7JWTNM2R9T199QRZN1MYEDTAR0KP27',
24
contractName: 'miamicoin-core-v1',
25
functionName: 'mine-many',
26
functionArgs: [mineManyArrayCV],
27
postConditionMode: PostConditionMode.Deny,
28
postConditions: [
29
makeStandardSTXPostCondition(
30
ownerStxAddress,
31
FungibleConditionCode.Equal,
32
totalUstxCV.value
33
),
34
],
35
network: NETWORK,
36
onCancel: () => {
37
// what to do if tx is canceled / window is closed
38
console.log('Transaction canceled, please try again');
39
},
40
onFinish: result => {
41
// what to if tx is successfully broadcasted
42
console.log(`Transaction successfully broadcasted:\n${result.txId}`);
43
},
44
});
45
} catch (err) {
46
console.log(`Error: ${err.message}`);
47
}
Copied!
Last modified 1mo ago