
SDK
Library to interact with the GEB smart contracts. Manage your safes, mint RAI, inspect the system state, and much more.
The library is written in Typescript with full typing support. It allows access to the low level API to directly interact with the contracts.
Install
npm install @hai-on-op/sdk
Examples
This is a complete example of how you can inspect a SAFE and also open a new one using your own proxy:
import { ethers, utils as ethersUtils } from 'ethers'
import { Geb, utils } from '@hai-on-op/sdk'
const provider = new ethers.providers.JsonRpcProvider(
'http://kovan.infura.io/<API KEY>'
)
const wallet = new ethers.Wallet('0xdefiisawesome...', provider)
const geb = new Geb('kovan', provider)
const safe = await geb.getSafe(4)
console.log(`Safe id 4 has: ${utils.wadToFixed(safe.debt).toString()} RAI of debt.`)
console.log(`It will get liquidated if ETH price falls below ${(await safe.liquidationPrice())?.toString()} USD.`)
const globalDebt = await geb.contracts.safeEngine.globalDebt()
const debtCeiling = await geb.contracts.safeEngine.globalDebtCeiling()
const raiToDraw = ethersUtils.parseEther('15')
if(globalDebt.add(raiToDraw).gt(debtCeiling)) {
throw new Error('Debt ceiling too low, not possible to draw this amount of RAI.')
}
const proxy = await geb.getProxyAction(wallet.address)
const tx = proxy.openLockETHAndGenerateDebt(
ethersUtils.parseEther('1'),
utils.ETH_A,
raiToDraw
)
tx.gasPrice = ethers.BigNumber.from('80').mul('1000000000')
const pending = await wallet.sendTransaction(tx)
console.log(`Transaction ${pending.hash} waiting to be mined...`)
await pending.wait()
console.log('Transaction mined, safe opened!')
Additional examples
In the examples below we assume the variables are defined like in the complete example above.
Deploy a new proxy
const tx = geb.deployProxy()
await wallet.sendTransaction(tx)
Partial repay of safe debt
const proxy = await geb.getProxyAction("0xdefidream...")
let tx = geb.contracts.systemCoin.approve(proxy.proxyAddress, ethers.constants.MaxUint256)
await wallet.sendTransaction(tx)
tx = proxy.repayDebt(4, ethersUtils.parseEther('1'))
await wallet.sendTransaction(tx)
Complete repay of safe debt
const proxy = await geb.getProxyAction("0xdefidream...")
let tx = geb.contracts.systemCoin.approve(proxy.proxyAddress, ethers.constants.MaxUint256)
await wallet.sendTransaction(tx)
tx = proxy.repayAllDebt(4)
await wallet.sendTransaction(tx)
Withdraw Ether collateral
const proxy = await geb.getProxyAction("0xdefidream...")
const tx = proxy.freeETH(4, ethersUtils.parseEther('1'))
await wallet.sendTransaction(tx)
Repay all debt and withdraw all collateral
const proxy = await geb.getProxyAction("0xdefidream...")
const safe = await geb.getSafe(4)
let tx = geb.contracts.systemCoin.approve(proxy.proxyAddress, ethers.constants.MaxUint256)
await wallet.sendTransaction(tx)
const tx = proxy.repayAllDebtAndFreeETH(4, safe.collateral)
await wallet.sendTransaction(tx)
Make direct contract calls
The SDK exposes all contract APIs of all core contracts in the Geb.contracts object. Solidity functions that are read-only (view or pure) return asynchronously the expected value from the chain. State changing functions will return a transaction object to passed to ether.js or web3.
const surplusBuffer = await geb.contracts.accountingEngine.surplusBuffer()
const { stabilityFee } = await geb.contracts.taxCollector.collateralTypes(utils.ETH_A)
const tx = geb.contracts.liquidationEngine.liquidateSAFE(utils.ETH_A,"0xdefidream...");
await wallet.sendTransaction(tx)