Client Integration

How to Get Started With the Client Integration?

We will introduce some pieces of code in this section to help you make use of client integration. In this section, you can find the required functions written in JS or TS used to handle some specific operations within the Metis platform.

Note that all the client integration functions and code examples are for HTTP in JS or TS.

constructor(appId, accessToken, refreshToken, expiresIn, apiHost?)

Creating a new instance of the HttpClient.

const httpClient = new httpClient(
// The app id found on the application dashboard of Polis
"60f4f7e5ac31e3dccdcdecb5",
//The 0Auth2 Access token of your application
"3e2444385db44f1a802390f692041e92",
// The 0Auth2 refresh token of your application
"3eb18d851e414048b171ff53050b4fa2",
// Seconds until the accessToken expires
1800,
// The API host ip address
"localhost.localdomain"
);

Returns

Return example for accessing the token and confirming the transaction.

// Return Instance Example
HttpClient {
// The access token for the application api
accessToken: "68572b9bac1f499098b507467094662e"
// The API host of the application
apiHost: "https://rocket.metis.io"
// Your Polis app id
appId: "60ee207cac31e30b0b9f59fe"
// The transaction confirmation URL
confirmUrl: "https://rocket.metis.io/#/oauth2/confirm-tx"
// 0Auth2
oAuth2Client: Oauth2Client {
oauthRedirectUrl: "https://rocket.metis.io/#/oauth2-login?",
apiHost: "https://rocket.metis.io",
oauth2User: {…}
}
// The refresh token of application api
refreshToken: "664bbed738ae44469f1edc2be1016637"
// Promise config
swalPromise: null
}

sendTx(domain, chainid, fun, args?, succCallback?, errCallback?)

Sending a transaction onto the blockchain and calling a function within the smart contract.

// Getting the account balance of a wallet
const balance = httpClient.sendTx(
// This is your contract name when you assigned it to Polis
'my_contract',
// The chain id of blockchain to send the transaction onto
435,
// The specific function that you would like to call within the contract
'balanceOf',
// The parameters to call the contract function with
['0xabcffffffff'],
// Successful callback function passed into this function call
() => console.log("tx sent"),
// Error callback function passed into this function call
() => console.log("tx failed")
);

sendTx Return

sendTx Return example:

{
// Execution status
act: "SUCCESS"
// Parameters passed in at input
args: ["0x6902702BB5678D7361C94441c71F600C255dd833"]
// The chain id of the contract
chainid: 435
// The contract address
contract_address: "0x14B35991f4f2fb275C86156C6DD816b6F7D10052"
// Data status
data: "ok"
// The Domain of the contract on Polis
domain: "testest"
// The address of tx
eth_address: "0x137f2f8406594F855A913574A2e4DEC7E5383384"
// Contract function called
function: "balanceOf"
// Nonce of block containing tx
nonce: 0
// Result returned by the contract function call
result: 0
}
sendTxAsync(domain, chainid, fun, args?)

Sending a transaction onto the blockchain asynchronously and calling a method within a smart contract.

// Sending tokens
const result = await httpClient.sendTxAsync(
// This is your contract name when you assigned it to Polis
"my_contract",
// The chain id of blockchain to send the transaction
435,
// The specific function that you would like to call within the contract
"sendToken",
// The parameters to call the contract function with
[
"0x11e575e473D552849b35d15A71C85bfFA511f45B",
"0xb253C95DBc5e8E174C2D5272Ca8b06539c25E527",
900
]
);

sendTxAsync Return

An example of sending a transaction asynchronously.

// sendTxAsync Return Instance Example
{
// Result Output
act: "SUCCESS",
// Arguments that were passed in
args: ["0x6902702BB5678D7361C94441c71F600C255dd833"],
// The chain id of tx
chainid: 435,
// The contract address called
contract_address: "0x14B35991f4f2fb275C86156C6DD816b6F7D10052",
// Data status
data: "ok",
// The domain used on Polis
domain: "testest",
// The address of tx
eth_address: "0x137f2f8406594F855A913574A2e4DEC7E5383384",
// The function of contract called
function: "balanceOf",
// Nonce of block
nonce: 0,
// Return value of the contract function called
result: 0,
}beforeConfirm(domain, chainid, address, fun, args?, gas?, gasPrice?, fee?)

Checking the final validity of a transaction before it is executed on chain.

// Checking ClaimToken transaction
const result = await httpClient.beforeConfirm(
// This is your contract name when you assigned it to Polis
"my_contract",
// The chain id of blockchain to send the transaction onto
435,
// The address of the initiation of the tx
"0x11e575e473D552849b35d15A71C85bfFA511f45B",
// The name the function in the contract which is called
"claimToken",
// The parameters to call the contract function with
[
"0x11e575e473D552849b35d15A71C85bfFA511f45B",
"https://www.humanesociety.org/sites/default/files/styles/1240x698/public/2020-07/kitten-510651.jpg?h=f54c7448&itok=ZhplzyJ9"
],
// The Chain gas limit value
1500000,
// The gas price on chain
100000,
// Fees for this specific transaction
16000000000
)

queryTx(chainid, tx, succCallback?, errCallback?)

Querying a transaction’s validity and calling a successful or an error message in execution.

// Querying a transaction on the Metis testnet
const result = httpClient.queryTx(
// The chain id of blockchain to send the transaction into
435,
// The transaction hash to query
"0x7a242f5d0b10df0db158fcedb9d19f9d261dfbdc64c837370f1d13d2ad4862a4",
// Inputting an example of successful callback function into the function call
() => console.log("tx is valid"),
// Inputting an example of error callback function into the function call
() => console.log("tx is not valid")
)

queryTx Return

queryTx Return Example:

{
// Successful Query of tx hash
act: "SUCCESS"
// The chain Id of tx query
chainid: 435
// Returned data (nothing)
data: []
// The domain of the contract used on Polis
domain: "mydomain"
// Tx status
status: "SUCCEED"
// The transaction hash on chain
tx: "0x7a242f5d0b10df0db158fcedb9d19f9d261dfbdc64c837370f1d13d2ad4862a4"
}
// Console should also output: "tx is valid" or "tx is not valid" based on input examplequeryTxAsync(chainid, tx)

Querying transactions’ validity asynchronously to return a promise.

// Querying Transactions Asynchronously
const result = await queryTxAsync(
// The chain Id of the Transaction
435,
// The executed transaction hash on chain
"0xbf09763a4eefd24cf08439d30d82581c366ecfa62763c1ad686a6e8b741f0b8b"
);

queryTxAsync Return

queryTxAsync Return Example:

{
// Call status
act: "SUCCESS"
// The chain Id with tx
chainid: 435
// Data returned (nothing)
data: []
// The domain name of the contract on Polis
domain: "mydomain"
// Tx status
status: "SUCCEED"
// The Tx hash
tx: "0x7a242f5d0b10df0db158fcedb9d19f9d261dfbdc64c837370f1d13d2ad4862a4"
}
]closeConfirmDialogue()

Closing the dialog and calling the close() method without any parameters.

httpClient.closeConfirmDialogue();

error(msg)

Calling the error method and sending an error message to the console.

httpClient.error("Deployment Error");

Error Return

Error return example:

["socket client runtime error!", "Deployment Error"]

handleRefreshToken(callback?)

Using the refreshToken to obtain a new accessToken. The callback function should be called and log the message “Successful Refresh” in the console.

// Calling the handleRefreshToken with the refreshFunction
httpClient.handleRefreshToken(
// Callback function
() => console.log("Successful Refresh")
);

handleRefreshTokenAsync()

Handling Refresh token asynchronously as input without any parameters.

await httpClient.handleRefreshTokenAsync()

log(obj)

Logging different variables.

// Passing string into the log function and logging them into the console

httpClient.log(["address: ", "0x11e575e473D552849b35d15A71C85bfFA511f45B"])

Log Return

// Log return example

["address: ", "0x11e575e473D552849b35d15A71C85bfFA511f45B"]

Please feel free to reach out to our Help Center if you have any technical questions.

Last updated