Interfacing with Bancor Contracts
Access Bancor features from your smart contract
While many users benefit from the Bancor Network by using the Bancor App or a Bancor Widget, developers can also access Bancor's many features from their own smart contracts. The API reference section provides a detailed look into the full functionality of each contract in the system. This section will provide a quick look into some more common features and should contain sufficient information for most use cases.

Adding/Removing Liquidity

If you're drawing liquidity from users, note that adding liquidity to Bancor pools requires ERC20 approvals of the underlying token reserves. You'll need to have the user submit an approval transaction for each reserve (e.g. BNT or DAI) previously with your Converter contract address as the spender field and the amount they would like to contribute in the amount field .
This is the Converter interface for adding (fund) or removing (liquidate) liquidity. In the case of adding liquidity, the amount value is the number of pool tokens that you want to create. Based on the contributor's token reserves and the current state of the liquidity pool, you'll have to calculate this value either on-chain or off-chain. This number is capped by the reserves owned by the contributor and the amount of ERC20 approved for transfer.
In the case of removing liquidity, the amount value is the number of pool tokens to burn for the underlying liquidity reserves. This number is capped by the number of tokens owned by the contributor's address.
1
contract IBancorConverter {
2
function fund(uint256 amount) external;
3
function liquidate(uint256 amount) external;
4
}
5
6
contract MyContract {
7
IBancorConverter converter = IBancorConverter(<converter-address>);
8
9
function addLiquidity(uint tokensToMint) public {
10
...
11
converter.fund(tokensToMint);
12
...
13
}
14
}
Copied!

Trading With Bancor

  • path: Network path between sourceToken and toToken
    • The getPathAndRate function on the Bancor SDK will generate the optimal path for this parameter
  • amount: Source token input amount
  • minReturn: To token minimum return
  • affiliateAccount: Address to direct affiliate fees
  • affiliateFee: Fee amount (1000 would be equal to 0.1%)
1
contract IBancorNetwork {
2
// call when sending eth
3
function convert(
4
IERC20Token[] path,
5
uint256 amount,
6
uint256 minReturn
7
) external payable returns(uint256 returnAmount);
8
9
// call when sending tokens
10
function claimAndConvert(
11
IERC20Token[] path,
12
uint256 amount,
13
uint256 minReturn
14
) external returns(uint256 returnAmount);
15
}
16
17
contract IContractRegistry {
18
function addressOf(bytes32 contractName) public view returns(address);
19
}
20
21
contract MyContract {
22
address private constant CONTRACT_REGISTRY_ADDRESS = 0x52Ae12ABe5D8BD778BD5397F99cA900624CfADD4;
23
bytes32 private constant BANCOR_NETWORK = "BancorNetwork";
24
25
function trade(
26
IERC20Token[] path,
27
uint256 minReturn
28
) external payable {
29
IContractRegistry contractRegistry = IContractRegistry(CONTRACT_REGISTRY_ADDRESS);
30
address bancorNetworkAddress = IContractRegistry.addressOf(BANCOR_NETWORK);
31
IBancorNetwork bancorNetwork = IBancorNetwork(bancorNetworkAddress);
32
bancorNetwork.convert(path, msg.value, minReturn);
33
}
34
35
function tradeToken(
36
IERC20Token[] path,
37
uint256 amount,
38
uint256 minReturn
39
) external payable {
40
IContractRegistry contractRegistry = IContractRegistry(CONTRACT_REGISTRY_ADDRESS);
41
address bancorNetworkAddress = IContractRegistry.addressOf(BANCOR_NETWORK);
42
IBancorNetwork bancorNetwork = IBancorNetwork(bancorNetworkAddress);
43
bancorNetwork.convert(path, amount, minReturn);
44
}
45
}
Copied!
Last modified 6mo ago