cargo

cargo

estimateGas

Boolean This can be set to true if you would like Cargo JS to automatically estimate gas for you when calling methods which execute Ethereum transactions. This is useful if you are using a Web3 wallet that does not estimate gas for you, estimates gas incorrectly, or may submit a transaction even if it is bound to fail.

// You can set this value to true before calling any Cargo JS methods
// that submit Ethereum transactions
cargo.estimateGas = true;

enable()

Use the enable method to connect to MetaMask, or another wallet and get access to the current account. After calling enable you can use API methods that require a Web3 provider.

Arguments:

This method doesn't take any arguments.

Returns:

This function returns a Promise and resolves with a boolean stating whether a successful connection has been made.

Events:

enabled - Emitted when successfully enabled.

enable-required - Emitted if the user has MetaMask, but declined to connect to Cargo. Could also emit when an error was thrown.

on(event, listener)

Use the on method to add event listeners.

Arguments:

This method takes two arguments:

Parameter

Description

event - CargoEvent

The event to listen for.

listener - function

The listener is a function that takes data as an argument.

off(event, listener)

Use the off method to remove an event listener.

Arguments:

This method takes two arguments:

Parameter

Description

event - string

The event to listen for.

listener - function

The listener is a function that takes data as an argument.

removeAllListeners(event)

Use the removeAllListeners method to remove all listeners for a given event.

Arguments:

This method takes one argument, event which is a string.

getCommission(percentage)

Use the getCommission method to calculate beneficiary commissions. When updating beneficiary commissions, or application fees the total amount of all commissions cannot equal more than 100%.

Arguments:

This method takes one argument, percentage which is a number between 0 and 1.

Example:

// Calculate a 2% commission
const commission = cargo.getCommission(0.02);
console.log(commission); // '20000000000000000'

getContractInstance(contract, setAddress)

Use this method to get a Web3 contract instance of one of Cargo's smart contracts. This can only be called after calling the enable method. You can also use this method to get an instance of a Cargo ERC-721 contract with the given address.

Arguments:

Argument

Description

contract

Can be one of the following:

  • cargoNft

  • nftCreator

  • cargoData

  • cargoAsset

  • cargoSell

  • cargoVendor

  • cargoMintingCredits

  • super721

​

setAddress

Optional. Contract address to use when getting contract instance.

Example:

const myNftContract = cargo.getContractInstance('cargoNft', myContractAddress);

You can read about how to interact with these contracts in the web3 documentation about Contracts.

setProvider

You can update the provider that the Cargo JS instance uses by using the setProvider method.

cargo.setProvider(newProvider);

Cargo Events

The following events can be passed to the on function

// Event stating that Cargo JS is enabled
cargo.on('enabled', () => {});
​
// Event stating that enabling was rejected. Either an
// error was thrown, or the user rejected
cargo.on('enable-required', () => {});
​
// MetaMask supported event. Emitted when accounts change
// First argument of callback function is an array of accounts.
cargo.on('accounts-changed', (accounts/*: Array<Account>*/) => {});
​
// Fired if cargo.enable is called without a web3 provider
// available.
cargo.on('provider-required', () => {});
​

After enabling Cargo an instance of PollTx will be created. You can use the events on that instance to watch for completed transactions when using the API methods that make transactions on the blockchain.

​