@abaxx/ax-js-beta 中文文档教程
SynthetixJs library
Synthetix-JS 库提供了一个简单的预打包 API,用于与以太坊上的 Synthetix 进行通信。 您可以使用它为 DeFi 不断发展的合成资产生态系统做出贡献。
这对于黑客马拉松团队快速 npm install synthetix-js
并在几分钟内开始构建特别有用。
在底层,SynthetixJs 使用 ethers.js 库及其 提供商 和 交易签名者。
Install via npm
npm install synthetix-js
Developer Docs
Example for getting the total sUSD stablecoin in circulation
const { SynthetixJs } = require('synthetix-js');
const snxjs = new SynthetixJs(); //uses default ContractSettings - ethers.js default provider, mainnet
(async function() {
const totalSUSD = await snxjs.sUSD.totalSupply();
const totalSUSDSupply = snxjs.utils.formatEther(totalSUSD);
console.log('sUSDTotalSupply', totalSUSDSupply);
})();
默认设置不使用任何签名者。 这意味着可以从合约中查看常量,但执行交易将失败。 要执行交易,请设置签名者。
库中包含 4 个签名者 - Metamask(与 Dapp 浏览器兼容)、Trezor、Ledger 和 PrivateKey。 也可以使用自定义的 ethers.js 兼容签名者。
Example using a metamask signer
const { SynthetixJs } = require('synthetix-js');
const metaMaskSigner = new SynthetixJs.signers.Metamask();
const snxjs = new SynthetixJs({ signer: metaMaskSigner }); //uses Metamask signer and default infura.io provider on mainnet
Example of minting stablecoin(sUSD) with private key signer
const { SynthetixJs } = require('synthetix-js');
//parameters: default provider, default networkId, private key as a string
const signer = new SynthetixJs.signers.PrivateKey(
null,
0,
'0x0123456789012345678901234567890123456789012345678901234567890123'
);
const snxjs = new SynthetixJs({ signer });
async function run() {
const totalSupply = await snxjs.Synthetix.totalSupply();
const snxTotalSupply = snxjs.utils.formatEther(totalSupply);
console.log('snxTotalSupply', snxTotalSupply);
//issue 100 synths (will throw if insufficient funds for gas)
try {
const txObj = await snxjs.Synthetix.issueSynths(snxjs.util.parseEther('100')); //execute transaction (requires gas)
console.log('transaction hash', txObj.hash);
} catch (e) {
console.log(e);
}
}
run();
Live examples
Got any questions?
SynthetixJs library
The Synthetix-JS Library provides a simple pre-packaged API to communicate with Synthetix on ethereum. You can use it to contribute to DeFi's growing synthetic asset ecosystem.
This is particularly useful for hackathon teams to quickly npm install synthetix-js
and start building in just a few minutes.
Under the hood, SynthetixJs uses ethers.js library and its concept of providers and transaction signers.
Install via npm
npm install synthetix-js
Developer Docs
Example for getting the total sUSD stablecoin in circulation
const { SynthetixJs } = require('synthetix-js');
const snxjs = new SynthetixJs(); //uses default ContractSettings - ethers.js default provider, mainnet
(async function() {
const totalSUSD = await snxjs.sUSD.totalSupply();
const totalSUSDSupply = snxjs.utils.formatEther(totalSUSD);
console.log('sUSDTotalSupply', totalSUSDSupply);
})();
Default settings don't use any signer. That means that constants can be viewed from the contract but executing a transaction will fail. To execute transactions, set up signer.
4 signers are included in the library - Metamask (compatible with Dapp browsers), Trezor, Ledger and PrivateKey. Custom ethers.js compatible signers can be used too.
Example using a metamask signer
const { SynthetixJs } = require('synthetix-js');
const metaMaskSigner = new SynthetixJs.signers.Metamask();
const snxjs = new SynthetixJs({ signer: metaMaskSigner }); //uses Metamask signer and default infura.io provider on mainnet
Example of minting stablecoin(sUSD) with private key signer
const { SynthetixJs } = require('synthetix-js');
//parameters: default provider, default networkId, private key as a string
const signer = new SynthetixJs.signers.PrivateKey(
null,
0,
'0x0123456789012345678901234567890123456789012345678901234567890123'
);
const snxjs = new SynthetixJs({ signer });
async function run() {
const totalSupply = await snxjs.Synthetix.totalSupply();
const snxTotalSupply = snxjs.utils.formatEther(totalSupply);
console.log('snxTotalSupply', snxTotalSupply);
//issue 100 synths (will throw if insufficient funds for gas)
try {
const txObj = await snxjs.Synthetix.issueSynths(snxjs.util.parseEther('100')); //execute transaction (requires gas)
console.log('transaction hash', txObj.hash);
} catch (e) {
console.log(e);
}
}
run();