Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kosujs/payable functions #179

Merged
merged 4 commits into from Jul 24, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Adding functions and tests for new bonding curve contract methods.

  • Loading branch information
Freydal committed Jul 23, 2019
commit 7ae6295161404b2d091b1a23e319d66b38b7cf07
@@ -157,4 +157,60 @@ export class KosuToken {
const contract = await this.getContract();
return contract.allowance.callAsync(owner, spender);
}

/**
* Calculated tokens to be minted from deposited ether.
*
* @param etherInput Amount of ether to be submitted to generate tokens.
* @returns Estimation of tokens to be minted.
*/
public async estimateEtherToToken(etherInput: BigNumber): Promise<BigNumber> {
const contract = await this.getContract();
return contract.estimateEtherToToken.callAsync(etherInput);
}

/**
* Calculates ether to be returned for burning tokens.
*
* @param tokensToBurn Amount of tokens to burn for returned ether.
* @returns Estimation of ether to be returned.
*/
public async estimateTokenToEther(tokensToBurn: BigNumber): Promise<BigNumber> {
const contract = await this.getContract();
return contract.estimateTokenToEther.callAsync(tokensToBurn);
}

/**
* Sends ether to the contract to bond tokens.
*
* @param value Amount of wei to deposit
* @param minPayout Minimum amount of tokens required to be minted to prevent transaction from reverting.
* @returns Logs from the transaction block.
*/
public async bondTokens(value: BigNumber, minPayout: BigNumber): Promise<TransactionReceiptWithDecodedLogs> {
const contract = await this.getContract();
return contract.bondTokens.awaitTransactionSuccessAsync(minPayout, { value });
}

/**
* Releases tokens to be burned and return bonded ether.
*
* @param tokensToBurn Amount of tokens to burn for returned ether.
* @returns Logs from the transaction block.
*/
public async releaseTokens(tokensToBurn: BigNumber): Promise<TransactionReceiptWithDecodedLogs> {
const contract = await this.getContract();
return contract.releaseTokens.awaitTransactionSuccessAsync(tokensToBurn);
}

/**
* Sends ether to the contract to bond tokens.
*
* @param value Amount of wei to deposit
* @returns Logs from the transaction block.
*/
public async pay(value: BigNumber): Promise<TransactionReceiptWithDecodedLogs> {
const contract = await this.getContract();
return this.web3Wrapper.sendTransactionAsync({ from: await this.web3.eth.getCoinbase(), to: contract.address, value, gas: 70000 }).then(async txHash => this.web3Wrapper.awaitTransactionSuccessAsync(txHash));
}
}
@@ -142,4 +142,15 @@ export class PosterRegistry {
const contract = await this.getContract();
return contract.releaseTokens.awaitTransactionSuccessAsync(amount);
}

/**
* Sends ether to the contract to bond and register tokens for posting.
*
* @param value Amount of wei to deposit
* @returns Logs from the transaction block.
*/
public async pay(value: BigNumber): Promise<TransactionReceiptWithDecodedLogs> {
const contract = await this.getContract();
return this.web3Wrapper.sendTransactionAsync({ from: await this.web3.eth.getCoinbase(), to: contract.address, value, gas: 220000 }).then(async txHash => this.web3Wrapper.awaitTransactionSuccessAsync(txHash));
}
}
@@ -216,4 +216,15 @@ export class Treasury {
const contract = await this.getContract();
return this.kosuToken.approve(contract.address, value);
}

/**
* Sends ether to the contract to bond and deposit tokens.
*
* @param value Amount of wei to deposit
* @returns Logs from the transaction block.
*/
public async pay(value: BigNumber): Promise<TransactionReceiptWithDecodedLogs> {
const contract = await this.getContract();
return this.web3Wrapper.sendTransactionAsync({ from: await this.web3.eth.getCoinbase(), to: contract.address, value, gas: 120000 }).then(async txHash => this.web3Wrapper.awaitTransactionSuccessAsync(txHash));
}
}
@@ -17,35 +17,88 @@ describe("KosuToken", () => {

describe("transfer", () => {
it("should succeed with the expected log", async () => {
await kosu.kosuToken.transfer(accounts[0], 1, { from: accounts[0] }).then(({ logs }) => {
await kosu.kosuToken.transfer(accounts[0], 1).then(({ logs }) => {
logs.length.should.eq(1);
});
});
});

describe("transferFrom", () => {
it("should succeed with the expected log", async () => {
await kosu.kosuToken.approve(accounts[0], 1, { from: accounts[0] });
await kosu.kosuToken.transferFrom(accounts[0], accounts[0], 1, { from: accounts[0] }).then(({ logs }) => {
await kosu.kosuToken.approve(accounts[0], 1);
await kosu.kosuToken.transferFrom(accounts[0], accounts[0], 1).then(({ logs }) => {
logs.length.should.eq(2);
});
});
});

describe("approve", () => {
it("should succeed with the expected log", async () => {
await kosu.kosuToken.approve(accounts[0], 1, { from: accounts[0] }).then(({ logs }) => {
await kosu.kosuToken.approve(accounts[0], 1).then(({ logs }) => {
logs.length.should.eq(1);
});
});
});

describe("allowance", () => {
it("should report the allowance", async () => {
await kosu.kosuToken.approve(accounts[0], 1, { from: accounts[0] });
await kosu.kosuToken.approve(accounts[0], 1);
await kosu.kosuToken.allowance(accounts[0], accounts[0]).then(allowance => {
allowance.eq(1).should.eq(true);
});
});
});

describe("bondTokens", () => {
it("should bond ether for tokens", async () => {
const initialBalance = await kosu.kosuToken.balanceOf(accounts[0]);
await kosu.kosuToken.pay(TestValues.oneEther);
const finalBalance = await kosu.kosuToken.balanceOf(accounts[0]);
const difference = finalBalance.minus(initialBalance);
difference.toNumber().should.be.gt(0);

await kosu.kosuToken.releaseTokens(difference);
});
});

describe("releaseTokens", () => {
it("should burn tokens for ether", async () => {
const initialBalance = await kosu.kosuToken.balanceOf(accounts[0]);
await kosu.kosuToken.pay(TestValues.oneEther);
const finalBalance = await kosu.kosuToken.balanceOf(accounts[0]);
const difference = finalBalance.minus(initialBalance);
difference.toNumber().should.be.gt(0);

const initialEth = await web3Wrapper.getBalanceInWeiAsync(accounts[0]);
await kosu.kosuToken.releaseTokens(difference);
const finalEth = await web3Wrapper.getBalanceInWeiAsync(accounts[0]);
finalEth.minus(initialEth).toNumber().should.be.gt(0);
});
});


describe("estimateEtherToToken", () => {
it("should estimate tokens generated from ether", async () => {
await kosu.kosuToken.estimateEtherToToken(TestValues.oneEther).then(val => val.toNumber().should.be.gt(0));
});
});

describe("estimateTokenToEther", () => {
it("should estimate ether returned from tokens", async () => {
await kosu.kosuToken.estimateTokenToEther(TestValues.oneEther).then(val => val.toNumber().should.be.gt(0));
});
});


describe("pay", () => {
it("should generate tokens", async () => {
const initialBalance = await kosu.kosuToken.balanceOf(accounts[0]);
await kosu.kosuToken.pay(TestValues.oneEther);
const finalBalance = await kosu.kosuToken.balanceOf(accounts[0]);
const difference = finalBalance.minus(initialBalance);
difference.toNumber().should.be.gt(0);

await kosu.kosuToken.releaseTokens(difference);
});
});
});
@@ -0,0 +1,20 @@
describe("PosterRegistry", () => {
describe("pay", () => {
it("should generate treasury balance", async () => {
const initialSystemBalance = await kosu.treasury.systemBalance(accounts[0]);
const initialPosterBalance = await kosu.posterRegistry.tokensRegisteredFor(accounts[0]);
await kosu.posterRegistry.pay(TestValues.oneEther);
const finalSystemBalance = await kosu.treasury.systemBalance(accounts[0]);
const finalPosterBalance = await kosu.posterRegistry.tokensRegisteredFor(accounts[0]);

const difference = finalSystemBalance.minus(initialSystemBalance);
const posterDifference = finalPosterBalance.minus(initialPosterBalance);

difference.toString().should.eq(posterDifference.toString());

await kosu.posterRegistry.releaseTokens(difference);
await kosu.treasury.withdraw(difference);
await kosu.kosuToken.releaseTokens(difference);
});
});
});
@@ -0,0 +1,14 @@
describe("Treasury", () => {
describe("pay", () => {
it("should generate treasury balance", async () => {
const initialSystemBalance = await kosu.treasury.systemBalance(accounts[0]);
await kosu.treasury.pay(TestValues.oneEther);
const finalSystemBalance = await kosu.treasury.systemBalance(accounts[0]);
const difference = finalSystemBalance.minus(initialSystemBalance);
difference.toNumber().should.be.gt(0);

await kosu.treasury.withdraw(difference);
await kosu.kosuToken.releaseTokens(difference);
});
});
});
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.