Skip to the content
TAPEHOUSE

SDKs

Go SDK

Read the band, margin accounts, shorts, baskets, auction and gap cover from Go and pack their transactions, over go-ethereum bindings.

The Go SDK reads Tapehouse's contracts and packs their transactions as calldata you sign and send yourself. It sits on go-ethereum's bind/v2 and bindings generated from the contracts' ABIs. The repository's keeper, bidder, alerts, sponsor and backtest commands import it.

Read and send#

Go
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/tapehouse/tapehouse/services/sdk"
)

func main() {
	ctx := context.Background()
	deployments, err := sdk.LoadDeployments("deployments/46630.json")
	if err != nil {
		log.Fatal(err)
	}
	backend, err := ethclient.Dial("https://rpc.testnet.chain.robinhood.com")
	if err != nil {
		log.Fatal(err)
	}
	client := sdk.NewClient(backend, deployments)

	quote, err := client.Band().Quote(&bind.CallOpts{Context: ctx}, "TSLA")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(quote.State, quote.Low, quote.High)
}

A transaction function returns an sdk.Tx, the recipient and the calldata. Check it with client.Simulate, then sign and send it with client.Send, both of which return a decoded *sdk.Revert where the revert data matches an error of the contracts.

Go
tx, err := client.Accounts().Borrow(sdk.Cross, big.NewInt(50_000_000), account, account)
if err != nil {
	log.Fatal(err)
}
if err := client.Simulate(&bind.CallOpts{Context: ctx, From: account}, tx); err != nil {
	log.Fatal(err)
}
sent, err := client.Send(transactOpts, tx)

Assets are strings ("TSLA") and the SDK converts them with sdk.ToBytes32. A position is sdk.Cross, the cross position, or an asset's bytes32. Amounts are *big.Int in raw units. Every read takes *bind.CallOpts; set BlockNumber to read at a block, and reads that combine several calls (Repayment, InBaskets) use one block, the latest where you name none. Slippage arguments are int64 basis points from 0 to 10,000.

Registry#

FunctionWhat it does
sdk.LoadDeployments(path)Reads deployments/<chainId>.json and returns *Deployments
sdk.ParseDeployments(data)The same from bytes
deployments.SharePrices()Whether the chain's Chainlink feeds price the share (sdk.SharePriceChains, Arbitrum One)
deployments.TokenPriceFeed(name), deployments.SharePriceFeed(name)A Chainlink feed typed by what it prices
sdk.ToBytes32(name)An asset symbol or feed ID as 32 bytes
sdk.CrossThe cross position

Deployments also carries .chainlinkSequencer (ChainlinkSequencer), which the Go SDK reads for the sequencer-uptime feed.

Client#

sdk.NewClient(backend bind.ContractBackend, deployments *Deployments) returns a *Client. Its accessors return the objects below.

MethodReturns
Band()The band and each asset's BandFeed
Accounts()The margin accounts
Margin()The margin engine
Liquidator(), Backstop()The liquidator and the gap backstop's calls
ReopeningAuction()The reopening auction
LendingVault(asset)An asset's stock lending vault
Shorts()The short positions
Basket(key)A basket of .tapehouse.Baskets
GapCover()The gap cover
MorphoOracles()The Morpho oracles
Pool(address), SwapRouter(), RoundFeed(address)A Uniswap v3 pool, the swap router and a round feed
Header, Simulate, SendThe block header, an eth_call of a Tx, and sending a Tx
TokenPrice, SharePrice, RoundData, LatestRoundChainlink feed reads
BalanceOf, Allowance, Decimals, Paused, ApproveToken reads and an approval

Band#

MethodArgumentsDoes
Quoteopts, assetState (0 halted, 1 degraded, 2 closed, 3 open), legs, centre, half-width and edges
SessionoptsChainlink's session and NYSE's state and boundaries
Haltopts, assetThe signed halt and the issuer's pause
Priceopts, feedIDThe RedStone value the band stores
WritePricesctx, source, feedIDsPacks writePrices with the payload of a PackageSource you supply
Asset, CorporateAction, Termsopts, assetHow the band is configured for an asset
HaltSigneroptsThe halt signer's address
WriteHaltasset, halted, issuedAt, expiresAt, signaturePacks a signed halt
SyncMultiplierassetPacks a multiplier sync
LatestRound, LatestBand, Sealed, Sealopts, asset (and reopenMs)The BandFeed's round and band, the sealed band, and the seal call
SequencerSettled, Varianceopts (and feedID)The sequencer state and a feed's variance

PackageSource is an interface with one method, Payload(ctx context.Context, feedIDs []string) ([]byte, error). The SDK holds no RedStone key and calls no gateway.

Accounts#

position is [32]byte.

MethodArguments
SetAuthorizationauthorized, allowed
SetMandateagent, expiresAt, maxDebt, maxLeverage
Depositposition, token, amount, account
Withdrawposition, token, amount, account, receiver
Borrowposition, assets, account, receiver
Repayposition, assets, account
Unwrapaccount, key, shares
Settle, Clear, Sync, Observe, AccruePremiumKeeper calls
IsAuthorized, Mandate, MandateStatusAuthorization reads
Healthopts, account, position
Collateral, Leverageopts, account, position (and token)
LiquidationPriceopts, account, position, asset, borrowing
Repayment, InBasketsopts, account, position
Stocks, Holding, Closure, RecoveryEngine assets, the holding of an asset, the closure and the recovery window
BackstopPremium, HasBackstop, Lent, SellablePremium and lending reads

Shorts, baskets and gap cover#

ObjectMethods
Shorts()Deposit(asset, amount, account), Withdraw(asset, amount, account, receiver), Sell(asset, amount, minProceeds, account), Cover(asset, amount, maxCost, account), Liquidate(account, asset), Mark(asset); reads Position, Health, Epoch, Book, Restriction, Fee; quotes QuoteSale and QuoteCover returning Sale{Proceeds, MinProceeds} and BuyBack{Cost, MaxCost}
Basket(key)Mint(shares, receiver, maxAssets), Redeem(shares, receiver, owner), Rebalance; reads Components, PreviewMint, PreviewRedeem, Target, PendingTarget, TotalSupply
GapCover()Buy(asset, layer, maxPremium, holder), Deposit, Redeem, Record, Measure, Observe, Settle, Void, Release, Claim; reads Quote, MinDeductible, Tail, PricingGap, Capacity, LastCloseMs, Sales, Series, Feed, RegularHours, ReopenOf, Cover, Payouts; sdk.Payout(layer, referencePrice, price) computes a settled cover's payout
ReopeningAuction()Commit, Reveal, Enroll, Clear, Claim, Forfeit; reads Constants, Phase, LastOpenMs, Round, Lots, Bids, Commitment, Enrolled; sdk.Commitment(...) and sdk.ClearingPrice(bids, supply, floor)
Liquidator()Start, Stop, Buy, SettleCash, WriteOff, Recall; reads Auction, Shortfall, Running, Price, HourlyAllowance, HeldUntil, EthUsd, RecallHaircut
LendingVault(asset)Queue, Ticket, Claimable, Counted, BuyIn
MorphoOracles()Price(opts, asset) returning an OraclePrice, Halt, Oracle

The Go SDK has no builders for the supply vault, and no reads for the gap backstop's state or a depositor's holdings. Use the generated binding in services/sdk/bindings/supplyvault or gapbackstop for those, or the TypeScript SDK, which has supply and backstop modules.

Errors#

sdk.DecodeRevert(err) returns the *sdk.Revert behind an error, with Name, Args and Data. client.Simulate and client.Send already return one. See errors.

Go
if revert, ok := sdk.DecodeRevert(err); ok {
	fmt.Println(revert.Name, revert.Args)
}

Example#

services/sdk/examples/devnode runs the SDK against a local Nitro dev node: PRIVATE_KEY=0x… go run ./sdk/examples/devnode RPC_URL DEPLOYMENTS_JSON PAYLOAD_FILE, from the services directory. It reads the key from the environment, never from a flag.

Next#