Please refer to https://developers.eos.io/manuals/eosjs/v16.0/index
Get Amax Object
This is a local instantiation object, and the private key is stored in the front-end (not secure), independent of the Scatter plugin.
Copy import Amax from "@amax/amaxjs";
const client = Amax({
keyProvider: "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3",// private key
httpEndpoint: "http://127.0.0.1:8888",
chainId: chain.sys,
});
However, we usually use the Scatter plugin for operations, where the private key is placed in the plugin or app, and then we need to obtain the Amax object from the plugin. Please refer to the Scatter 6.1.0 document for details.
Copy import Amax from "@amax/amaxjs";
const client = scatter.amax(
network,
Amax,
{
...options,
authorization: [`${account.name}@${account.authority}`],
},
network.protocol,
);
At this point, the client is the proxy object in the plugin or APP. When a signature is required, the plugin or APP provides the signature.
Amax Method on Object
Copy abiBinToJson: ƒ ()
abiJsonToBin: ƒ ()
bidname: ƒ ()
buyram: ƒ ()
buyrambytes: ƒ ()
canceldelay: ƒ ()
claimrewards: ƒ ()
config: {…}
contract: ƒ ()
create: ƒ ()
createTransaction: ƒ ()
delegatebw: ƒ ()
deleteauth: ƒ ()
fc: {structs: {…}, types: {…}, abiCache: {…}, fromBuffer: ƒ, toBuffer: ƒ}
getAbi: ƒ ()
getAccount: ƒ ()
getActions: ƒ ()
getBlock: ƒ ()
getBlockHeaderState: ƒ ()
getCode: ƒ ()
getCodeHash: ƒ ()
getControlledAccounts: ƒ ()
getCurrencyBalance: ƒ ()
getCurrencyStats: ƒ ()
getInfo: ƒ ()
getKeyAccounts: ƒ ()
getProducerSchedule: ƒ ()
getProducers: ƒ ()
getRawCodeAndAbi: ƒ ()
getRequiredKeys: ƒ ()
getScheduledTransactions: ƒ ()
getTableRows: ƒ ()
getTransaction: ƒ ()
issue: ƒ ()
linkauth: ƒ ()
modules: {format: {…}}
newaccount: ƒ ()
nonce: ƒ ()
onerror: ƒ ()
pushBlock: ƒ ()
pushTransaction: ƒ ()
pushTransactions: ƒ ()
refund: ƒ ()
regproducer: ƒ ()
regproxy: ƒ ()
reqauth: ƒ ()
rmvproducer: ƒ ()
sellram: ƒ ()
setabi: ƒ ()
setalimits: ƒ ()
setcode: ƒ ()
setglimits: ƒ ()
setparams: ƒ ()
setpriv: ƒ ()
setprods: ƒ ()
setram: ƒ ()
transaction: ƒ _callee()
transfer: ƒ ()
undelegatebw: ƒ ()
unlinkauth: ƒ ()
unregprod: ƒ ()
updateauth: ƒ ()
voteproducer: ƒ ()
Contract operation (single or multiple contracts)
Copy // @returns {Promise}
eos.contract("myaccount", [options], [callback])
// Run immediately, `myaction` returns a Promise
eos.contract("myaccount").then(myaccount => myaccount.myaction(..))
// Group actions. `transaction` returns a Promise but `myaction` does not
eos.transaction("myaccount", myaccount => { myaccount.myaction(..) })
// Transaction with multiple contracts
eos.transaction(["myaccount", "myaccount2"], ({myaccount, myaccount2}) => {
myaccount.myaction(..)
myaccount2.myaction(..)
})
Other Methods
Copy import Amax from "@amax/amaxjs";
const {format, api, ecc, json, Fcbuffer} = Amax.modules
Copy DecimalImply: ƒ DecimalImply(value, precision)
DecimalPad: ƒ DecimalPad(num, precision)
DecimalString: ƒ DecimalString(value)
DecimalUnimply: ƒ DecimalUnimply(value, precision)
ULong: ƒ ULong(value)
decodeName: ƒ decodeName(value)
decodeNameHex: ƒ decodeNameHex(hex)
encodeName: ƒ encodeName(name)
encodeNameHex: ƒ encodeNameHex(name)
isName: ƒ isName(str, err)
parseAsset: ƒ parseAsset(str)
printAsset: ƒ printAsset(_ref)
Copy Aes: {encrypt: ƒ, decrypt: ƒ}
PrivateKey: ƒ PrivateKey(d)
PublicKey: ƒ PublicKey(Q)
Signature: ƒ Signature(r, s, i)
initialize: ƒ ()
isValidPrivate: ƒ isValidPrivate(wif)
isValidPublic: ƒ isValidPublic(pubkey)
key_utils: {random32ByteBuffer: ƒ, addEntropy: ƒ, cpuEntropy: ƒ, entropyCount: ƒ, checkDecode: ƒ, …}
privateToPublic: ƒ privateToPublic(wif)
randomKey: ƒ randomKey(cpuEntropyBits)
recover: ƒ recover(signature, data)
recoverHash: ƒ recoverHash(signature, dataSha256)
seedPrivate: ƒ seedPrivate(seed)
sha256: ƒ sha256(data)
sign: ƒ sign(data, privateKey)
signHash: ƒ signHash(dataSha256, privateKey)
unsafeRandomKey: ƒ unsafeRandomKey()
verify: ƒ verify(signature, data, pubkey)
verifyHash: ƒ verifyHash(signature, dataSha256, pubkey)
Copy ecc.verify(signature, signatureString, publicKey);
Convert account to bigNumber
Usage scenario: When querying contract data, it is usually necessary to use a username to search, and the account passed to the contract needs to be converted to bigNumber. If you don't convert there'll be a pure digital account bug.
Copy format.encodeName(account, false)
Index serialization issue
Contract Code
Copy uint128_t by_account() const { return (uint128_t)account.value << 64 | (uint128_t)id; }
Front-end
Copy import Amax from "@amax/amaxjs";
import BigInt from "big-integer";
const { format } = Amax.modules;
/**
* Decimal to binary
* @returns
*/
export const decimalToBinary = num => Number(num).toString(2);
/**
* Binary to Decimal
* @param {*} bin
* @returns
*/
export const binaryToDecimal = bin => parseInt(bin, 2);
/**
* According to the account, the second parameter is the sorting field, which can be either time or ID
* This index is 128 bit, which means 64 bit account+64 bit maximum value
* @ param {*} account account
* @ param {*} bin has a maximum value of 1 and a minimum value of 0
* @ returns returns a decimal string
*/
export function getBound(account, bin) {
const name = Number(format.encodeName(account, false));
const binName = decimalToBinary(name);
const lower = binName.padStart(64, 0) + "".padStart(64, bin);
return new BigInt(lower, 2).toString(10);
}
// lower_bound
// getBound("user1", 0)
// upper_bound
// getBound("user1", 1)
Query
Copy client.getTableRows({
code: "mart",
scope: "3741319478",
table: "sellorders",
lower_bound: getBound("user1", 0),
upper_bound: getBound("user1", 1),
json: true,
encode_type: "i128",
key_type: "i128",
limit: 1,
reverse: true,
index_position: 3,
});