Arcana CA SDK Usage
Estimated time to read: 3 minutes
Use the Arcana CA SDK to leverage Arcana's Chain Abstraction and enable unified balance for Web3 app users.
Installation
NPM/Yarn Install
npm install --save @arcana/ca-sdk
yarn add @arcana/ca-sdk
Usage
Import CA
import { CA } from '@arcana/ca-sdk' // From npm
Initialize CA
import { CA } from '@arcana/ca-sdk'
const provider = window.ethereum
const ca = new CA(provider)
//CA provider can be used as a drop in replacement for EIP1193 provider(like window.ethereum)
await ca.init()
//Initialize CA provider before calling any API function
See Get Started with CA SDK for more CA SDK usage insights.
CA APIs
UI Hooks
Use the following UI Hooks for allowance and intent setup in the dApp:
ca.setOnAllowanceHook(async ({ allow, deny, sources }) => {
// This is a hook for the dev to show user the allowances that need to be setup for the current tx to happen
// sources: an array of objects with minAllowance, chainID, token symbol etc
// allow(allowances): allowances is an array with allowance for each source (len(sources) == len(allowances))
// deny(): stop the flow
})
ca.setOnIntentHook(({ intent, allow, deny, refresh }) => {
// This is a hook for the dev to show user the intent, the sources and associated fees
// intent: Intent data containing sources and fees for display purpose
// allow(): accept the current intent
// deny(): deny the intent and stop the flow
// refresh(): should be on a timer of 5s to refresh the intent (if not refreshed old intents might fail due to fee changes)
})
Allowance
Allowances are required to enable unified balance in the wallet. It requires the wallet user to permit the Arcana Vault contract to set up ERC20 token allowances on their behalf for spending unified balance across supported chains.
Get Allowance
// Get USDC allowance for Polygon
await ca.allowance().tokens(["USDC"]).chain(137).get()
// Get USDC & USDT allowance for all supported chains
await ca.allowance().tokens(["USDC", "USDT"]).get()
// Get all supported token allowances for all supported chains
await ca.allowance().get()
Set Allowance
await ca.allowance().tokens(["USDC"]).chain(42161).amount("max").set()
// You can specify custom values for tokens and amount in hex, for example
// await ca.allowance().tokens(["USDC"]).chain(42161).amount("0x989680").set()
// Alternatively, you can also specify the amount 10,000,000 for USDC tokens as follows:
// await ca.allowance().tokens(["USDC"]).chain(42161).amount("10000000").set()
Unified Balance
const balances = await ca.getUnifiedBalances()
const usdtBalance = await ca.getUnifiedBalance("usdt")
Bridge
await ca.bridge().token("pol").amount(10).chain(137).exec();
Request
await ca.request({
method: "eth_sendTransaction",
params: [{
to: "0xEa46Fb4b4Dc7755BA29D09Ef2a57C67bab383A2f",
from: "0x7f521A827Ce5e93f0C6D773525c0282a21466f8d",
value: "0x001"
}],
})
Transfer
await ca.transfer().to("0x...").amount(5).chain(10).token("eth").exec()
// Note: Here chain() is optional.
// The current chain will be used as input if not specified.
CA Events Listener
Add Listener
ca.addCAEventListener((data) => {
switch(data.type) {
case "EXPECTED_STEPS":{
// store data.data(an array of steps which will be followed)
state.value.steps = data.data.map(s => ({ ...s, done: false }))
state.value.inProgress = true
break;
}
case "STEP_DONE": {
const v = state.value.steps.find(s => {
return s.typeID === data.data.typeID
})
if (v) {
v.done = true
}
break;
}
}
});
Remove Listener
ca.removeCAEventListener((data) => {...})
Check out CA SDK Reference Guide for details.
Last update:
December 26, 2024 by