-
-
Save MrToph/6a24e978f620f79f7f34b71fb0aa1b20 to your computer and use it in GitHub Desktop.
Using Hydra to test Bancor Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { loadConfig, Blockchain } = require("@klevoya/hydra"); | |
const config = loadConfig("hydra.yml"); | |
describe("BancorConverter", () => { | |
let blockchain = new Blockchain(config); | |
let token = blockchain.createAccount(`token`); | |
let network = blockchain.createAccount(`network`); | |
let converter = blockchain.createAccount(`converter`); | |
let creator = blockchain.createAccount(`creator`); | |
let user1 = blockchain.createAccount(`user1`); | |
const ASymbol = { code: `AAAA`, precision: 4 }; | |
const BNTSymbol = { code: `BNT`, precision: 10 }; | |
const SmartSymbol = { code: `BNTAAAA`, precision: 4 }; | |
beforeAll(async () => { | |
token.setContract(blockchain.contractTemplates[`eosio.token`]); | |
converter.setContract(blockchain.contractTemplates[`BancorConverter`]); | |
token.updateAuth(`active`, `owner`, { | |
accounts: [ | |
// need to add permission of CONVERTER for converter.create action | |
{ | |
permission: { | |
actor: converter.accountName, | |
permission: `eosio.code` | |
}, | |
weight: 1 | |
} | |
] | |
}); | |
converter.updateAuth(`active`, `owner`, { | |
accounts: [ | |
{ | |
permission: { | |
actor: converter.accountName, | |
permission: `eosio.code` | |
}, | |
weight: 1 | |
} | |
] | |
}); | |
network.updateAuth(`active`, `owner`, { | |
accounts: [ | |
{ | |
permission: { | |
actor: network.accountName, | |
permission: `eosio.code` | |
}, | |
weight: 1 | |
} | |
] | |
}); | |
network.setContract(blockchain.contractTemplates[`BancorNetwork`]); | |
await token.loadFixtures(); | |
}); | |
beforeEach(async () => {}); | |
it("can set up converters", async () => { | |
expect.assertions(2); | |
// init network account | |
await network.contract.setnettoken({ | |
network_token: token.accountName // the account managing BNT token | |
}); | |
// init converter | |
await converter.contract.setnetwork({ | |
network: network.accountName | |
}); | |
await converter.contract.setmultitokn({ | |
multi_token: token.accountName | |
}); | |
await converter.contract.create( | |
{ | |
owner: creator.accountName, | |
token_code: SmartSymbol.code, | |
initial_supply: `10.0000` | |
}, | |
[{ actor: creator.accountName, permission: `active` }] | |
); | |
expect( | |
converter.getTableRowsScoped(`converters`)[SmartSymbol.code] | |
).toEqual([ | |
{ | |
currency: "4,BNTAAAA", | |
fee: "0", | |
owner: "creator", | |
stake_enabled: false | |
} | |
]); | |
await converter.contract.setreserve( | |
{ | |
converter_currency_code: `BNTAAAA`, | |
currency: "10,BNT", | |
contract: token.accountName, | |
ratio: 500000 | |
}, | |
[{ actor: creator.accountName, permission: `active` }] | |
); | |
await converter.contract.setreserve( | |
{ | |
converter_currency_code: `BNTAAAA`, | |
currency: "4,AAAA", | |
contract: token.accountName, | |
ratio: 500000 | |
}, | |
[{ actor: creator.accountName, permission: `active` }] | |
); | |
await token.contract.transfer( | |
{ | |
from: creator.accountName, | |
to: converter.accountName, | |
quantity: "10.0000 AAAA", | |
memo: "fund;BNTAAAA" | |
}, | |
[{ actor: creator.accountName, permission: `active` }] | |
); | |
await token.contract.transfer( | |
{ | |
from: creator.accountName, | |
to: converter.accountName, | |
quantity: "10.0000000000 BNT", | |
memo: "fund;BNTAAAA" | |
}, | |
[{ actor: creator.accountName, permission: `active` }] | |
); | |
console.log(token.getTableRowsScoped(`accounts`)); | |
// await converter.contract.updatefee( | |
// { | |
// currency: "BNTAAAA", | |
// fee: 2000 | |
// }, | |
// [{ actor: creator.accountName, permission: `active` }] | |
// ); | |
expect(converter.getTableRowsScoped(`reserves`)[SmartSymbol.code]).toEqual([ | |
{ | |
balance: "10.0000000000 BNT", | |
contract: "token", | |
ratio: "500000" | |
}, | |
{ | |
balance: "10.0000 AAAA", | |
contract: "token", | |
ratio: "500000" | |
} | |
]); | |
}); | |
it("can convert BNT to AAAA", async () => { | |
expect.assertions(1); | |
const buildMemo = ({ relay, toSymbolCode, minReturn, toAccount }) => | |
`1,${relay}:BNTAAAA AAAA,${minReturn},${toAccount}`; | |
const bntToConvert = `20.0000000000 BNT`; | |
// convert BNT to AAAA (actually BNT -> BNTAAAA -> AAAA) | |
await token.contract.transfer( | |
{ | |
from: user1.accountName, | |
to: network.accountName, // all trades must go through network account | |
quantity: bntToConvert, | |
memo: buildMemo({ | |
relay: converter.accountName, | |
toSymbolCode: SmartSymbol.code, | |
minReturn: `6.0000`, | |
toAccount: user1.accountName | |
}) | |
}, | |
[{ actor: user1.accountName, permission: `active` }] | |
); | |
// manually computed as | |
// input / (current_from_balance + input) * current_to_balance; | |
// 20 BNT / (10 BNT + 20 BNT) * 10 AAAA = 2/3 * 10 AAAA | |
const expectedATokenBalance = `6.6666 AAAA` | |
expect(token.getTableRowsScoped(`accounts`)[user1.accountName]).toContainEqual({ | |
balance: expectedATokenBalance | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment