@bandprotocol/bandchain.js
Version:
Library for interacting with BandChain in browser and Node.js environments
61 lines (50 loc) • 1.73 kB
text/typescript
import { Client, Wallet, Obi, Message, Coin, Transaction, Fee } from '../../lib'
const grpcEndpoint = 'https://laozi-testnet6.bandchain.org/grpc-web'
const client = new Client(grpcEndpoint)
export async function exampleSendBlockTransaction() {
// Step 1: Import a private key for signing transaction
const { PrivateKey } = Wallet
const mnemonic = 'test'
const privateKey = PrivateKey.fromMnemonic(mnemonic)
const pubkey = privateKey.toPubkey()
const sender = pubkey.toAddress().toAccBech32()
// Step 2.1: Prepare oracle request's properties
const obi = new Obi('{symbols:[string],multiplier:u64}/{rates:[u64]}')
const calldata = obi.encodeInput({ symbols: ['ETH'], multiplier: 100 })
let coin = new Coin()
coin.setDenom('uband')
coin.setAmount('1000000')
let feeCoin = new Coin()
feeCoin.setDenom('uband')
feeCoin.setAmount('5000')
// Step 2.2: Create an oracle request message
const requestMessage = new Message.MsgRequestData(
37,
calldata,
4,
3,
'BandProtocol',
sender,
[coin],
50000,
200000,
)
// Step 3.1: Construct a transaction
const fee = new Fee()
fee.setAmountList([feeCoin])
fee.setGasLimit(1000000)
const chainId = await client.getChainId()
const txn = new Transaction()
txn.withMessages(requestMessage)
await txn.withSender(client, sender)
txn.withChainId(chainId)
txn.withFee(fee)
txn.withMemo('')
// Step 3.2: Sign the transaction using the private key
const signDoc = txn.getSignDoc(pubkey)
const signature = privateKey.sign(signDoc)
const txRawBytes = txn.getTxData(signature, pubkey)
// Step 4: Broadcast the transaction
const sendTx = await client.sendTxBlockMode(txRawBytes)
return sendTx
}