Skip to content

Commit

Permalink
add allow trust test
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Jun 25, 2020
1 parent 5f15ede commit 2ddd6df
Showing 1 changed file with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions test/trust_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,145 @@ void main() {
}
assert(!found);
});

test('allow trust test', () async {
KeyPair issuerKeipair = KeyPair.random();
KeyPair trustorKeipair = KeyPair.random();

String issuerAccountId = issuerKeipair.accountId;
String trustorAccountId = trustorKeipair.accountId;

await FriendBot.fundTestAccount(trustorAccountId);

AccountResponse trustorAccount = await sdk.accounts.account(trustorAccountId);
CreateAccountOperationBuilder caob = CreateAccountOperationBuilder(issuerAccountId, "10");
Transaction transaction = TransactionBuilder(trustorAccount, Network.TESTNET).addOperation(caob.build()).build();
transaction.sign(trustorKeipair);
SubmitTransactionResponse response = await sdk.submitTransaction(transaction);
assert(response.success);

AccountResponse issuerAccount = await sdk.accounts.account(issuerAccountId);
SetOptionsOperationBuilder sopb = SetOptionsOperationBuilder();
sopb.setSetFlags(3); // Auth required, auth revocable
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(sopb.build()).build();
transaction.sign(issuerKeipair);
response = await sdk.submitTransaction(transaction);
assert(response.success);
issuerAccount = await sdk.accounts.account(issuerAccountId);
assert(issuerAccount.flags.authRequired);
assert(issuerAccount.flags.authRevocable);
assert(!issuerAccount.flags.authImmutable);

String assetCode = "ASTRO";
Asset astroDollar = AssetTypeCreditAlphaNum12(assetCode, issuerAccountId);

String limit = "10000";
ChangeTrustOperationBuilder ctob = ChangeTrustOperationBuilder(astroDollar, limit);
transaction = TransactionBuilder(trustorAccount, Network.TESTNET).addOperation(ctob.build()).build();
transaction.sign(trustorKeipair);

response = await sdk.submitTransaction(transaction);
assert(response.success);

trustorAccount = await sdk.accounts.account(trustorAccountId);
bool found = false;
for (Balance balance in trustorAccount.balances) {
if (balance.assetCode == assetCode) {
found = true;
//assert(double.parse(balance.balance) == 100.0);
break;
}
}
assert(found);


PaymentOperation po = PaymentOperationBuilder(trustorAccountId, astroDollar, "100").build();
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(po).build();
transaction.sign(issuerKeipair);

response = await sdk.submitTransaction(transaction);
assert(!response.success); // not authorized.

AllowTrustOperation aop = AllowTrustOperationBuilder(trustorAccountId, assetCode, 1).build(); // authorize
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(aop).build();
transaction.sign(issuerKeipair);

response = await sdk.submitTransaction(transaction);
assert(response.success);

po = PaymentOperationBuilder(trustorAccountId, astroDollar, "100").build();
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(po).build();
transaction.sign(issuerKeipair);

response = await sdk.submitTransaction(transaction);
assert(response.success); // authorized.

String amountSelling = "100";
String price = "0.5";

CreatePassiveSellOfferOperation cpso = CreatePassiveSellOfferOperationBuilder(astroDollar, Asset.NATIVE, amountSelling, price).build();
transaction = TransactionBuilder(trustorAccount, Network.TESTNET).addOperation(cpso).build();
transaction.sign(trustorKeipair);
response = await sdk.submitTransaction(transaction);
assert(response.success);

List<OfferResponse> offers = (await sdk.offers.forAccount(trustorAccountId).execute()).records;
assert(offers.length == 1);
OfferResponse offer = offers.first;
assert(offer.buying == Asset.NATIVE);
assert(offer.selling == astroDollar);

aop = AllowTrustOperationBuilder(trustorAccountId, assetCode, 0).build(); // authorize
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(aop).build();
transaction.sign(issuerKeipair);

response = await sdk.submitTransaction(transaction);
assert(response.success);

offers = (await sdk.offers.forAccount(trustorAccountId).execute()).records;
assert(offers.length == 0);

trustorAccount = await sdk.accounts.account(trustorAccountId);
found = false;
for (Balance balance in trustorAccount.balances) {
if (balance.assetCode == assetCode) {
found = true;
assert(double.parse(balance.balance) == 100.0);
break;
}
}
assert(found);

aop = AllowTrustOperationBuilder(trustorAccountId, assetCode, 1).build(); // authorize
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(aop).build();
transaction.sign(issuerKeipair);
response = await sdk.submitTransaction(transaction);
assert(response.success);

cpso = CreatePassiveSellOfferOperationBuilder(astroDollar, Asset.NATIVE, amountSelling, price).build();
transaction = TransactionBuilder(trustorAccount, Network.TESTNET).addOperation(cpso).build();
transaction.sign(trustorKeipair);
response = await sdk.submitTransaction(transaction);
assert(response.success);

offers = (await sdk.offers.forAccount(trustorAccountId).execute()).records;
assert(offers.length == 1);

aop = AllowTrustOperationBuilder(trustorAccountId, assetCode, 2).build(); // authorized to maintain liabilities.
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(aop).build();
transaction.sign(issuerKeipair);
response = await sdk.submitTransaction(transaction);
assert(response.success);

offers = (await sdk.offers.forAccount(trustorAccountId).execute()).records;
assert(offers.length == 1);

po = PaymentOperationBuilder(trustorAccountId, astroDollar, "100").build();
transaction = TransactionBuilder(issuerAccount, Network.TESTNET).addOperation(po).build();
transaction.sign(issuerKeipair);

response = await sdk.submitTransaction(transaction);
assert(!response.success); // is not authorized for new funds

});
}

0 comments on commit 2ddd6df

Please sign in to comment.