-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: auto-deploy testnet contracts with github actions
- Loading branch information
Showing
9 changed files
with
312 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: deploy-contracts | ||
on: | ||
schedule: | ||
- cron: "*/15 * * * *" | ||
workflow_dispatch: | ||
|
||
jobs: | ||
deploy-contracts: | ||
runs-on: ubuntu-latest | ||
env: | ||
API_SERVER: https://stacks-node-api.blockstack.org | ||
CONTRACT_PRIVATE_KEY: ${{ secrets.CONTRACT_PRIVATE_KEY }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Set Node Version | ||
uses: actions/setup-node@v2-beta | ||
with: | ||
node-version: 12.16.1 | ||
- name: Restore lerna cache | ||
id: lerna-cache | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
node_modules | ||
*/*/node_modules | ||
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }} | ||
- name: Install monorepo deps | ||
run: yarn --frozen-lockfile | ||
if: steps.lerna-cache.outputs.cache-hit != 'true' | ||
- name: Deploy contracts | ||
run: yarn deploy-contracts |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
;; A component with a function that accepts all argument types. | ||
;; Use for testing. | ||
|
||
(define-public | ||
(rawr | ||
(arg-uint uint) | ||
(arg-int int) | ||
(arg-buff (buff 20)) | ||
(arg-string-ascii (string-ascii 20)) | ||
(arg-string-utf8 (string-utf8 20)) | ||
(arg-principal principal) | ||
(arg-bool bool) | ||
) | ||
(ok u0) | ||
) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
(define-fungible-token connect-token) | ||
(begin (ft-mint? connect-token u10000000 tx-sender)) | ||
|
||
(define-public (transfer | ||
(recipient principal) | ||
(amount uint) | ||
) | ||
(ok (ft-transfer? connect-token amount tx-sender recipient)) | ||
) | ||
|
||
(define-public (faucet) | ||
(ok (ft-mint? connect-token u100 tx-sender)) | ||
) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { readFile as readFileFn } from 'fs'; | ||
import { promisify } from 'util'; | ||
import { RPCClient } from '@stacks/rpc-client'; | ||
import { | ||
getAddressFromPrivateKey, | ||
TransactionVersion, | ||
makeContractDeploy, | ||
StacksTestnet, | ||
} from '@blockstack/stacks-transactions'; | ||
import * as BN from 'bn.js'; | ||
import * as dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
const readFile = promisify(readFileFn); | ||
|
||
interface Contract { | ||
name: string; | ||
file?: string; | ||
} | ||
|
||
const contracts: Contract[] = [ | ||
{ | ||
name: 'connect-token', | ||
file: 'token', | ||
}, | ||
{ | ||
name: 'counter', | ||
}, | ||
{ | ||
name: 'faker', | ||
}, | ||
{ | ||
name: 'status-2', | ||
file: 'status', | ||
}, | ||
]; | ||
|
||
const rpcClient = new RPCClient(process.env.API_SERVER || 'http://localhost:3999'); | ||
const privateKey = process.env.CONTRACT_PRIVATE_KEY; | ||
if (!privateKey) { | ||
console.error('Provide a private key with `process.env.CONTRACT_PRIVATE_KEY`'); | ||
process.exit(1); | ||
} | ||
const address = getAddressFromPrivateKey(privateKey, TransactionVersion.Testnet); | ||
|
||
const network = new StacksTestnet(); | ||
network.coreApiUrl = rpcClient.url; | ||
|
||
const run = async () => { | ||
const account = await rpcClient.fetchAccount(address); | ||
console.log(`Account balance: ${account.balance.toString()} mSTX`); | ||
console.log(`Account nonce: ${account.nonce}`); | ||
|
||
const txResults: string[] = []; | ||
let index = 0; | ||
for (const contract of contracts) { | ||
let exists: boolean; | ||
const contractId = `${address}.${contract.name}`; | ||
try { | ||
await rpcClient.fetchContractInterface({ | ||
contractAddress: address, | ||
contractName: contract.name, | ||
}); | ||
exists = true; | ||
} catch (error) { | ||
// console.error(error); | ||
exists = false; | ||
} | ||
if (exists) { | ||
console.log(`Contract ${contractId} already exists.`); | ||
continue; | ||
} | ||
|
||
console.log(`Deploying ${contractId}`); | ||
|
||
const source = await readFile(`./contracts/${contract.file || contract.name}.clar`); | ||
const tx = await makeContractDeploy({ | ||
contractName: contract.name, | ||
codeBody: source.toString('utf8'), | ||
senderKey: privateKey, | ||
nonce: new BN(account.nonce + index, 10), | ||
network, | ||
}); | ||
|
||
const result = await rpcClient.broadcastTX(tx.serialize()); | ||
if (result.ok) { | ||
index += 1; | ||
txResults.push((await result.text()).replace(/"/g, '')); | ||
} else { | ||
const errorMsg = await result.text(); | ||
throw new Error(errorMsg); | ||
} | ||
} | ||
|
||
if (txResults.length > 0) console.log('Broadcasted transactions:'); | ||
txResults.forEach(txId => { | ||
console.log(`${rpcClient.url}/extended/v1/tx/0x${txId}`); | ||
}); | ||
}; | ||
|
||
run() | ||
.then(() => { | ||
console.log('Finished successfully.'); | ||
process.exit(); | ||
}) | ||
.catch(error => { | ||
console.error('Error while running:'); | ||
console.error(error); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.