Last active
May 5, 2020 08:49
-
-
Save kyranjamie/bfd2a0f2717ea4685ce5010b82e72b15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as BigNum from 'bn.js'; | |
import * as qs from 'qs'; | |
import axios from 'axios'; | |
import { | |
makeSTXTokenTransfer, | |
StacksNetwork, | |
TransactionVersion, | |
ChainID, | |
getPublicKey, | |
createStacksPrivateKey, | |
addressFromPublicKeys, | |
AddressHashMode, | |
broadcastTransaction, | |
TokenTransferOptions, | |
makeStandardSTXPostCondition, | |
} from '@blockstack/stacks-transactions'; | |
import { c32address } from 'c32check'; | |
import { FungibleConditionCode, AddressVersion } from '../src/constants'; | |
const localNetwork: StacksNetwork = { | |
version: TransactionVersion.Testnet, | |
chainId: ChainID.Testnet, | |
coreApiUrl: 'http://localhost:3999', | |
broadcastApiUrl: 'http://localhost:3999/v2/transactions', | |
transferFeeEstimateApiUrl: 'http://localhost:3999/v2/fees/transfer', | |
balanceApiUrl: 'http://localhost:3999/v2/accounts', | |
}; | |
async function getTestnetStacks(address: string) { | |
await axios({ | |
url: 'http://localhost:3999/sidecar/v1/debug/faucet', | |
headers: { 'content-type': 'application/x-www-form-urlencoded' }, | |
data: qs.stringify({ address }), | |
method: 'POST', | |
}); | |
} | |
function delay(ms: number) { | |
return new Promise(resolve => setTimeout(() => resolve(), ms)); | |
} | |
(async () => { | |
// result of `blockstack-cli make_keychain -t | |
// can also use `makeRandomPrivKey()` | |
const key = 'abc07c0560ccc9d4606f86abc5fda761e1372f154266c007ef617ce4fde8eba501'; | |
const privateKey = createStacksPrivateKey(key); | |
const publicKey = getPublicKey(privateKey); | |
const stxAddress = addressFromPublicKeys( | |
AddressVersion.TestnetSingleSig, | |
AddressHashMode.SerializeP2PKH, | |
1, | |
[publicKey] | |
); | |
const address = c32address(stxAddress.version, stxAddress.hash160); | |
console.log(address); | |
console.assert( | |
c32address(stxAddress.version, stxAddress.hash160) === | |
'ST3TYD6PWK9621JBT15T5ZYJ70EBSTXSFZ8T7X6YW' | |
); | |
await getTestnetStacks(address); | |
// wait for a new block | |
await delay(4000); | |
const recipients = [ | |
'ST3JVGCJ0ER79Q5AKHD08JK2R1TX2GY10QCV9SQSD', | |
// 'ST3Z3ZDG4VPSTDH380X5BF5F225BV85B9AAN3S2T8', | |
// 'ST31E69TCPFMRJWE3MZNZ8ERS2AQDAPTQQ8GRASMX', | |
]; | |
for await (let recipient of recipients) { | |
const txConfig: TokenTransferOptions = { | |
recipient, | |
network: localNetwork, | |
memo: 'test memo', | |
amount: new BigNum(10), | |
senderKey: key, | |
nonce: new BigNum(1), | |
fee: new BigNum(300), | |
postConditions: [ | |
makeStandardSTXPostCondition(address, FungibleConditionCode.Greater, new BigNum(0.0000001)), | |
], | |
}; | |
const transaction = await makeSTXTokenTransfer(txConfig); | |
console.log({ transaction }); | |
const sentTx = await broadcastTransaction(transaction, localNetwork); | |
console.log(JSON.parse(sentTx)); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment