Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update workshop to v1 of SDK #330

Merged
merged 19 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions code_examples/sdk_examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,36 @@
"test": "ts-node src/test.ts"
},
"dependencies": {
"@kiltprotocol/sdk-js": "0.35.0",
"axios": "^1.5.1",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"web3": "^4.1.2"
"@kiltprotocol/sdk-js": "1.0.0",
"@kiltprotocol/chain-helpers": "^0.100.0",
"@kiltprotocol/credentials": "^0.100.0",
"@kiltprotocol/utils":"^0.100.0",
"@kiltprotocol/types":"^0.100.0",
"axios": "^1.7.5",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"web3": "^4.12.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
},
"devDependencies": {
"@polkadot/types": "^10.4.0",
"@types/node": "^20.8.6",
"@types/node-fetch": "^2.6.6",
"@typescript-eslint/eslint-plugin": "^5.36.0",
"@typescript-eslint/parser": "^6.8.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"@polkadot/types": "^12.4.2",
"@polkadot/util": "^13.0.2",
"@polkadot/util-crypto": "^13.1.1",
"@polkadot/keyring": "^13.0.2",
"@types/node": "^22.5.1",
"@types/node-fetch": "^2.6.11",
"@typescript-eslint/eslint-plugin": "^8.3.0",
"@typescript-eslint/parser": "^8.3.0",
"eslint": "^9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-formatter-codeframe": "^7.32.1",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^5.0.1",
"node-fetch": "^2.6.7",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"eslint-plugin-prettier": "^5.2.1",
"node-fetch": "^3.3.2",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
}
77 changes: 18 additions & 59 deletions code_examples/sdk_examples/src/getFunds.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,27 @@
import * as Kilt from '@kiltprotocol/sdk-js'
import { BalanceUtils, Blockchain } from '@kiltprotocol/chain-helpers'
import type {
KiltAddress,
MultibaseKeyPair,
TransactionSigner
} from '@kiltprotocol/types'
import { Multikey } from '@kiltprotocol/utils'
import { BN } from '@polkadot/util'
import { setTimeout } from 'timers/promises'

async function failproofSubmit(
tx: Kilt.SubmittableExtrinsic,
submitter: Kilt.KeyringPair
) {
try {
await Kilt.Blockchain.signAndSubmitTx(tx, submitter)
} catch {
// Try a second time after a small delay and fetching the right nonce.
const waitingTime = 12_000 // 12 seconds
console.log(
`First submission failed for workshop. Waiting ${waitingTime} ms before retrying.`
)
await setTimeout(waitingTime)
console.log('Retrying...')
// nonce: -1 tells the client to fetch the latest nonce by also checking the tx pool.
const resignedBatchTx = await tx.signAsync(submitter, { nonce: -1 })
await Kilt.Blockchain.submitSignedTx(resignedBatchTx)
}
}

// TODO: Look into tidier way with PD Keyring…
ChrisChinchilla marked this conversation as resolved.
Show resolved Hide resolved
export async function getFunds(
faucetAccount: Kilt.KeyringPair,
recipient: Kilt.KiltAddress,
faucetAccount: MultibaseKeyPair,
recipient: MultibaseKeyPair,
kiltAmount: number
) {
const api = Kilt.ConfigService.get('api')
const tx = api.tx.balances.transfer(
recipient,
Kilt.BalanceUtils.convertToTxUnit(new BN(kiltAmount), 0)
)
await failproofSubmit(tx, faucetAccount)
console.log('Successfully transferred tokens')
}
const api = Kilt.ConfigService.get("api")

export async function endowAccounts(
faucetAccount: Kilt.KeyringPair,
destinationAccounts: Kilt.KiltAddress[],
amount: BN
): Promise<void> {
const api = Kilt.ConfigService.get('api')

const transferBatch = destinationAccounts.map((acc) =>
api.tx.balances.transfer(
acc,
Kilt.BalanceUtils.convertToTxUnit(
Kilt.BalanceUtils.KILT_COIN.mul(amount),
0
)
)
)

console.log(
`Endowing test accounts "${destinationAccounts}"
from faucet "${faucetAccount.address}"
with ${Kilt.BalanceUtils.formatKiltBalance(amount, {
decimals: 0
})} each...`
const tx = api.tx.balances.transferAllowDeath(
Multikey.decodeMultibaseKeypair(recipient).publicKey,
BalanceUtils.convertToTxUnit(new BN(kiltAmount), 0)
)
const batchTx = api.tx.utility.batchAll(transferBatch)

await failproofSubmit(batchTx, faucetAccount)

console.log('Successfully transferred tokens')
const [faucetSigner] = await Kilt.getSignersForKeypair<KiltAddress>({
keypair: faucetAccount
})
await Blockchain.signAndSubmitTx(tx, <TransactionSigner>faucetSigner)
}
12 changes: 0 additions & 12 deletions code_examples/sdk_examples/src/workshop/holder/createClaim.ts

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions code_examples/sdk_examples/src/workshop/holder/generateAccount.ts

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions code_examples/sdk_examples/src/workshop/holder/generateKeypairs.ts

This file was deleted.

33 changes: 0 additions & 33 deletions code_examples/sdk_examples/src/workshop/holder/generateLightDid.ts

This file was deleted.

Loading
Loading