Deployment
On BandChain, a data script can be registered into the system by anyone. This is done through the registrant sending
a MsgCreateOracleScript
message to the chain.
A MsgCreateOracleScript
message contains various parameters of the data script that is to be registered. These
parameters include:
name
: Name of the data script.description
: A description of the data script.schema
: The data script's schema which details the inputs and outputs of this data script.source_code_url
: The URL for the source code of the data script.code
: The Owasm-compiled binary of the data script.owner
: The owner of the data script. The owner will have edit rights. If omitted, the data script's parameters will no longer be able to be edited after being registered.sender
: The message sender account.
In order to send a MsgCreateOracleScript
message, we can use either
bandchain.js or
pyband
An example on how to send a MsgCreateOracleScript
message via
bandchain.js can be seen below.
import { band, getSigningBandClient } from '@bandprotocol/bandchain.js'
import fs from 'fs'
import path from 'path'
// Import DirectSecp256k1HdWallet from the bundled cosmjs in bandchain.js
const { DirectSecp256k1HdWallet } = await import(
'@bandprotocol/bandchain.js/node_modules/@cosmjs/proto-signing'
)
const rpcEndpoint = 'https://rpc.band-v3-testnet.bandchain.org'
const mnemonic = process.env.MNEMONIC
async function createOracleScript() {
// Create a signer using the compatible DirectSecp256k1HdWallet
const signer = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: 'band',
})
const [account] = await signer.getAccounts()
// Create a signing client
const client = await getSigningBandClient({
rpcEndpoint: rpcEndpoint,
signer: signer,
})
// Setup the transaction's properties
const execPath = path.resolve(__dirname, 'hello_world.wasm')
const code = fs.readFileSync(execPath)
// Import the createOracleScript message composer from Band's oracle module
const { createOracleScript } = band.oracle.v1.MessageComposer.withTypeUrl
// Create a MsgCreateOracleScript message
const msgCreateOracleScript = band.oracle.v1.MsgCreateOracleScript.fromPartial({
name: 'Hello World!', // data script name
code: code, // data script code
owner: account.address, // owner
sender: account.address, // sender
description: '', // description
schema: '{repeat:u64}/{response:string}', // schema
sourceCodeUrl: 'https://ipfs.io/ipfs/QmSSrgJ6QuFDJHyC2SyTgnHKRBhPdLHUD2tJJ86xejrCfn' // source code url
})
// Convert to properly formatted message with type URL
const msg = createOracleScript(msgCreateOracleScript)
// Set transaction fee
const fee = {
amount: [{ denom: 'uband', amount: '5000' }], // Transaction fee
gas: '350000', // Gas limit
}
// Sign and broadcast the transaction
const result = await client.signAndBroadcast(account.address, [msg], fee)
return result
}
;(async () => {
console.log(await createOracleScript())
})()
An example on how to send a MsgCreateDataSource
message via
pyband can also be seen below.
import os
from pyband import Client, Transaction
from pyband.wallet import PrivateKey
from pyband.proto.cosmos.base.v1beta1.coin_pb2 import Coin
from pyband.proto.oracle.v1.tx_pb2 import MsgCreateOracleScript
from google.protobuf.json_format import MessageToJson
def main():
# Setup Client
grpc_url = "laozi-testnet6.bandchain.org"
c = Client(grpc_url)
# Setup Wallet
mnemonic = os.getenv("MNEMONIC")
private_key = PrivateKey.from_mnemonic(mnemonic)
public_key = private_key.to_public_key()
sender_addr = public_key.to_address()
sender = sender_addr.to_acc_bech32()
# Prepare Transaction Properties
deploy_msg = MsgCreateOracleScript(
name="Hello World!",
description="",
schema="{repeat:u64}/{response:string}",
source_code_url="https://ipfs.io/ipfs/QmSSrgJ6QuFDJHyC2SyTgnHKRBhPdLHUD2tJJ86xejrCfn",
code=open("hello_world.wasm", "rb").read(),
owner=sender,
sender=sender,
)
account = c.get_account(sender)
account_num = account.account_number
sequence = account.sequence
fee = [Coin(amount="0", denom="uband")]
chain_id = c.get_chain_id()
# Construct a Transaction
txn = (
Transaction()
.with_messages(deploy_msg)
.with_sequence(sequence)
.with_account_num(account_num)
.with_chain_id(chain_id)
.with_gas(250000)
.with_fee(fee)
)
# Sign the Transaction
sign_doc = txn.get_sign_doc(public_key)
signature = private_key.sign(sign_doc.SerializeToString())
tx_raw_bytes = txn.get_tx_data(signature, public_key)
# Broadcast the transaction
tx_block = c.send_tx_block_mode(bytes(tx_raw_bytes))
print(MessageToJson(tx_block))
if __name__ == "__main__":
main()
After a successful transaction broadcast, the newly created data script ID can be found in the response json. The registrant can also view the created data script details on CosmoScan. An example of a successful transaction will return a response similar to the one shown below.
{
"height": "7440523",
"txhash": "FEDE0E7482CA6AB3A08E4643B2ADA03B0E6E961EE8747F41A1BF891BEDFE3C23",
"data": "0A220A202F6F7261636C652E76312E4D73674372656174654F7261636C65536372697074",
"rawLog": "[{\"events\":[{\"type\":\"create_oracle_script\",\"attributes\":[{\"key\":\"id\",\"value\":\"202\"}]},{\"type\":\"message\",\"attributes\":[{\"key\":\"action\",\"value\":\"/oracle.v1.MsgCreateOracleScript\"}]}]}]",
"logs": [
{
"events": [
{
"type": "create_oracle_script",
"attributes": [
{
"key": "id",
"value": "202"
}
]
},
{
"type": "message",
"attributes": [
{
"key": "action",
"value": "/oracle.v1.MsgCreateOracleScript"
}
]
}
]
}
],
"gasWanted": "250000",
"gasUsed": "246278"
}