Skip to content

Commit

Permalink
add: single swap to sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
sirpy committed Jul 4, 2024
1 parent 162b48d commit 05b0144
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/sdk-js/scripts/createPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ const createUbiPool = async () => {
minActiveUsers: ethers.BigNumber.from(100),
claimForEnabled: true,
maxClaimAmount: ethers.utils.parseEther('100'),
maxMembers: 500,
maxClaimers: 500,
onlyMembers: true,
};

const pool = await sdk.createUbiPoolWithAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const testPoolSettings = [
maxMemberPerMonth: 10000,
maxTotalPerMonth: 100000,
},
false,
];
describe('GoodCollective SDK', () => {
beforeAll(async () => {
Expand All @@ -67,6 +68,8 @@ describe('GoodCollective SDK', () => {
description: 'zz',
twitter: '@ss',
email: '[email protected]',
headerImage: '',
logo: '',
});

expect(uri).equal('abc');
Expand Down Expand Up @@ -172,4 +175,22 @@ describe('GoodCollective SDK', () => {

expect(tx.wait()).not.rejects;
});

it('should support single with swap', async () => {
const pool = await sdk.createPool(...testPoolSettings);

const balance = await gooddollar.balanceOf(wallet.address);
const routerBalance = await gooddollar.balanceOf(await pool.swapRouter());
console.log({ balance, router: await pool.swapRouter(), routerBalance });
await (await gooddollar.connect(wallet).approve(pool.address, '1000')).wait();
const tx = await sdk.supportSingleWithSwap(wallet, pool.address, {
amount: 1000,
minReturn: 100000000000000,
path: '0x',
swapFrom: gooddollar.address,
deadline: (Date.now() + 1000000 / 1000).toFixed(0),
});

expect(tx.wait()).not.rejects;
});
});
26 changes: 25 additions & 1 deletion packages/sdk-js/src/goodcollective/goodcollective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class GoodCollectiveSDK {
poolIpfs: string,
poolSettings: PoolSettings,
poolLimits: PoolLimits,
isBeacon: boolean
isBeacon = true
) {
poolSettings.nftType = 0; // force some type, this will be re-assigned by the factory
const createMethod = isBeacon
Expand Down Expand Up @@ -447,6 +447,30 @@ export class GoodCollectiveSDK {
return op.exec(signer);
}

/**
* Single donation using superfluid batch call
* Executes a batch of operations including token approval and calling a function on the pool contract.
* @param {ethers.Signer} signer - The signer object for the transaction.
* @param {string} poolAddress - The address of the pool contract.
* @param {string} amount - The amount of tokens to transfer.
* @returns {Promise<ethers.ContractTransaction>} A promise that resolves to a transaction object when the operation is complete.
*/
async supportSingleWithSwap(signer: ethers.Signer, poolAddress: string, swap: SwapData) {
const tcabi = ['function allowance(address _from, address _to) view returns (uint256 allowance)'];
const token = new ethers.Contract(await swap.swapFrom, tcabi, signer);
const signerAddress = await signer.getAddress();
const allowance = await token.allowance(signerAddress, poolAddress);
console.log({ allowance, amount: await swap.amount });
if (allowance.lt(ethers.BigNumber.from(swap.amount))) {
throw new Error('Not enough allowance');
}
const tx = await this.pool
.attach(poolAddress)
.connect(signer)
.supportWithSwap(signerAddress, swap, '0x', { gasLimit: 2000000 });
return tx;
}

async swap(signer: ethers.Signer, poolAddress: string, swap: SwapData) {
const signerAddress = await signer.getAddress();

Expand Down

0 comments on commit 05b0144

Please sign in to comment.