Strike.js
A JavaScript SDK for Ethereum and the Strike Protocol. Wraps around Ethers.js. Works in the web browser and Node.js.
Strike.js Documentation
This SDK is in open beta, and is constantly under development. USE AT YOUR OWN RISK.
Ethereum Read & Write
JSON RPC based Ethereum read and write.
Read
const Strike = require('@strike-finance/strike-js');
const sUsdtAddress = Strike.util.getAddress(Strike.sUSDT);
(async function() {
let supplyRatePerBlock = await Strike.eth.read(
sUsdtAddress,
'function supplyRatePerBlock() returns (uint)',
[],
{}
);
console.log('USDT supplyRatePerBlock:', supplyRatePerBlock.toString());
})().catch(console.error);
Write
const toAddress = '0xa0df350d2637096571F7A701CBc1C5fdE30dF76A';
(async function() {
const trx = await Strike.eth.trx(
toAddress,
'function send() external payable',
[],
{
value: Strike._ethers.utils.parseEther('1.0'),
provider: window.ethereum,
}
);
const toAddressEthBalance = await Strike.eth.getBalance(toAddress);
})().catch(console.error);
Strike Protocol
Simple methods for using the Strike protocol.
const strike = new Strike(window.ethereum);
(async function() {
console.log('Supplying ETH to the Strike protocol...');
const trx = await strike.supply(Strike.ETH, 1);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
Install / Import
Web Browser
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@strike-finance/strike-js@latest/dist/browser/strike.min.js"></script>
<script type="text/javascript">
window.Strike;
</script>
Node.js
npm install @strike-finance/strike-js
const Strike = require('@strike-finance/strike-js');
import Strike from '@strike-finance/strike-js';
More Code Examples
See the docblock comments above each function definition or the official Strike.js Documentation.
Instance Creation
The following are valid Ethereum providers for initialization of the SDK.
var strike = new Strike(window.ethereum);
var strike = new Strike('http://127.0.0.1:8545');
var strike = new Strike();
var strike = new Strike('ropsten');
var strike = new Strike('https://mainnet.infura.io/v3/_your_project_id_', {
privateKey: '0x_your_private_key_',
});
var strike = new Strike('mainnet' {
mnemonic: 'clutch captain shoe...',
});
Constants and Contract Addresses
Names of contracts, their addresses, ABIs, token decimals, and more can be found in /src/constants.ts
. Addresses, for all networks, can be easily fetched using the getAddress
function, combined with contract name constants.
console.log(Strike.BUSD, Strike.ETH, Strike.sETH);
const sUsdtAddress = Strike.util.getAddress(Strike.sUSDT);
Mantissas
Parameters of number values can be plain numbers or their scaled up mantissa values. There is a transaction option boolean to tell the SDK what the developer is passing.
await strike.borrow(Strike.BUSD, '1000000000000000000', { mantissa: true });
await strike.borrow(Strike.BUSD, 1, { mantissa: false });
Transaction Options
Each method that interacts with the blockchain accepts a final optional parameter for overrides, much like Ethers.js overrides.
const trxOptions = {
mantissa,
abi,
provider,
network,
from,
gasPrice,
gasLimit,
value,
data,
chainId,
nonce,
privateKey,
mnemonic,
};
API
The Strike API is accessible from Strike.js. The corresponding services are defined in the api
namespace on the class.
Strike.api.account
Strike.api.sToken
Strike.api.marketHistory
Strike.api.governance
The governance method requires a second parameter (string) for the corresponding endpoint shown in the documentation.
proposals
voteReceipts
accounts
Here is an example for using the account
endpoint. The network
parameter in the request body is optional and defaults to mainnet
.
const main = async () => {
const account = await Strike.api.account({
"addresses": "0xB61C5971d9c0472befceFfbE662555B78284c307",
"network": "ropsten"
});
let busdBorrowBalance = 0;
if (Object.isExtensible(account) && account.accounts) {
account.accounts.forEach((acc) => {
acc.tokens.forEach((tok) => {
if (tok.symbol === Strike.sBUSD) {
busdBorrowBalance = +tok.borrow_balance_underlying.value;
}
});
});
}
console.log('busdBorrowBalance', busdBorrowBalance);
}
main().catch(console.error);
Test
Tests are available in ./test/*.test.js
. The tests are configured in ./test/index.js
. Methods are tested using a forked chain using ganache-core. A JSON RPC provider URL needs to be configured as an environment variable before running the tests (MAINNET_PROVIDER_URL
). Archive state must be available to run the tests. For free archive node access, get a provider URL from Alchemy.
## Run all tests
npm test
## Run a single test (Mocha JS grep option)
npm test -- -g 'runs eth.getBalance'
Build for Node.js & Web Browser
git clone git@github.com:StrikeFinance/strike-js.git
cd strike-js/
npm install
npm run build
Web Browser Build
<script type="text/javascript" src="./dist/browser/strike.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@strike-finance/strike-js@latest/dist/browser/strike.min.js"></script>
Node.js Build
const Strike = require('./dist/nodejs/index.js');
const Strike = require('@strike-finance/strike-js');